File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ // http://boj.kr/bf81d062967a4ba2993ba38a135b2bab
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 ;
25+ }
26+
27+ int main (){
28+ ios::sync_with_stdio (0 );
29+ cin.tie (0 );
30+
31+ int n, m;
32+ cin >> n >> m;
33+ while (m--){
34+ int q, a, b;
35+ cin >> q >> a >> b;
36+ if (q == 0 )
37+ uni (a, b);
38+ else {
39+ if (find (a) == find (b))
40+ cout << " YES\n " ;
41+ else
42+ cout << " NO\n " ;
43+ }
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments