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

为何一直RE 求大侠指点 (并查集+离散)

Posted by slp at 2011-03-31 16:51:02 on Problem 1733
#include <stdio.h>
#include <string.h>

const int maxn=1000100;
const int  mod=999997;

int set[maxn],val[maxn],rank[maxn];
int head[mod],top,index;
struct nodes
{
	int num,next,id;
}node[maxn];

int find(int a)
{
	int pa=set[a];
	if(set[a]==a) return a;
	set[a]=find(set[a]);
	val[a]=val[pa]^val[a];
	return set[a];
}

void Union(int a,int b,int v)
{
	int fa,fb;
	fa=find(a);
	fb=find(b);
	if(fa==fb) return;
	if(rank[fa]>=rank[fb])
	{
		if(rank[fa]==rank[fb])
			rank[fa]++;
		set[fb]=set[fa];
		val[fb]=(val[a]+val[b]+v)%2;
	}else{
		set[fa]=set[fb];
		val[fa]=val[a]^val[b]^v;
	}
}

void init()
{
	for(int i=0;i<maxn;i++)
	{
		set[i]=i;
		rank[i]=0;
		val[i]=0;
		head[i]=0;
	}
	top=1;
	index=1;
}

int getid(int a)
{
	return a;
	int i,key=a%mod;
	for(i=head[key];i;i=node[i].next)
	{
		if(node[i].num==a) return node[i].id;
	}
	node[++top].next=head[key];
	node[top].id=++index;
	node[top].num=a;
	head[key]=top;
	return index;
}

int main()
{
	int a,b,i,t=0,ha,hb,v,n,flag=0;
	char op[10];
	
	freopen("d:\\1.txt","r",stdin);

	init();
	scanf("%d%d",&a,&n);
	for(i=0;i<n;i++)
	{
		scanf("%d%d%s",&a,&b,op);
		//if(flag) continue;
		if(op[0]=='o') v=1;
		else v=0;
		ha=getid(a-1);
		hb=getid(b);
		//if(top>maxn) while(1);
		if(find(ha)!=find(hb)){
			Union(ha,hb,v);
		}
		else if((val[ha]^val[hb])!=v) break;
	    t++;
	}
	printf("%d\n",t);
	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