poj1182是此题的加强版。若两人属于不同集合则他们所属帮派未知。若属于同一集合,设ra=find(a),若rank[a]==0,则a与ra为同一帮派,反之rank[a]==1,a与ra则分属于不同帮派。另外测下这组数据 1 2 1 A 1 2 答案应该是In different gangs.(在discuss里看到的)。 1 #include<cstdio> 2 #include<string.h> 3 #include<iostream> 4 using namespace std; 5 const int maxn=100001; 6 int p[m Read More
posted @ 2013-06-10 19:43 longlongago Views(110) Comments(0) Diggs(0) Edit
并查集的基础题。在加入边前检查两点之前是否是同一个集合,若是则不是一棵树。输入完后检查是否所有点属于同一个集合。 1 #include<cstdio> 2 #include<string.h> 3 #include<iostream> 4 using namespace std; 5 const int maxn=1<<15; 6 bool flag,vis[maxn]; 7 int p[maxn],rank[maxn],data[maxn],m; 8 int a,b,ra,rb; 9 10 int find(int x)11 {12 if(x! Read More
posted @ 2013-06-10 18:38 longlongago Views(99) Comments(0) Diggs(0) Edit