标签:
用并查集查找根节点,包括三种方法:
int find(int x) { int r = x; while(father[r] != r) r = father[r]; return r; }
int find(int x) { if(father[x] != x) father[x] = find(father[x]); return father[x]; }
int find(int x) { int i, j, r = x; while(father[r] != r) r = father[r]; i = x; while(i != r) { j = father[i]; father[i] = r; i = j; } return r; }
标签:
原文地址:http://www.cnblogs.com/1203ljh/p/4641656.html