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

拓扑排序第一题,顺便也熟悉了一下图的储存,一次0ms AC

Posted by su03121231 at 2013-07-27 20:50:27 on Problem 2367
//poj2367
//拓扑排序+图的储存
#include<cstdio>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
int vis[105],can[105];
int main()
{
	int n,i,num,a;
	int s[105];
	while(scanf("%d",&n)!=EOF)
	{
		vector<int> g[105];
		queue<int> q;
		num=-1;
		memset(vis,0,sizeof(vis));
		memset(can,0,sizeof(can));
		for(i=1;i<=n;i++)
		{
			while(scanf("%d",&a),a)
			{
				g[i].push_back(a);
				vis[a]++;
			}
		}
		for(i=1;i<=n;i++)
		{
			if(!vis[i])
			{
				q.push(i);
				break;
			}
		}
		while(!q.empty())
		{
			s[++num]=q.front();
			can[s[num]]=1;
			q.pop();
			for(i=0;i<g[s[num]].size();i++)
				vis[g[s[num]][i]]--;
			for(i=1;i<=n;i++)
			{
				if(!vis[i] && !can[i])
				{
					q.push(i);
					break;
				}
			}
		}
		for(i=0;i<=num;i++)
			printf("%d ",s[i]);
		printf("\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