标签:highlight tor ++ const ini mes == string cpp
基本思想:
无;
关键点:
无;
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<set> using namespace std; const int maxn = 1000000; int father[maxn]; set<int>se; int findfather(int a) { while (father[a] != a) a = father[a]; return a; } void Union(int a, int b) { int aa = findfather(a); int bb = findfather(b); father[aa] = bb; } void init() { for (int i = 0; i < maxn; i++) father[i] = i; } int main() { int n,m; int cnt=0; init(); while (cin >>m>>n) { Union(m, n); se.insert(m); se.insert(n); } for (set<int>::iterator it = se.begin(); it != se.end(); it++) { if (findfather(*it) == *it) cnt++; } cout << cnt << endl; return 0; }
标签:highlight tor ++ const ini mes == string cpp
原文地址:https://www.cnblogs.com/songlinxuan/p/12490065.html