Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
请问怎么用并查集优化?(附我的并查集代码)int FindSet(int x) // 找集合 { while(P[x] != x) x = P[x]; return x; } bool FindPath(int x, int tar) // 找x到根的路径是否有tar, 说明tar是x的祖先,这个在后面用来找矛盾 { while(P[x] != x) { x = P[x]; if(x == tar) return true; } return false; } void Link(int x, int y) // 合并两个集合 { if(P[x] > P[y]) { P[y] = P[x]; rank[x] += rank[y]; } else { if(P[x] != P[y]) rank[y] += rank[x]; P[x] = P[y]; } return; } // 下面这个把右边集合合并到左边,并且预先检查右边的是否有可能是x的祖先(如果是,就冲突了) bool LeftJoin(int x, int y) // join the right set to the left one, check if conflicts { if(FindPath(x, y) == true) // no need to do anything any more return false; Link(FindSet(x), FindSet(y)); return true; } 我加上了这个,但是wa了,青椒高手 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator