N = 100 + 10
fa, x, y, ans = [0] * N, [0] * N, [0] * N, 0
def find(root):
if root == fa[root]: return root
fa[root] = find(fa[root])
return fa[root]
def merge(a, b):
ra, rb = find(a), find(b)
if ra == rb: return
fa[ra] = rb
n = int(input())
for i in range(1, n + 1):
_x, _y = map(int, input().split())
x[i] = _x
y[i] = _y
for i in range(1, n + 1): fa[i] = i
for a in range(1, n + 1):
for b in range(a + 1, n + 1):
if x[a] == x[b] or y[a] == y[b]:
merge(a, b)
for i in range(1, n + 1):
if fa[i] == i: ans += 1
print(str(ans - 1))
#include <bits/stdc++.h>
using namespace std;
#define futaba ios_base::sync_with_stdio(false); cin.tie(NULL);
#define rio return 0;
#define fi first
#define se second
// Fun things are fun. //
pair<int, int> a[105];
vector<int> adj[105];
bool vis[105];
void dfs(int n) {
vis[n] = true;
for(auto i : adj[n]) {
if(!vis[i]) {
dfs(i);
}
}
}
int main() {
/* freopen(".txt", "r", stdin);
freopen(".txt", "w", stdout); */
futaba
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i].fi >> a[i].se;
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i].fi == a[j].fi) {
adj[i].push_back(j);
adj[j].push_back(i);
}
if(a[i].se == a[j].se) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
int ans = 0;
for(int i = 0; i < n; i++) vis[i] = false;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
dfs(i);
ans++;
}
}
ans--;
cout << ans << '\n';
rio
}
405A - Gravity Flip | 499B - Lecture |
709A - Juicer | 1358C - Celex Update |
1466B - Last minute enhancements | 450B - Jzzhu and Sequences |
1582C - Grandma Capa Knits a Scarf | 492A - Vanya and Cubes |
217A - Ice Skating | 270A - Fancy Fence |
181A - Series of Crimes | 1638A - Reverse |
1654C - Alice and the Cake | 369A - Valera and Plates |
1626A - Equidistant Letters | 977D - Divide by three multiply by two |
1654B - Prefix Removals | 1654A - Maximum Cake Tastiness |
1649A - Game | 139A - Petr and Book |
1612A - Distance | 520A - Pangram |
124A - The number of positions | 1041A - Heist |
901A - Hashing Trees | 1283A - Minutes Before the New Year |
1654D - Potion Brewing Class | 1107B - Digital root |
25A - IQ test | 785A - Anton and Polyhedrons |