-
Notifications
You must be signed in to change notification settings - Fork 0
/
217a.cpp
190 lines (174 loc) · 2.9 KB
/
217a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define watch(x) cout << (#x) << " is " << (x) << endl
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gcd __gcd
#define nof1 __builtin_popcountll
#define nofleading_zerosull __builtin_clzll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<long long int> vl;
const double pi = 2 * acos(0.0);
const int inf = 0x3f3f3f3f;//(ll) 9e18
const double infd = 1.0/0.0;
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define clr(a) memset(a, 0, sizeof(a))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a+n
//#define endl "\n"
long long power(long long x, long long y,long long MOD)
{
long long res = 1;
x = x % MOD;
while (y > 0)
{
if (y & 1)
res = (res*x) % MOD;
y = y>>1;
x = (x*x) % MOD;
}
return res;
}
long long mul(long long a,long long b,long long MOD)
{
if(b==1)
{
return a;
}
if(b%2==0)
{
return 2*(mul(a,b/2,MOD)%MOD);
}
else
{
return (a+(2*(mul(a,b/2,MOD))%MOD))%MOD;
}
}
/*ll ncr[6001][6001];
void nCr(long long MOD)
{
for(ll i=0;i<=6000;i++)
{
for(ll k=0;k<=6000;k++)
{
if(k<=i)
{
if((k==0)||(k==i))
ncr[i][k]=1;
else
ncr[i][k]=(ncr[i-1][k-1]+ncr[i-1][k])%MOD;
}
}
}
}
ll inv[100005];
void mulmodinv(long long MOD)
{
inv[1]=1;
inv[0]=1;
for(int i = 2; i <= 100005; i++)
inv[i] = inv[MOD % i]*(MOD-MOD / i) % MOD;
}*/
bool ispow2(ll n)
{
if(((n & (n-1)) == 0) && n!=0)
{
return true;
}
return false;
}
bool prime(int x)
{
if(x==1)
{
return false;
}
if(x==2)
{
return true;
}
if(x%2==0)
{
return false;
}
for(int i=3;i*i<=x;i+=2)
{
if(x%i==0)
{
return false;
}
}
return true;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int coprime(int a, int b)
{
if(a==b)
return 0;
if(prime(a) && prime(b))
return 1;
if((a==2 && b%2!=0) ||(b==2 && a%2!=0))
return 1;
if((a%2!=0 && prime(b) && a<b ) || (b%2!=0 && prime(a) && a>b))
return 1;
if(abs(a-b)==1)
return 1;
if(a==1 || b==1)
return 1;
return gcd(a,b);
}
unsigned long long lcm (unsigned a, unsigned b)
{
return ((unsigned long long) a) * (b / gcd(a, b));
}
//======================================================================
int vis[101],x[101],y[101],n;
void dfs(int u)
{
vis[u]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i] && (x[u]==x[i] || y[u]==y[i]))
{
dfs(i);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
clr(vis);
int ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
dfs(i);
ans++;
}
}
cout<<ans-1<<endl;
return 0;
}