-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1717.cpp
More file actions
64 lines (56 loc) · 788 Bytes
/
1717.cpp
File metadata and controls
64 lines (56 loc) · 788 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
58
59
60
61
62
63
64
// 1717. 집합의 표현
// 2019.05.18
// Disjoint-set
#include<iostream>
using namespace std;
int parent[1000001];
// 초기화
void MakeSet(int v)
{
parent[v] = v;
}
// v의 최상위 노드 찾기
int FindSet(int v)
{
if (v == parent[v])
{
return v;
}
parent[v] = FindSet(parent[v]);
return parent[v];
}
// u를 v의 부분집합으로 넣기
void UnionSet(int u, int v)
{
parent[FindSet(u)] = FindSet(v);
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
MakeSet(i);
}
for (int i = 0; i < m; i++)
{
int cnt, a, b;
scanf("%d %d %d", &cnt, &a, &b);
if (cnt == 0)
{
UnionSet(a, b);
}
else
{
if (FindSet(a) == FindSet(b))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
return 0;
}