Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

郁闷的WA又找不到错,贴代码...

Posted by whosyourdaddy at 2010-06-20 12:14:54 on Problem 1703
并查集是看和父亲的关系,一致是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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator