题意:

经典的拓扑排序。有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列。

分析:

拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系。我们将“小于”这种关系看做一条有向边,如果得到的图是有向无环图DAG(Directed Acyclic Graph),则是存在拓扑排序的,如果存在有向环,则不存在拓扑排序。

注意输入数据里面m可能等于0的情况,就因为这个WA了两次。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
int G[maxn][maxn], c[maxn];
int topo[maxn], t;
int n, m; bool dfs(int u)
{
c[u] = -;
for(int v = ; v <= n; ++v) if(G[u][v])
{
if(c[v] < ) return false; //´æÔÚ»·
else if(!c[v] && !dfs(v)) return false;
}
c[u] = ; topo[--t] = u;
return true;
} bool toposort()
{
t = n;
memset(c, , sizeof(c));
for(int u = ; u <= n; ++u) if(!c[u])
if(!dfs(u)) return false;
return true;
} int main(void)
{
#ifdef LOCAL
freopen("10305in.txt", "r", stdin);
#endif while(scanf("%d%d", &n, &m) == && n)
{
memset(G, , sizeof(G));
int a, b;
for(int i = ; i < m; ++i)
{
scanf("%d%d", &a, &b);
G[a][b] = ;
}
toposort();
for(int i = ; i < n-; ++i)
printf("%d ", topo[i]);
printf("%d\n", topo[n-]);
} return ;
}

代码君

UVa 10305 (拓扑排序) Ordering Tasks的更多相关文章

  1. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

  2. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  3. Uva 10305 拓扑排序

    题意: 给定n个点,与m条边, 给出他们的拓扑排序. 分析: 拓扑排序可以有两种做法, 第一种是dfs, 每次都找到某一个点的终点, 然后加入序列末尾, 正在访问的标记为-1, 访问过的标记为1, 未 ...

  4. uva 10305 拓扑排序裸题

    https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...

  5. uva 1423 拓扑排序

    刘书上例题  拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...

  6. UVa 1572 (拓扑排序) Self-Assembly

    题意: 有n种正放形,每种正方形的数量可视为无限多.已知边与边之间的结合规则,而且正方形可以任意旋转和反转,问这n中正方形是否可以拼成无限大的图案. 分析: 首先因为可以旋转和反转,所以可以保证在拼接 ...

  7. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  8. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

随机推荐

  1. 生鲜电商的O2O之道

  2. windows下编译ffmpeg

    windows 编译ffmpeg 搞过很多次,每次总是磕磕碰碰,从头到尾不能一直顺利,浪费一些时间.终究起原因,都是当时记得,过段时间就忘了.好记性不如烂笔头,大好周末晚上,闲暇无事,记录最近一次编译 ...

  3. asp.net asp:TextBox控件绑定值后,获取不到新值问题解决方法

    把Page_Load里绑定的代码放在    if(!IsPostBack){}里面后,即可获取到更新的值. 意思为第一次加载执行.

  4. 2437: [Noi2011]兔兔与蛋蛋 - BZOJ

    Description Input 输入的第一行包含两个正整数 n.m.接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母" ...

  5. BZOJ 3436: 小K的农场 差分约束

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c  ->  b<=a-c ...

  6. Win 7怎样拒绝所有可移动存储设备的所有权限

    在Windows 7中,我们可拒绝对任何可移动存储类的权限.下面让我来教大家怎样在组策略中启用“所有可移动存储类:拒绝所有权限”策略,具体操作步骤如下所述: 步骤/方法 在开始搜索框中键入“gpedi ...

  7. 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型

    this.BeginInvoke(() => { this.btnQuery.Enabled = false; //禁用查询 }); 跨线程调用时,编译上面的代码将提示 对于Control.In ...

  8. 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮

    谷歌用一个变了身的古老「穷举算法」,披上「神经网络」的画皮,假装「跨时代」的黑科技,忽悠广大「膜拜者」,「狮仙」我实在看不下去了,来揭一揭这只幺蛾子小狗的画皮. 本期是移动开发精英俱乐部的第10期推荐 ...

  9. timeit统计运行时间

    import timeitt1 = timeit.timeit('sum(x*x for x in xrange(10000))',number = 10000) print t1

  10. [转载]Spring Java Based Configuration

    @Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...