收藏一篇讲的不错的拓扑排序:点击打开链接

HDU1285 确定比赛名次

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


Problem Description


有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。


 



Input


输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。


 



Output


给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。


 



Sample Input


4 3 1 2 2 3 4 3


 



Sample Output


1 2 4 3


 【解析】:


拓扑排序过程中,sort一下,以求得最小号的拓扑排序


【代码】:


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. int f=0,int t=0,int ne=0){  
12.         from=f;to=t;next=ne;  
13.     }  
14. }edge[120000];  
15. int head[520];  
16. int in[520];//入度  
17. int main()  
18. {  
19. int n,m;  
20. while(cin>>n>>m)  
21.     {  
22. sizeof(head));  
23. sizeof(in));  
24. for(int i=0;i<m;i++)  
25.         {  
26. int f,t;  
27.             cin>>f>>t;  
28.             in[t]++;  
29.             edge[i]=NODE(f,t,head[f]);  
30. //链式前向星  
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. //排序取最小序号  
39. int s=queue[i];//此起点的边全删  
40. for(int j=head[s];j!=-1;j=edge[j].next)  
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. ' ';  
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 20 11 22 20 11 00 0





 


Sample Output





YESNO




【解析】:判断给出的有向图,有没有环路。用拓扑排序,拍一遍吧,环路肯定排不进去。


坑:注意有可能输入重边,而这里用的是链式前向星,重边不影响。


如果用邻接矩阵,重边的入度不能加!


【代码】:



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. int f=0,int t=0,int ne=0){    
12.         from=f;to=t;next=ne;  
13.     }    
14. }edge[120000];    
15. int head[520];    
16. int in[520];//入度    
17. int main()    
18. {    
19. int n,m;    
20. while(cin>>n>>m,n)    
21.     {    
22. sizeof(head));    
23. sizeof(in));    
24. for(int i=0;i<m;i++)    
25.         {    
26. int f,t;    
27.             cin>>f>>t;    
28.             in[t]++;    
29.             edge[i]=NODE(f,t,head[f]);    
30. //链式前向星    
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];//此起点的边全删    
40. for(int j=head[s];j!=-1;j=edge[j].next)    
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. }