auto optimize_cpp_stdio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return0; }(); classSolution { public: structUF { vector<int> fa; int count; UF(int n) : fa(n), count(n) { for (int i = 0; i < n; ++i) fa[i] = i; } intfind(int u){ return u == fa[u] ? u : fa[u] = find(fa[u]); } voidunite(int u, int v) { int up = find(u), vp = find(v); if (up != vp) { fa[up] = vp; count--; } } boolconnect(int u, int v){ return find(u) == find(v); } intgetCount(){ return count; } }; intremoveStones(vector<vector<int>> &stones) { int n = stones.size(); unordered_map<int, int> mp; int cnt = 0; for (int i = 0; i < n; ++i) { int &x = stones[i][0], &y = stones[i][1]; y += 10001; if (!mp.count(x)) x = mp[x] = cnt++; else x = mp[x]; if (!mp.count(y)) y = mp[y] = cnt++; else y = mp[y]; } UF uf(cnt); for (int i = 0; i < n; ++i) { int x = stones[i][0], y = stones[i][1]; uf.unite(x, y); } return n - uf.getCount(); } };