# 拓扑排序例题 hdu1285 hdu3342

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28138    Accepted Submission(s): 11215

Problem Description

Input

Output

Sample Input
4 3 1 2 2 3 4 3

Sample Output
1 2 4 3
【解析】：

【代码】：
[cpp] view plain
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4. #include<iostream>
5. #include<algorithm>
6. #include<queue>
7. using namespace std;
8. struct NODE{
9.     int from,to;
10.     int next;
11.     NODE(int f=0,int t=0,int ne=0){
12.         from=f;to=t;next=ne;
13.     }
14. }edge[120000];
16. int in[520];//入度
17. int main()
18. {
19.     int n,m;
20.     while(cin>>n>>m)
21.     {
23.         memset(in,0,sizeof(in));
24.         for(int i=0;i<m;i++)
25.         {
26.             int f,t;
27.             cin>>f>>t;
28.             in[t]++;
31.         }
32.         int queue[520],tail=0;
33.         for(int i=1;i<=n;i++)//入度为0的点入队
34.             if(!in[i])
35.                 queue[tail++]=i;
36.         for(int i=0;i<tail;i++)
37.         {
38.             sort(queue+i,queue+tail);//排序取最小序号
39.             int s=queue[i];//此起点的边全删
41.             {
42.                 in[edge[j].to]--;
43.                 if(!in[edge[j].to])//新的入度为0的点
44.                 {
45.                     queue[tail++]=edge[j].to;
46.                 }
47.             }
48.         }
49.         for(int i=0;i<tail-1;i++)
50.             cout<<queue[i]<<' ';
51.         cout<<queue[tail-1]<<endl;
52.     }
53.     return 0;
54. }

hdu3342：

# Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9076    Accepted Submission(s): 4209

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
【解析】：判断给出的有向图，有没有环路。用拓扑排序，拍一遍吧，环路肯定排不进去。

【代码】：
[cpp] view plain
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4. #include<iostream>
5. #include<algorithm>
6. #include<queue>
7. using namespace std;
8. struct NODE{
9.     int from,to;
10.     int next;
11.     NODE(int f=0,int t=0,int ne=0){
12.         from=f;to=t;next=ne;
13.     }
14. }edge[120000];
16. int in[520];//入度
17. int main()
18. {
19.     int n,m;
20.     while(cin>>n>>m,n)
21.     {
23.         memset(in,0,sizeof(in));
24.         for(int i=0;i<m;i++)
25.         {
26.             int f,t;
27.             cin>>f>>t;
28.             in[t]++;
31.         }
32.         int queue[520],tail=0;
33.         for(int i=0;i<n;i++)//入度为0的点入队
34.             if(!in[i])
35.                 queue[tail++]=i;
36.         for(int i=0;i<tail;i++)
37.         {
38.             //sort(queue+i,queue+tail);//排序取最小序号
39.             int s=queue[i];//此起点的边全删
41.             {
42.                 in[edge[j].to]--;
43.                 if(!in[edge[j].to])//新的入度为0的点
44.                 {
45.                     queue[tail++]=edge[j].to;
46.                 }
47.             }
48.         }
49.         if(tail==n)puts("YES");
50.         else puts("NO");
51.     }
52.     return 0;
53. }

#### hdu1285 确定比赛名次(拓扑排序多种方法)

2014-07-21 16:02:41

#### HDU 1285 比赛排名(拓扑排序)

2016-07-11 11:04:29

#### hdu1285确定比赛名次（字典序最小的拓扑排序）

2014-07-22 22:36:19

#### HDU1285基于有向图邻接表的优先队列的拓扑排序

2011-12-28 15:44:43

#### HDU3342-拓扑排序

2017-03-04 00:21:39

#### HDU1285(拓扑排序+优先队列)

2015-12-16 17:45:05

#### hdu3342（Legal or Not）----- 学习拓扑排序的好例题

2015-08-09 03:32:01

#### [poj1094]拓扑排序经典例题

2017-03-28 14:41:44

#### 【算法小总结】拓扑排序+例题解析

2014-08-01 10:08:29

#### 拓扑排序的题目集合

2015-10-26 17:10:12