UVA10305 Ordering Tasks

一个很裸的拓扑排序题目,只是因为很久没有复习toposort,所以拿来复习一下,最近几天要把图论的经典算法都复习一遍。

#include  <iostream>
#include  <cstdio>
#include  <cstring>
#include  <cstdlib>
#include  <vector>
#include  <queue>
#include  <algorithm>
#include  <cmath>
using namespace std;
const int maxn=105;
bool G[maxn][maxn];
int n,m,t,c[maxn],topo[maxn];
bool dfs(int u)
{
	c[u]=-1;
	for(int v=1;v<=n;v++)
	{
		if(G[u][v])
		{
			if(c[v]<0)
				return false;
			else
			{
				if(!c[v]&&!dfs(v))
					return false;
			}
		}
	}
	c[u]=1;
	topo[t--]=u;
	return true;
}
void toposort()
{
	t=n;
	memset(c,0,sizeof(c));
	for(int u=1;u<=n;u++)
	{
		if(!c[u])
		{
			if(!dfs(u))
				return;
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m))
	{
		memset(G,false,sizeof(G));
		if(n==0&&m==0)
			break;
		int u,v;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&u,&v);
			G[u][v]=true;
		}
		toposort();
		for(int i=1;i<=n-1;i++)
			printf("%d ",topo[i]);
		printf("%d\n",topo[n]);
	}
	return 0;
}

  

时间: 2024-09-29 18:56:39

UVA10305 Ordering Tasks的相关文章

UVA10305 Ordering Tasks【DFS】【拓扑排序】

Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been

拓扑排序(Topological Order)UVa10305 Ordering Tasks

2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意一对顶点u,v满足如下条件: 若边(u,v)∈E(G),则在最终的线性序列中出现在v的前面 好了,说人话:拓扑排序的应用常常和AOV网相联系,在一个大型的工程中,某些项目不是独立于其他项目的,这意味着这种非独立的项目的完成必须依赖与其它项目的完成而完成,不妨记为u,v,则若边(u,v)∈E(G),代

UVA10305 Ordering Tasks(有向无环图排序--toposort) Kahn算法

题目描述:https://vjudge.net/problem/UVA-10305 题目分析: 恨水的题目,只要学了toposort就会做的,大概意思是给你n个变量,m个不等关系表示a<b,问n个数可能的关系;不如举个例子例如n=3表示3个变量我们假如他们是a,b,c现在有两个关系a<b,a<c 那么输出有两种a<b<c或者a<c<b(题目要求输出任意一种); 题目大概就这个意思,那么我们怎么做呢,我们想一想如果把变量看成点,关系看成有向边,那么就得到一个图,这个

UVa10305 Ordering Tasks (拓扑排序)

链接:http://acm.hust.edu.cn/vjudge/problem/19494分析: 题目中n个变量看成图中n个结点,图中的边即是二元关系,m个二元组就代表了这m条边表示的二元关系,(u,v)表示v大于u,从u向v连一条有向边,如果这m个二元组不矛盾,那么这个图便是一个DAG否则就存在一个有向环,解便不存在了,举个栗子a<b,b<c,可以推出a<c,但是现在出现了c连到了a意思是a>c,与之前的递推相悖,所以如果图中存在有向环,则不存在拓扑序,反之一个DAG图就存在拓

UVA10305 Ordering Tasks (拓扑序列)

题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大排列起来应该是什么样子的呢?例如,有四个变量a,b,c,d,若a < b, c < b, d < c, 则这四个变量的排序可能是a < d < c < b;尽管还有其他可能,你只需要找出其中一个即可. 思路: 把每个变量看成一个点,“小于”看成一个边,则得到一个有向图.这样,实际任务就是把一个图的所有节点排序,使得对应的每一条有向边(u, v),对应的u都排在v前面,即就

[SOJ] Ordering Tasks

1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input

UVA - 10305 - Ordering Tasks (拓扑排序!)

UVA - 10305 Ordering Tasks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n task

UVa 10305 - Ordering Tasks【拓扑排序】

Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input The input will consist of several instances of the problem. Each insta

Ordering Tasks From:UVA, 10305(拓扑排序)

Ordering Tasks From:UVA, 10305 Submit Time Limit: 3000 MS      Special Judge John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input The inpu