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
|
#include <iostream> #include <cstdlib> #include <cstring>
#define MAXN 1000 using namespace std;
int father[MAXN]; int rank[MAXN];
void () { int i; for(i = 0; i < MAXN; i++){ father[i] = i; rank[i] = 0; } }
int Find(int x){ if(x != father[x]) father[x] = Find(father[x]); return father[x]; }
void Union(int x, int y){ int root_x = Find(x); int root_y = Find(y); if(root_x != root_y){ if(rank[root_x] > rank[root_y]) father[root_y] = root_x; else if(rank[root_x] < rank[root_y]) father[root_x] = root_y; else{ father[root_x] = root_y; rank[root_y]++; } } }
|
近期评论