File tree Expand file tree Collapse file tree 1 file changed +39
-3
lines changed Expand file tree Collapse file tree 1 file changed +39
-3
lines changed Original file line number Diff line number Diff line change 11// Authored by : BaaaaaaaaaaarkingDog
22// Co-authored by : -
3- // http://boj.kr/****************
3+ // http://boj.kr/bf81d062967a4ba2993ba38a135b2bab
44#include < bits/stdc++.h>
55using namespace std ;
66
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 (){
830 ios::sync_with_stdio (0 );
931 cin.tie (0 );
1032
11- }
33+ int n, m;
34+ cin >> n >> m;
35+ while (m--){
36+ int q, a, b;
37+ cin >> q >> a >> b;
38+ if (q == 0 )
39+ uni (a, b);
40+ else {
41+ if (find (a) == find (b))
42+ cout << " YES\n " ;
43+ else
44+ cout << " NO\n " ;
45+ }
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments