File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+ #include < vector>
3
+
4
+ using namespace std ;
5
+
6
+ const int MAX_N = 105 ;
7
+ vector <int > visited (MAX_N, false );
8
+ vector <int > graph[MAX_N];
9
+
10
+ void dfs (int v)
11
+ {
12
+ visited[v] = true ;
13
+
14
+ for (int i = 0 ; i < graph[v].size (); i++)
15
+ {
16
+ int child = graph[v][i];
17
+
18
+ if (!visited[child])
19
+ dfs (child);
20
+ }
21
+ }
22
+
23
+ int main ()
24
+ {
25
+ int no_of_points;
26
+ scanf (" %d" , &no_of_points);
27
+
28
+ vector <int > X (no_of_points + 1 );
29
+ vector <int > Y (no_of_points + 1 );
30
+ for (int i = 1 ; i <= no_of_points; i++)
31
+ scanf (" %d %d" , &X[i], &Y[i]);
32
+
33
+ int no_of_components = 0 ;
34
+ for (int i = 1 ; i <= no_of_points; i++)
35
+ {
36
+ for (int j = 1 ; j < i; j++)
37
+ {
38
+ if (X[i] == X[j] || Y[i] == Y[j])
39
+ {
40
+ graph[i].push_back (j);
41
+ graph[j].push_back (i);
42
+ }
43
+ }
44
+ }
45
+
46
+ for (int i = 1 ; i <= no_of_points; i++)
47
+ {
48
+ if (!visited[i])
49
+ {
50
+ no_of_components++;
51
+
52
+ dfs (i);
53
+ }
54
+ }
55
+
56
+ printf (" %d\n " , no_of_components - 1 );
57
+ return 0 ;
58
+ }
You can’t perform that action at this time.
0 commit comments