简介
- 每个顶点出现且只出现一次。
- 若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面。
例题1 HDU 3342 Legal or Not
Problem Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?
We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.
Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
Sample Input
- 3 2
- 0 1
- 1 2
- 2 2
- 0 1
- 1 0
- 0 0
Sample Output
- YES
- NO
C++代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<iostream> #include<vector> #include<queue> using namespace std; //topological order int main(){ int n, m; while(cin>>n>>m){ if(n == 0 && m == 0)break; int a, b; int cnt[100]; //统计每个节点的入度 vector<int> v[100]; for(int i = 0; i < m; i++){ cin>>a>>b; v[a].push_back(b); //存入后继 cnt[b]++; //入度增加 } queue<int> Q; //保存入度为0的节点和队列 for(int i = 0; i < n; i++){ //找到入度为0的节点存起来 if(cnt[i] == 0)Q.push(i); } int times = 0; while(!Q.empty()){ int nowP = Q.front(); //取出第一个入度为0的节点 Q.pop(); times++; for(int i = 0; i < v[nowP].size(); i++){ cnt[v[nowP][i]]--; //当前节点所有后继的入度减1 if(cnt[v[nowP][i]] == 0){ Q.push(v[nowP][i]); } } } if(times == n)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; } |
例题2 PAT 1146 Topological Order (25 分)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.
Input Specification
Each input file contains one test case. For each case, the first line gives two positive integers N ( 1,000), the number of vertices in the graph, and M ( 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K ( 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.
Output Specification
Print in a line all the indices of queries which correspond to "NOT a topological order". The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
6 8 1 2 1 3 5 2 5 4 2 3 2 6 3 4 6 4 5 1 5 2 3 6 4 5 1 2 6 3 4 5 1 2 3 6 4 5 2 1 6 3 4 1 2 3 4 5 6 |
Sample Output
1 2 |
3 4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> using namespace std; int main() { int n, m, a, b, k, flag = 0, in[1010]; vector<int> v[1010]; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); v[a].push_back(b); in[b]++; } scanf("%d", &k); for (int i = 0; i < k; i++) { int judge = 1; vector<int> tin(in, in+n+1); for (int j = 0; j < n; j++) { scanf("%d", &a); if (tin[a] != 0) judge = 0; for (int it : v[a]) tin[it]--; } if (judge == 1) continue; printf("%s%d", flag == 1 ? " ": "", i); flag = 1; } return 0; } |