CodeForces 412D 手速题

//CodeForces 412D

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 using namespace std;
 7 int tot, first[30010], next[100010], to[100010];
 8 bool vis[30010];
 9 int n, m;
10 int st, s[300010];
11 void dfs(int p)
12 {
13     vis[p] = 1;
14     int edge;
15     for(edge = first[p]; edge; edge = next[edge]){
16         if(!vis[to[edge]])
17             dfs(to[edge]);
18     }
19     s[++st] = p;
20 }
21 
22 int main()
23 {
24     int i, j;
25     scanf("%d%d", &n, &m);
26     int a, b;
27     tot = 0;
28     for(i = 1; i <= m; ++i) {
29         scanf("%d%d", &a, &b);
30         next[++tot] = first[a];
31         first[a] = tot;
32         to[tot] = b;
33     }
34     st = 0;
35     for(i = 1; i <= n; ++i) {
36         if(!vis[i])
37             dfs(i);
38     }
39     printf("%d", s[1]);
40     for(i = 2; i <= n; ++i)
41         printf(" %d", s[i]);
42     printf("
");
43 }
原文地址:https://www.cnblogs.com/AC-Phoenix/p/4298841.html