拓扑排序--UVa10305
题目
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 executed.
Input
The input will consist of several instances of the problem. Each instance begins with a line containing two integers, 1 <= n <= 100 and m. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. An instance with n = m = 0 will finish the input.
Output
For each instance, print a line with n integers representing the tasks in a possible order of execution.
Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3 (不好意思, 忘了这道题目啦, 现在给出思路和题解吧!) 其实这个问题就是裸裸的拓扑排序, 首先解释一下什么是拓扑排序? 就是有些事, 而你要完成这件事前, 就必须要完成另一件事。 就像你
如果想要一个孩子, 就必须要先有个女票一样(结不结婚不是问题, 呵呵!)。 而要有女票, 你就要先谈场恋爱, ,,,,好啦! 这下拓扑排序解释清楚啦! 重要性也显现出来啦! 它可是能关系
到终身大事的算法啊!(嘿嘿!)。
排序方法: 把每个变量看成一个点, “小于”关系看成有向边, 则得到一个有向图。 这样, 我们实际上只需把这个图的所有节点排序。 使得每一条有向边(u,v)对应的u都在v的前面。 在图论中
这个问题称为拓扑排序。
很明显, 若在图中存在有向环, 则不存在拓扑排序。 可以借助DFS完成拓扑排序。 在访问完一个结点以后把它加到当前拓扑序的尾部。 (聪明的你想一下, 为什么不是首部)。 机智的我告诉你,试想
最后一件事, 一定是在最后做的, 然后向前滚, (刚好是DFS的逆序, 哈哈! 太巧妙啦)。
#include<cstdio>
#include<cstring>
const int maxn = ; int n, m, G[maxn][maxn], c[maxn], topo[maxn], t; 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);
}
c[u] = ; topo[--t] = u;//DFS向根追溯。
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()
{
while(scanf("%d%d", &n, &m)==&&n)
{
memset(G, , sizeof(G));//标记有向线段。
for(int i=; i<m; i++)
{
int u, v;
scanf("%d%d", &u, &v); u--; v--;
G[u][v] = ;
}
if(toposort())
{
for(int i=; i<n-; i++)
printf("%d ", topo[i]+);
printf("%d\n", topo[n-]+);
}
else
printf("No\n");
}
return ;
} //代码解释: c[u]=0表示从没访问过,c[u] = 1 表示已经访问过啦。
//c[u] = -1表示正在访问(即递归调用dfs(u)正在栈桢中, 尚未返回)。
《2》来道模板题吧! (其实还是有一点要注意的, 嘿嘿!)。
http://acm.hdu.edu.cn/showproblem.php?pid=4324
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; const int maxn = +; int n, c[maxn];
char G[maxn][maxn]; 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] = ;
return true;
} bool toposort()
{
memset(c, , sizeof(c));
for(int u=; u<n; u++)if(!c[u])
if(!dfs(u)) return false;
return true;
}
int main()
{
int T;
scanf("%d", &T);
for(int i=; i<=T; i++)
{
scanf("%d", &n);
for(int j = ; j<n; j++)
scanf("%s", G[j]);
printf("Case #%d: %s\n", i, toposort() ? "No" : "Yes"); }
return ;
}
有疑问,请细读代码, DFS递归真的很,,,(哈哈!)。
拓扑排序--UVa10305的更多相关文章
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA10305 拓扑排序
网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...
- UVA-10305 Ordering Tasks (拓扑排序)
题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- UVA10305:Ordering Tasks(拓扑排序)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Ordering Tasks UVA - 10305(拓扑排序)
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...
- noip复习之拓扑排序
之前很多很多紫书上的东西我都忘了…… 抄题解的后果…… 做了一下裸题 https://vjudge.net/problem/UVA-10305 拓扑排序还可以来判环 #include<bits/ ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
随机推荐
- ERR: Call to undefined function openssl_random_pseudo_bytes()
最近使用TP5/PHP7,总是出现ERR: Call to undefined function index\index\openssl_random_pseudo_bytes(),才发现是php没有 ...
- redis make test报错 Test replication partial resync: ok psync
更改 tests/integration/replication-psync.tcl 文件: vi tests/integration/replication-psync.tcl 把对应报错的那段代码 ...
- Environment中针对的读写权限判断
Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态,并且需要在AndroidManifes ...
- NSDictionary to jsonString || 对象转json格式
-(NSString*)DataTOjsonString:(id)object { NSString *jsonString = nil; NSError *error; NSData *jsonDa ...
- python 拷贝文件夹下所有的文件到指定文件夹(不包括目录)
1.随便简单些写了一下.直接粘结代码,只是简单的实现一下,还很多需要完善和扩展的地方,比如忽略掉后缀文件,删除文件 如果排除的某些的话可以用: sourceF.find('.后缀')>0 2.注 ...
- 只使用处理I/O的PrintDigit函数,编写一个过程以输出任意实数
#include <stdio.h> #include <stdlib.h> #include <math.h> int printDigit(int a) { p ...
- Excel 函数
Excel 函数: 一.定义: Excel 函数即是预先定义,执行计算.分析等处理数据任务的特殊公式. 二.结构: 1.单一结构 =函数名(参数1,参数2,参数3.....) 示例:=sum(A3:A ...
- windows tomcat配置大全
Tomcat下JSP.Servlet和JavaBean环境的配置 第一步:下载j2sdk和tomcat:到sun官方站点()下载j2sdk,注意下载版本为Windows Offline Install ...
- andriod 新建 Activity_ Form (详细设置)
参考: Starting Another Activity 去创建Activity New->Other->Android->Android Activity->BlankAc ...
- 如何在 Eclipse 中连接源码
1:首先在window 中 打开首选项(preferences) 找到如下java -- 已安装的JRE