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

用幷查集做的 可为什么这么慢呢?请大牛指教!

Posted by xiaotianwei at 2010-03-19 14:31:20 on Problem 2492
#include<iostream>
#include<cstdio>
using namespace std;
const int lmax=2001;
struct B
{
	int parent;
	int relation;
};
B bugs[lmax];
int find(int x)
{
	int t=bugs[x].parent;
	if(t!=x)
	{  
		bugs[x].parent=find(t);
		bugs[x].relation=(bugs[x].relation+bugs[t].relation)%2;
		
	}           
	return  bugs[x].parent;
}
void makeset()
{
	for(int i=1;i<=2000;i++)
	{
		bugs[i].parent=i;
		bugs[i].relation=0;
	}
}
int main()
{
	int t,n,m,x,y,xr,yr,flag;
	scanf("%d",&t);
	int h=0;
	while(t--)
	{
		makeset();flag=1;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			xr=find(x);
			yr=find(y);
			if(flag==1)
			{
			    if(xr==yr)
			    {
			    	if(bugs[x].relation==bugs[y].relation)
			    	{
			    	   flag=0;
			    	}
			    }
			    else 
			    {
				  bugs[xr].parent=yr;
				  bugs[xr].relation=(bugs[x].relation+bugs[y].relation+1)%2;
			    }
			}
		}
		if(!flag)
		{
		     printf("Scenario #%d:\nSuspicious bugs found!\n\n",++h);
		}
		else  printf("Scenario #%d:\nNo suspicious bugs found!\n\n",++h);
	}
	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