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 yy_yy at 2010-12-19 00:59:37 on Problem 1308
#include "stdio.h"
#include "string.h"
#define M 1000
int map[M][M];
int visit[M],in_du[M];
int num=0;
bool topo()
{
	int top=0,cout=0;
	if(!num) return true;
	for(int i=1;i<=100;i++)
	{
		if(in_du[i]>1) return false;
		if(!in_du[i] && !visit[i]) cout++;
		if(cout>1) return false;                // 是森林就返回false
	}
	cout=0;
	while(top<num)
	{
		int i,j;
		for(i=1;i<=1000;i++)
			if(!in_du[i] && !visit[i]) break;
		if(i<=1000)
		{
		visit[i]=1;
		cout++;
		for(j=1;j<=1000;j++)
			if(map[i][j]) in_du[j]--;
		}
		top++;
	}
	if(cout<num) return false;
	else return true;
}
int main()
{
	int x,y;
	int cout=1;
	while(1)
	{
		bool flag=false;
		memset(map,0,sizeof(map));
		memset(in_du,0,sizeof(in_du));
		for(int j=1;j<=1000;j++)
			visit[j]=1;
		num=0;
		while(1)
		{
			scanf("%d%d",&x,&y);
			if(!x && !y) break;
			if(x==-1 && y==-1) {flag=true;break;}
			map[x][y]=1;
			if(in_du[y]==0) {num++;visit[y]=0;visit[x]=0;}
			in_du[y]++;
		}
		if(flag==true) break;
		if(topo()==true)
			printf("Case %d is a tree.\n",cout);
		else
			printf("Case %d is not a tree.\n",cout);
		cout++;
	}
	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