题意:给你一些任务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到底再输出。的更多相关文章

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

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

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

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

  3. UVa 10305 (拓扑排序) Ordering Tasks

    题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...

  4. Ordering Tasks UVA - 10305(拓扑排序)

    在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...

  5. Uva 10305 拓扑排序

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

  6. 拓扑排序 (Ordering Tasks UVA - 10305)

    题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...

  7. uva 10305 拓扑排序裸题

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

  8. 【紫书】Tree UVA - 548 静态建树dfs

    题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...

  9. 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)

    这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...

随机推荐

  1. TurboCAD Pro for Mac(二维绘图和三维建模工具)破解版安装

    1.软件简介    TurboCAD Pro 是 macOS 系统上一款二维绘图和三维建模工具,具备强大的绘图和设计特性,加上强大的创建复杂的三维模型的工具,三维 OpenGL 的渲染,和超过 11, ...

  2. python工具 - 从文件名读取特定信息到excel表格

    情景:文件名中包含学号和用户名,其中用户名在前学好在后,学号为2位,如harry33.txt.natasha12.txt. 要求:将多个文件名中的用户名与学号分开并保存到excle中. 代码部分: i ...

  3. SAP Study Notes: BW Queriy-Variables(变量)

    About Variable:1.Variable 是和InfoObject绑定的,可用于任何含有该IO的query中.2.Variable有以下几种类型:Characteristic:用于限制Cha ...

  4. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  5. idea language level 介绍

    language level 介绍 其他 IDE 没有看到类似 language level 的设置,所以这个功能应该算是 IntelliJ IDEA 特有的,可是 IntelliJ IDEA 官网也 ...

  6. c# 调用非托管c++dll 参数问题(转)

    在C#中调用C(C++)类的DLL的时候,有时候C的接口函数包含很多参数,而且有的时候这些参数有可能是个结构体,而且有可能是结构体指针,那么在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细 ...

  7. SmileyCount.java笑脸加法程序代写(QQ:928900200)

    SmileyCount.java 1/4Java Programming 2014Course Code: EBU4201Mini ProjectTask 1 [30 marks]SmileyCoun ...

  8. Fedora 21 安装 Bumblebee with the NVIDIA proprietary drivers

    最新文章:Virson's Blog 参考Fedora Wiki:http://fedoraproject.org/wiki/Bumblebee#Fedora_21

  9. Java如何使服务器允许连接到套接字端口?

    在Java编程中,如何使服务器允许连接到套接字端口? 以下示例显示如何使服务器通过使用ServerSocket类的server.accept()方法和Socket类的sock.getInetAddre ...

  10. Cisco NTP配置

    Windows 或 Linux 系统配置成NTP服务器,思科交换机配置成NTP客户端 ##创建VLAN 10 SW01>enable SW01#vlan database SW01(vlan)# ...