-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11724.cpp
More file actions
57 lines (45 loc) · 868 Bytes
/
BOJ_11724.cpp
File metadata and controls
57 lines (45 loc) · 868 Bytes
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
// 21/10/22
#include <iostream>
using namespace std;
int parent[1000+1];
int n, m;
int Find_parent(int node)
{
if (parent[node] == node)
return node;
return parent[node] = Find_parent(parent[node]);
}
void Input(void)
{
cin >> n >> m;
for (int i=1; i<=n; i++)
parent[i] = i;
while (m--)
{
int node1, node2;
cin >> node1 >> node2;
int parent1 = Find_parent(node1);
int parent2 = Find_parent(node2);
parent[parent2] = parent1;
}
}
int Solve(void)
{
for (int i=1; i<=n; i++)
parent[i] = Find_parent(i);
int answer = 0;
for (int i=1; i<=n; i++)
{
if (parent[i] == i)
answer++;
}
return answer;
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
Input();
cout << Solve();
return 0;
}