HDOJ1285 确定比赛名次[拓扑排序]


确定比赛名次

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


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
 
Author
SmallBeer(CML)
 
Source
 
Recommend
lcy
 
 
 
 
 
 
 
 
 
虽然过了,但写的不是很满意,不喜欢
code:
 1 #include <iostream>   
 2 #include <iomanip>   
 3 #include <fstream>   
 4 #include <sstream>   
 5 #include <algorithm>   
 6 #include <string>   
 7 #include <set>   
 8 #include <utility>   
 9 #include <queue>   
10 #include <stack>   
11 #include <list>   
12 #include <vector>   
13 #include <cstdio>   
14 #include <cstdlib>   
15 #include <cstring>   
16 #include <cmath>   
17 #include <ctime>   
18 #include <ctype.h> 
19 using namespace std;
20 
21 int main()
22 {
23     int map[501][501];
24     int vst[501];
25     int result[501];
26     int n,m;
27     int i,j,k;
28     int a,b;
29     bool flag;
30     int cnt;
31     while(~scanf("%d%d",&n,&m))
32     {
33         memset(map,0,sizeof(map));
34         memset(vst,0,sizeof(vst));
35         cnt=1;
36         for(i=1;i<=m;i++)
37         {
38             scanf("%d%d",&a,&b);
39             map[a][b]=1;
40         }
41         while(cnt<=n)
42         {
43             for(i=1;i<=n;i++)
44             {
45                 flag=true;
46                 if(vst[i])
47                     continue;
48                 for(j=1;j<=n;j++)
49                 {
50                     if(!vst[i]&&map[j][i]==1)
51                     {
52                         flag=false;
53                         break;
54                     }
55                 }
56                 if(flag)
57                 {
58                     vst[i]=1;
59                     if(cnt==1)
60                         printf("%d",i);
61                     else
62                         printf(" %d",i);
63                     cnt++;
64                     for(j=1;j<=n;j++)
65                     {
66                         if(map[i][j]==1)
67                             map[i][j]=0;
68                     }
69                     break;
70                 }
71             }
72         }
73         printf("\n");
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/XBWer/p/2622394.html