标签:nbsp bsp roo code class init div log void
非递归
void init() { for(int i = 1;i <= n;i++) pre[i] = i; } int findd(int x) { int root = x; while(pre[root] != root) root = pre[root]; int i = x,j; while(pre[i] != root) { j = pre[i]; pre[i] = root; i = j; } return root; } void join(int a,int b) { int x = findd(a),y = findd(b); if(x != y) pre[x] = y; }
递归
int findd(int x) { if (x != pre[x]) { return pre[x] = findd(pre[x]); } return x; }
标签:nbsp bsp roo code class init div log void
原文地址:http://www.cnblogs.com/zhurb/p/6064240.html