8000 Create 1717.cpp · qus0in/basic-algo-lecture@df96216 · GitHub
[go: up one dir, main page]

Skip to content

Commit df96216

Browse files
Create 1717.cpp
1 parent d9c2213 commit df96216

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Appendix D/1717.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// http://boj.kr/bf81d062967a4ba2993ba38a135b2bab
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
vector<int> p(1000001, -1);
6+
7+
int find(int x){
8+
if(p[x] < 0)
9+
return x;
10+
return p[x] = find(p[x]);
11+
}
12+
13+
bool uni(int u, int v){
14+
u = find(u);
15+
v = find(v);
16+
if(u == v)
17+
return false;
18+
if(p[v] < p[u]) // v의 랭크가 더 큰 경우
19+
swap(u, v); // u, v를 swap
20+
// 위의 if문으로 인해 u의 랭크 >= v의 랭크이다
21+
if(p[u] == p[v]) // 랭크가 같은 경우에 대한 처리
22+
p[u]--;
23+
p[v] = u; // v를 u의 자식으로 만든다
24+
return true;
25+
}
26+
27+
int main(){
28+
ios::sync_with_stdio(0);
29+
cin.tie(0);
30+
31+
int n, m;
32+
cin >> n >> m;
33+
while(m--){
34+
int q, a, b;
35+
cin >> q >> a >> b;
36+
if(q == 0)
37+
uni(a, b);
38+
else{
39+
if(find(a) == find(b))
40+
cout << "YES\n";
41+
else
42+
cout << "NO\n";
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)
0