UVA_459_Graph Connectivity
2016-04-07 17:59
357 查看
#include<iostream> #include<sstream> #include<string> #include<vector> #include<list> #include<set> #include<map> #include<stack> #include<queue> #include<algorithm> #pragma warning(disable:4996) using std::cin; using std::cout; using std::endl; using std::stringstream; using std::string; using std::vector; using std::list; using std::pair; using std::set; using std::multiset; using std::map; using std::multimap; using std::stack; using std::queue; class DisjoinSet { private: vector<int>represent, rank; public: DisjoinSet(const int &n):represent(n),rank(n) { for (int i = 0; i < n; i++) { represent[i] = i; } } int find(int i) { return represent[i] = represent[i] == i ? i : find(represent[i]); } void merge(const int &x, const int &y) { auto a = find(x);auto b = find(y); if (rank[a] < rank[b]) { represent[a] = b; } else { represent[b] = a; if (rank[b] == rank[a]) { rank[a]++; } } } int count() { set<int>ret; for (size_t i = 0; i < represent.size(); i++) { ret.insert(find(i)); } return ret.size(); } }; int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int T; cin >> T; while (T--) { char n; cin >> n; cin.get(); DisjoinSet union_set(n - 'A'+1); string str; while (getline(cin, str)) { if (str.empty()) { break; } union_set.merge(str[0] - 'A', str[1] - 'A'); } cout << union_set.count() << endl; if (T) { cout << endl; } } return 0; }
相关文章推荐
- 文本框双击
- 数据库sql
- LintCode-三数之和
- jquery 获取input radio/checkbox 的值 【注意写法】
- Android属性(property)机制
- 图像算法:图像阈值分割
- SpringMVC通过model设置中文消息乱码问题
- Android webview使用详解
- Implementing a Neural Network from Scratch in Python – An Introduction
- hdu1432
- struts2 select标签
- CFileDialog 多选文件,取得的文件路径错误BUG
- qt button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画)
- iOS蓝牙4.0开发(BLE)
- LintCode-三数之和 II
- 数据分析入门(谁说菜鸟不会数据分析)
- 胖子哥的大数据之路(16):大数据采集标准-我们到底需要什么样的数据?
- 二叉树的部分操作
- 第六周项目1==分数类的雏形
- 使用OVS DPDK (by quqi99)