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:可以的,链表存下所有插入记录,遇到删除就重新插入一遍,可达到500MS左右,应该比构图快

Posted by iscubus at 2011-10-15 22:31:45 on Problem 2838
In Reply To:可以的,链表存下所有插入记录,遇到删除就重新插入一遍,可达到500MS左右,应该比构图快 Posted by:iscubus at 2011-10-15 22:12:43
# include <stdio.h>
# include <string.h>

int table[20002][2],next[20002],en,head;
int n,m;
int father[1005],rank[1005];

int find(int x)
{
	if(father[x]==-1)	return x;
	return father[x]=find(father[x]);
}

int main()
{
	char c;int a,b,x,y,p,q;
	//freopen("in","r",stdin);
	memset(father,-1,sizeof(father));
	memset(next,-1,sizeof(next));
	memset(rank,0,sizeof(rank));
	en=0;
	scanf("%d%d\n",&n,&m);
	while(m--)
	{
		c=getchar();
		scanf("%d %d\n",&a,&b);
		if(c=='Q')
		{
			if(find(a)==find(b))
				printf("Y\n");
			else	printf("N\n");
		}else if(c=='I')
		{
			table[++en][0]=a;
			table[en][1]=b;
			next[en]=next[0];
			next[0]=en;
			x=find(a);y=find(b);
			if(x!=y)
			{
				if(rank[x]>rank[y])
					father[y]=x;
				else
				{
					father[x]=y;
					if(rank[x]==rank[y])	rank[y]++;
				}
			}
		}else if(c=='D')
		{
			memset(father,-1,sizeof(father));
			memset(rank,0,sizeof(rank));
			p=next[0];q=0;
			while(p!=-1)
			{
				x=table[p][0];
				y=table[p][1];
				if(x==a&&y==b||x==b&&y==a)
				{
					next[q]=next[p];
				}else
				{
					x=find(x);y=find(y);
					if(x!=y)
					{
						if(rank[x]>rank[y])
							father[y]=x;
						else
						{
							father[x]=y;
							if(rank[x]==rank[y])
								rank[y]++;
						}
					}
				}
				q=p;
				p=next[p];
			}

		}
	}
	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