//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") #include //#include //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int SZ = 500000; class UF { public: int P[SZ + 1], kazu[SZ + 1]; UF() : P() { rep1(i, SZ) P[i] = -1; } int find(int A) { if (P[A] < 0) return A; return P[A] = find(P[A]); } bool unite(int A, int B) { int a = find(A); int b = find(B); if (a == b) return false; if (P[a] > P[b]) swap(a, b); P[a] += P[b]; kazu[a] += kazu[b]; P[b] = a; return true; } } uf; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; rep1(i, M) uf.kazu[i] = 1; rep1(i, N) uf.kazu[i + 200000] = 0; rep1(i, N) { int b, c; cin >> b >> c; uf.unite(b, c + 200000); } int kotae = 0; rep1(i, 500000) { if (uf.find(i) == i && uf.kazu[i]) { kotae += uf.kazu[i] - 1; } } co(kotae); Would you please return 0; }