|
1 |
| -#pragma GCC optimize ("O3") |
2 |
| -#include <bits/stdc++.h> |
| 1 | +#include <cstdio> |
| 2 | +#include <numeric> |
3 | 3 |
|
4 | 4 | using namespace std;
|
5 |
| -const int N = (1<<17) , M = (1<<14), OO = 0x3f3f3f3f; |
| 5 | +const int N = 1e5+5 , M = (1<<14), OO = 0x3f3f3f3f; |
6 | 6 |
|
7 | 7 | int n, q;
|
8 | 8 | int t, a, b;
|
9 | 9 |
|
10 | 10 | int parent[N];
|
| 11 | +inline void init(){ |
| 12 | + iota(parent, parent+N, 0); |
| 13 | +} |
11 | 14 |
|
12 | 15 | int findParent(int x){
|
13 | 16 | if(parent[x] == x) return x;
|
14 | 17 | return parent[x] = findParent(parent[x]);
|
15 | 18 | }
|
16 | 19 |
|
17 |
| -bool sameSet(int a, int b){ |
| 20 | +inline bool sameSet(int a, int b){ |
18 | 21 | return findParent(a) == findParent(b);
|
19 | 22 | }
|
20 | 23 |
|
21 |
| -void unionSet(int a, int b){ |
22 |
| - int pa = findParent(a), pb = findParent(b); |
23 |
| - if(pa == pb) return; |
24 |
| - parent[pb] = pa; |
| 24 | +inline void merge(int a, int b){ |
| 25 | + a = findParent(a), b = findParent(b); |
| 26 | + if(a == b) return; |
| 27 | + parent[b] = a; |
25 | 28 | }
|
26 | 29 |
|
27 |
| -void init(){ |
28 |
| - for(int i = 0 ; i <= n ; ++i) parent[i] = i; |
29 |
| -} |
30 | 30 |
|
31 | 31 | int main(){
|
32 |
| - // freopen("i.in", "rt", stdin); |
33 |
| - // freopen("o.out", "wt", stdout); |
34 | 32 | scanf("%d %d", &n, &q);
|
35 | 33 | init();
|
36 | 34 | while(q--){
|
37 | 35 | scanf("%d %d %d", &t, &a, &b);
|
38 | 36 | if(t){ //Make Friends
|
39 |
| - unionSet(a, b); |
| 37 | + merge(a, b); |
40 | 38 | }else{ //Are Friends?
|
41 | 39 | printf("%d\n", sameSet(a, b));
|
42 | 40 | }
|
43 | 41 | }
|
44 | 42 | return 0;
|
45 | 43 | }
|
46 |
| - |
47 |
| - |
48 |
| - |
49 |
| - |
50 |
| - |
51 |
| - |
52 |
| - |
53 |
| - |
0 commit comments