Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- using namespace std;
- int INF = 1001;
- vector<vector<int>> arr(INF, vector<int>(INF));
- vector<int> color(INF, 0);
- int n;
- void f(int i) {
- color[i] = 1;
- for (int j = 1; j <= n; j++) {
- if (color[j] == 0 && ((arr[0][i] == arr[0][j]) || (arr[1][i] == arr[1][j]))) {
- f(j);
- }
- }
- }
- int main() {
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> arr[0][i] >> arr[1][i];
- }
- int ans = -1;
- for (int i = 1; i <= n; i++) {
- if (color[i] == 0) {
- f(i);
- ans++;
- }
- }
- cout << ans;
- return 0;
- }
Add Comment
Please, Sign In to add comment