【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。
题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序。
题解:拓扑排序版题 dfs到底再压入栈。
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<sstream>
using namespace std;
const int maxn = + ;
int G[maxn][maxn];
int c[maxn];
list<int>topo;
int n, m;
bool dfs(int u) {
c[u] = -;
for (int v = ; v <= n; v++)if(G[u][v]) {
if (c[v] == -)return false;//正在dfs栈中
else if (!c[v] && !dfs(v))return false;//若未dfs,就dfs一遍.
}
c[u] = ; topo.push_front(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() { while (cin >> n >> m)
{
if (n == m&&n == )break;
memset(G, , sizeof(G));
topo.clear();
for (int i = ; i < m; i++) {
int a, b;
cin >> a >> b;
G[a][b] = ;
}
if (toposort()) {
cout << topo.front();
topo.pop_front();
for (auto t : topo) cout << ' '<< t ;
cout << endl;
}
} system("pause");
return ;
}
附:不判环的ac代码(便于理解)
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
const int maxn = + ;
vector<int> G[maxn];
int c[maxn];
stack<int>topo;
int n, m;
void dfs(int u) {
c[u] = ;
for (int j = ; j <G[u].size(); j++)
{
int v = G[u][j];
if (!c[v])dfs(v);
}
c[u] = ; topo.push(u); }
void toposort() {
memset(c, , sizeof(c));
for (int u = ; u <= n; u++)if(c[u]==) {dfs(u);}
}
int main() { while (cin >> n >> m)
{
if (n == m&&n == )break;
for (int i = ; i <= n; i++)G[i].clear();
while (!topo.empty()) topo.pop();
for (int i = ; i < m; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
}
toposort();
cout << topo.top(); topo.pop();
while (!topo.empty()) {
cout << ' ' << topo.top(); topo.pop();
}
cout << endl;
} system("pause");
return ;
}
【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。的更多相关文章
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
- UVa 10305 (拓扑排序) Ordering Tasks
题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...
- Ordering Tasks UVA - 10305(拓扑排序)
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...
- Uva 10305 拓扑排序
题意: 给定n个点,与m条边, 给出他们的拓扑排序. 分析: 拓扑排序可以有两种做法, 第一种是dfs, 每次都找到某一个点的终点, 然后加入序列末尾, 正在访问的标记为-1, 访问过的标记为1, 未 ...
- 拓扑排序 (Ordering Tasks UVA - 10305)
题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...
- uva 10305 拓扑排序裸题
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
随机推荐
- EF+LINQ事物处理
在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作同一条数据的同一个字段的话, ...
- html中<a>标签的种类
在html中a 标签是一个链接标签,然而a 标签也有非常多的种类,在此做一个小结. 一.普通链接 <a href="http://www.baidu.com">百度&l ...
- 关于MYSQL ERROR1045 报错的解决办法
**问题描述 **ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)或者ERROR ...
- Django的AbstractUser的几大步骤
- 导入 AbstractUser from django.contrib.auth.models import AbstractUser - 往其中添加列,即可在Django的基础上添加我们所需要的 ...
- [svc]inotify+rsync解决nfs单点问题
安装配置inotify 参考 yum install inotify* -y [root@n2 shell]# rpm -qa|grep inotify inotify-tools-3.14-8.el ...
- 读取Excel二进制写入DB,并从DB中读取生成Excel文件
namespace SendMailSMSService { class Program { static void Main(string[] args) { var connString = Sq ...
- 3. Tensorflow生成TFRecord
1. Tensorflow高效流水线Pipeline 2. Tensorflow的数据处理中的Dataset和Iterator 3. Tensorflow生成TFRecord 4. Tensorflo ...
- python虚拟环境virtualenv的安装与使用
如果我们要同时开发多个应用程序,每个应用可能需要各自拥有一套“独立”的Python运行环境,我们可以使用virtualenv解决这个问题,它可以为一个应用创建一套“隔离”的Python运行环境. 一. ...
- 319. Bulb Switcher
题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...
- Java如何从IP地址查找主机名?
在Java编程中,如何从IP地址查询出主机名? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将指定的IP地址查到主机名称. package ...