数据结构课上被老师无情的pass掉了
/* father[x]表示x的父节点 */ int father[MAX]; /* rank[x]表示x的秩 */ int rank[MAX]; /* 初始化集合 */ void Make_Set(int x) { father[x] = x; rank[x] = 0; } /* 查找x元素所在的集合,回溯时压缩路径 */ int Find_Set(int x) { if (x != father[x]) { father[x] = Find_Set(father[x]); } return father[x]; } void Union(int x, int y) { x = Find_Set(x); y = Find_Set(y); if (x == y) return; if (rank[x] > rank[y]) { father[y] = x; } else { if (rank[x] == rank[y]) { rank[y]++; } father[x] = y; } } #define MAX **** //自己设置最大值 // father[x]表示x的父节点 int father[MAX]; // rank[x]表示x的秩 int rank[MAX]; // 初始化 void Make_Set(int n) { for(int i=1; i<=n; ++i) { father[i] = i; rank[i] = 0; } } // 查找 int Find_Set(int x) { if(x != father[x]) return Find_Set(father[x]); return x; } // 合并 void Union(int x, int y) { x = Find_Set(x); y = Find_Set(y); if(x == y) // x,y在同一个集合 return; if(rank[x] > rank[y]) father[y] = x; else if(rank[x] < rank[y]) father[x] = y; else { rank[y]++; father[x] = y; } }