-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunionfind_temp.cpp
57 lines (45 loc) · 1.22 KB
/
unionfind_temp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//2개의 노드씩 이어나가면서 두 노드가 같은 그래프에 있는지 판단할 때
#include <bits/stdc++.h>
#define endl "\n"
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<int,int,int> tp;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector <ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef unordered_map<int, int> mpii;
int n,m, c,a,b;
int parent[1000001];
int find(int x){
if(parent[x] == x) return x;//루트노드 반환
int p = find(parent[x]);
parent[x] = p; //부모노드로 갱신
return p;
}
void merge(int x, int y){
x = find(x);
y = find(y);
// x == y 라면 사이클이 형성된 것
if(x != y) parent[x] = y;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m;
for(int i=0; i<=n; i++) parent[i]= i;
while(m--){
cin >> c >> a >> b;
if(c==0){
merge(a,b);
}
else{ // x==1
if(find(a) == find(b)) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}//1717