-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e44c83
commit d9c2213
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
vector<int> p(1000001, -1); | ||
|
||
int find(int x){ | ||
if(p[x] < 0) | ||
return x; | ||
return p[x] = find(p[x]); | ||
} | ||
|
||
bool uni(int u, int v){ | ||
u = find(u); | ||
v = find(v); | ||
if(u == v) | ||
return false; | ||
if(p[v] < p[u]) // v의 랭크가 더 큰 경우 | ||
swap(u, v); // u, v를 swap | ||
// 위의 if문으로 인해 u의 랭크 >= v의 랭크이다 | ||
if(p[u] == p[v]) // 랭크가 같은 경우에 대한 처리 | ||
p[u]--; | ||
p[v] = u; // v를 u의 자식으로 만든다 | ||
return true; | ||
} |