| ||||||||||
| 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 | |||||||||
郁闷的WA又找不到错,贴代码...并查集是看和父亲的关系,一致是0,不一致是1。
求大牛看看可能是哪儿的问题...
#include <cstdio>
#include <memory>
using namespace std;
class node{
public:
int parent;
int idtoP;
} nod[100005];
int findp(int a)
{
int a1=a;
int a1a=nod[a1].idtoP;
while(nod[a1].parent!=0){
a1=nod[a1].parent;
if(a1a==0)
a1a=nod[a1].idtoP;
else
a1a=1-nod[a1].idtoP;
}
if(a1!=a){
nod[a].parent=a1;
nod[a].idtoP=a1a;
}
return a1;
}
void uni(int a, int b)
{
int a1=findp(a);
int b1=findp(b);
if(a1==b1)
return;
nod[a1].parent=b1;
nod[a1].idtoP=1;
}
bool find(int a, int b, bool& same)
{
int a1=findp(a);
int b1=findp(b);
if(a1!=b1)
return false;
int a1a=nod[a].idtoP;
int b1b=nod[b].idtoP;
if(a1a==b1b){
same=true;
return true;
}else{
same=false;
return true;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d %d",&n,&m);
memset(nod,0,sizeof(nod));
while(m--){
char in[10];
int a,b;
scanf("%s %d %d",&in,&a,&b);
if(in[0]=='D')
uni(a,b);
else{
bool iden;
if(find(a,b,iden))
if(iden==true)
printf("In the same gang.\n");
else
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator