题意:给你一些任务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> #in…
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…
拓扑排序模版题型: 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 be…
题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向边,如果得到的图是有向无环图DAG(Directed Acyclic Graph),则是存在拓扑排序的,如果存在有向环,则不存在拓扑排序. 注意输入数据里面m可能等于0的情况,就因为这个WA了两次. //#define LOCAL #include <iostream> #include <…
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被分离出来. 如果最后不存在入度为0的节点,那就说明有环,不存在拓扑排序,也就是很多题目的无解的情况. 下面是算法的演示过程. 下面看一道例题 题目链接:https://vjudge.net/problem/UVA-10305 代码真的很简单 ,完全是水题. 看代码: #include<iostrea…
题意: 给定n个点,与m条边, 给出他们的拓扑排序. 分析: 拓扑排序可以有两种做法, 第一种是dfs, 每次都找到某一个点的终点, 然后加入序列末尾, 正在访问的标记为-1, 访问过的标记为1, 未访问的标记为0 int c[maxn]; vector<int> G[maxn]; stack<int> s; bool dfs(int u){ c[u] = -;//标记为正在访问 ; i < G[i].size(); i++){ int v = G[u][i]; ) retu…
题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include <iostream> #include <cstring> #include <stack> using namespace std; ; int G[maxn][maxn],tag[maxn],m,n ; stack<int> s ; void dfs(int u)…
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有解不必考虑无解的情况. #include<bits/stdc++.h> using namespace std; ]; ][]; int main() { int n,m,i,j,k; ; queue<int>q; memset(,sizeof(in)); memset(e,,size…
刘书上例题  拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <sstream> #include <string> #include &…
题意: 有n种正放形,每种正方形的数量可视为无限多.已知边与边之间的结合规则,而且正方形可以任意旋转和反转,问这n中正方形是否可以拼成无限大的图案. 分析: 首先因为可以旋转和反转,所以可以保证在拼接的过程中正方形不会自交. 把边的标号看成点,将正方形的边界A+变成B+可以看做是一条边.比如说,一个正方形中有A-和B+两条边,则A-与其他正方形中A+结合后,结合前边界为A-,结合后变为B+. 这样就得到图中的一条有向边A+ → B+ 如果能在图中找到一个环,则可以无限循环拼接正方形. #incl…