题意:给你一些任务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. React Native for Android应用名及图标修改

    应用开发完了,总不能顶着MyProject和小机器人图标就发布了吧?在发布之前,有多处需要修改的地方.今天我们来全面的看一下 应用ID 俗称PackageName,或APP ID.注意,在gradle ...

  2. github团队协作

    1.打开项目 2.将成员添加到项目成员内 3.创建分支 4.提交修改到远程仓库 5.发起讨论Pull requests 6.以讨论结果修改后,合并到master

  3. 【Android】Android布局文件的一些属性值

    第一类:属性值 true或者 false   android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 androi ...

  4. 大津法---OTSU算法

    简介: 大津法(OTSU)是一种确定图像二值化分割阈值的算法,由日本学者大津于1979年提出.从大津法的原理上来讲,该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景 ...

  5. headfirst python 03, 04

    文件与异常 python中的输入机制是基于行的, open()函数与for 语句结合使用, 可以非常容易的读取文件.(打开->处理->关闭) #!/usr/bin/env python # ...

  6. Android应用图标微技巧,8.0系统中应用图标的适配

    现在已经进入了2018年,Android 8.0系统也逐渐开始普及起来了.三星今年推出的最新旗舰机Galaxy S9已经搭载了Android 8.0系统,紧接着小米.华为.OV等国产手机厂商即将推出的 ...

  7. Python之保存和读取字典

    import pickle def save_obj(obj, name ): with open('obj/'+ name + '.pkl', 'wb') as f: pickle.dump(obj ...

  8. Go指南练习_rot13Reader

    https://tour.go-zh.org/methods/23 一.题目描述 有种常见的模式是一个 io.Reader 包装另一个 io.Reader,然后通过某种方式修改其数据流. 例如,gzi ...

  9. 占位 Bootstrap

    中文网  http://www.bootcss.com/

  10. [IR] What is XML

    Concept: http://www.w3school.com.cn/xml/xml_cdata.asp Semistructured: 和普通纯文本相比,半结构化数据具有一定的结构性.OEM(Ob ...