标签:color tmp cout name stream return root int class
2017-07-28 17:27:41
writer:pprp
有两种优化方法:
题目:亲戚
代码如下:
#include <iostream> using namespace std; int N,M,Q; int parent[1000]; int Find(int x) //判断x所属集合 { int root = x; while(parent[root]>=0) //找到根节点 root = parent[root]; while(root!=x) //压缩路径,将该点直接 { int tmp = parent[x]; parent[x]= root; x = tmp; } return root; } void Merge(int a,int b) //较小的树成为较大的树的子树 { int pa = Find(a); int pb = Find(b); if(pb < pa) swap(pb,pa); if(pa!=pb) parent[pb] = pa; } int main() { int i,a,b; cin >> N >> M >> Q; for(i=1; i<=N; i++) { parent[i] = -1; } for(i=1; i<=M; i++) { cin >> a >> b; if(Find(a)!=Find(b)) Merge(a,b); } for(i=1; i<=Q; i++) { cin >> a >> b; if(Find(a)==Find(b)) cout <<"Yes"<<endl; else cout <<"No"<<endl; } return 0; }
标签:color tmp cout name stream return root int class
原文地址:http://www.cnblogs.com/ilovelianghui/p/7252339.html