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。谢谢啦#include <iostream> #include <stdlib.h> #define MAX 100001 using namespace std; int father[MAX],opt[MAX]; //opt表示的应该是i的对立面元素,而不是i所在集合的对立面集合 int getSet(int pos){//pos所在的集合 if(pos!=father[pos]){ father[pos] = getSet(father[pos]); } return father[pos]; } int main(int argc, char *argv[]) { int t,n,m; int i,j,k,ele1,ele2,set1,set2; char cmd; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(i=0;i<=n;i++){ father[i] = i; } memset(opt,0,sizeof(opt)); for(i=1;i<=m;i++){ getchar(); scanf("%c%d%d",&cmd,&ele1,&ele2); set1 = getSet(ele1);//ele1所在的集合 set2 = getSet(ele2);//ele2所在的集合 if(cmd=='A'){ if(set1 == set2) printf("In the same gang.\n"); else if(set1==getSet(opt[ele2])||set2==getSet(opt[ele1])) printf("In different gangs.\n"); else printf("Not sure yet.\n"); } else if(cmd=='D'&&set1!=set2){ if(opt[ele1]==0&&opt[ele2]==0){ opt[ele1] = ele2; opt[ele2] = ele1; continue; } else if(opt[ele1]==0){//已知ele2的一个对立点 opt[ele1] = set2;//ele1的对立点就是ele2所在的集合的根节点 father[ele1] = getSet(opt[ele2]);//ele1的父节点就是ele2的对立点所在的根节点 opt[ele2] = father[ele1]; continue; } else if(opt[ele2]==0){//已知ele1的一个对立点 opt[ele2] = set1; father[ele2] = getSet(opt[ele1]); opt[ele1] = father[ele2]; continue; } father[ele1] = getSet(opt[ele2]); father[ele2] = getSet(opt[ele1]); opt[ele1] = father[ele2]; opt[ele2] = father[ele1]; } } } system("PAUSE"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator