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 tengshengbo at 2005-08-09 17:17:15 on Problem 2492
In Reply To:为什么这么慢? Posted by:bakey at 2005-08-09 16:25:32
> 染色那种做法.不会写并查集,会的人愿意指导一下吗?
> #include <stdio.h>
> #include <string.h>
> const int maxn = 2000+5;
> int n,m,mat[maxn][maxn],flag[maxn];
> bool ok;
> 
> void paint(int a)
> {
> 	int i;
> 	for (i=1;i<=n;i++)
> 	{
> 		if (mat[a][i]==1)
> 		{
> 			if (flag[i]==0)
> 			{
> 				if (flag[a]==1){
> 					flag[i]=2;
> 					paint(i);
> 				}
> 				else {
> 					flag[i]=1;
> 					paint(i);
> 				}
> 			}
> 			else
> 			{
> 				if (flag[a]==flag[i]) {
> 					 ok = false;
> 					 return;
> 				}
> 				else continue;
> 			}
> 		}
> 	}
> }
> 
> int main()
> {
> 	int cases,a,b,c = 0,i;
> 
> 	scanf("%d",&cases);
> 	while (cases --)
> 	{
> 		c ++;
> 		scanf("%d %d",&n,&m);
> 		memset(mat,0,sizeof(mat));
> 		for (i = 0 ; i < m;i ++)
> 		{
> 			scanf("%d %d",&a,&b);
> 			mat[a][b]=mat[b][a]=1;
> 		}
> 		memset(flag,0,sizeof(flag));
> 		ok = true;
> 		for (i=0;i<=n;i++)
> 		{
> 			if (flag[i]==0)
> 			{
> 				flag[i]=1;
> 				paint(i);
> 			}
> 		}
> 		printf("Scenario #%d:\n",c);
> 		if (ok)
> 			printf("No suspicious bugs found!\n\n");
> 		else
> 			printf("Suspicious bugs found!\n\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