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

自己画了n个树写进去都过了,就是wa,大佬们帮忙看一下呗

Posted by 1109249095 at 2017-10-15 21:10:57 on Problem 1703
#include<cstdio>
#include<cstring>
using namespace std;
const int max=100000+50;
int pa[max],gp[max];

void init(int n){
   for(int i=1;i<=n;i++){
   	pa[i]=i;gp[i]=-1;
   }
}

int find(int n){
	if(n!=pa[n])
		pa[n]=find(pa[n]);
	gp[n]=(gp[n]+gp[pa[n]])%2;
	return pa[n];
}

void unite(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(gp[x]==-1) gp[x]=0;
	if(gp[y]==-1) gp[y]=0;
	pa[y]=fx;
	gp[y]=(gp[x]-gp[y]+2+1)%2;
}

int main(){
	int n,a,b;
	char m;
	scanf("%d",&n);
	while(n--){
		scanf("%d%d",&a,&b);getchar();
		init(a);
		int x,y;
		for(int j=0;j<b;j++){
			scanf("%c%d%d",&m,&x,&y); getchar(); 
			if(m=='A'&&a==2){
				printf("In different gangs.\n");
				continue;
			}
			if(m=='D')
				unite(x,y);
			else if(m=='A'){
				if(gp[x]==-1||gp[y]==-1) printf("Not sure yet.\n");
				else {
					if(gp[x]==gp[y]&&find(x)==find(y)) printf("In the same gang.\n");
				    else if(find(x)==find(y)&&gp[x]!=gp[y])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