File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
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 ;
8000
div>
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
+ }
You can’t perform that action at this time.
0 commit comments