File tree Expand file tree Collapse file tree 1 file changed +47
-3
lines changed Expand file tree Collapse file tree 1 file changed +47
-3
lines changed Original file line number Diff line number Diff line change 1
1
// Authored by : BaaaaaaaaaaarkingDog
2
2
// Co-authored by : -
3
- // http://boj.kr/****************
3
+ // http://boj.kr/48d21e5cb37f46978ecca75d63feebb0
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ vector<int > p (1000001 , -1 );
8
+
9
+ int find (int x){
10
+ if (p[x] < 0 )
11
+ return x;
12
+ return p[x] = find (p[x]);
13
+ }
14
+
15
+ bool uni (int u, int v){
16
+ u = find (u);
17
+ v = find (v);
18
+ if (u == v)
19
+ return false ;
20
+ if (p[v] < p[u]) // v의 랭크가 더 큰 경우
21
+ swap (u, v); // u, v를 swap
22
+ // 위의 if문으로 인해 u의 랭크 >= v의 랭크이다
23
+ if (p[u] == p[v]) // 랭크가 같은 경우에 대한 처리
24
+ p[u]--;
25
+ p[v] = u; // v를 u의 자식으로 만든다
26
+ return true ;
27
+ }
28
+
29
+ int main (){
8
30
ios::sync_with_stdio (0 );
9
31
cin.tie (0 );
32
+ int t;
33
+ cin >> t;
10
34
11
- }
35
+ for (int tc = 1 ; tc <= t; tc++){
36
+ cout << " Scenario " << tc << " :\n " ;
37
+ int n, k;
38
+ cin >> n >> k;
39
+ fill (p.begin () + 1 , p.begin () + n + 1 , -1 );
40
+ while (k--){
41
+ int a, b;
42
+ cin >> a >> b;
43
+ uni (a, b);
44
+ }
45
+
46
+ int m;
47
+ cin >> m;
48
+ while (m--){
49
+ int u, v;
50
+ cin >> u >> v;
51
+ cout << (find (u) == find (v)) << ' \n ' ;
52
+ }
53
+ cout << ' \n ' ;
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments