hdu 1285 确定比赛名次

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1285   

确定比赛名次

Description

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

Input

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

Output

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

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

Sample Input

4 3
1 2
2 3
4 3

Sample Output

1 2 4 3

拓扑排序+输出最小字典序。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::vector;
17 using std::multimap;
18 using std::priority_queue;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 510;
29 typedef unsigned long long ull;
30 struct Node { int to, next; };
31 struct cmp {
32     inline bool operator()(int &a, int &b) const {
33         return a > b;
34     }
35 };
36 struct TopSort {
37     Node G[N];
38     int tot, inq[N], head[N], topNum[N];
39     inline void init() {
40         tot = 0;
41         cls(inq, 0), cls(head, -1), cls(topNum, 0);
42     }
43     inline void add_edge(int u, int v) {
44         G[tot].to = v; G[tot].next = head[u]; head[u] = tot++;
45     }
46     inline void built(int m) {
47         int u, v;
48         rep(i, m) {
49             scanf("%d %d", &u, &v);
50             --u, --v;
51             inq[v]++;
52             add_edge(u, v);
53         }
54     }
55     inline void bfs(int n) {
56         int k = 0;
57         priority_queue<int, vector<int>, cmp> q;
58         rep(i, n) { if (!inq[i]) q.push(i); }
59         while (!q.empty()) {
60             int u = q.top(); q.pop();
61             topNum[k++] = u + 1;
62             for (int i = head[u]; ~i; i = G[i].next) {
63                 if (--inq[G[i].to] == 0) q.push(G[i].to);
64             }
65         }
66         rep(i, n) printf("%d%c", topNum[i], i < n - 1 ? ' ' : '
');
67     }
68 }work;
69 int main() {
70 #ifdef LOCAL
71     freopen("in.txt", "r", stdin);
72     freopen("out.txt", "w+", stdout);
73 #endif
74     int n, m;
75     while (~scanf("%d %d", &n, &m)) {
76         work.init();
77         work.built(m);
78         work.bfs(n);
79     }
80     return 0;
81 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4640816.html