#include <iostream> #include <set> #include <vector> using namespace std; const int N = 1e6 + 10; int f[N];//并查集 set<int> nodes;//有哪些节点 //查找n的根 int find(int n) { if (f[n] == n) return n; return find(f[n]); } //简单合并,不涉及高度 void merge(int a, int b) { int r1 = find(a), r2 = find(b); if (r1 != r2) f[r2] = r1; } void init() { for (int i = 0; i < N; i++) f[i] = i; } int count() { set<int> s; for (int node:nodes) { int root = find(node); s.insert(root); } return s.size(); } int main() { int x, y; init(); while (cin >> x >> y) { nodes.insert(x); nodes.insert(y); merge(x, y); } cout << count() << endl; } // 64 位输出请用 printf("%lld")