8000 Create 7511.cpp · terry-koo/basic-algo-lecture@cdedebb · GitHub
[go: up one dir, main page]

Skip to content

Commit cdedebb

Browse files
Create 7511.cpp
1 parent df96216 commit cdedebb

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Appendix D/7511.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// http://boj.kr/48d21e5cb37f46978ecca75d63feebb0
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+
int t;
31+
cin >> t;
32+
33+
for(int tc = 1; tc <= t; tc++){
34+
cout << "Scenario " << tc << ":\n";
35+
int n, k;
36+
cin >> n >> k;
37+
fill(p.begin() + 1, p.begin() + n + 1, -1);
38+
while(k--){
39+
int a, b;
40+
cin >> a >> b;
41+
uni(a, b);
42+
}
43+
44+
int m;
45+
cin >> m;
46+
while(m--){
47+
int u, v;
48+
cin >> u >> v;
49+
cout << (find(u) == find(v)) << '\n';
50+
}
51+
cout << '\n';
52+
}
53+
}

0 commit comments

Comments
 (0)
0