题解:裸的topo,注意判重,由于数据升序所以免排序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
#include <cstdio> #include <iostream> using
namespace std; #define N 505 int map[N][N],n,m,a,b,in[N],ans[N]; void
topo(){ int
top=0,i; memset (ans,0, sizeof
ans); while ( true ){ for (i=1;i<=n;i++) if (in[i]==0) break ; if (i==n+1) return ; in[i]=-1; ans[top++]=i; for ( int
x=1;x<=n;x++) if (map[i][x])in[x]--; } } int
main(){ while (~ scanf ( "%d%d" ,&n,&m)){ memset (map,0, sizeof
map); memset (in,0, sizeof
in); for ( int
i=0;i<m;i++){ scanf ( "%d%d" ,&a,&b); if (!map[a][b]){ map[a][b]++; in[b]++; } } topo(); printf ( "%d" ,ans[0]); for ( int
i=1;i<n;i++) printf ( " %d" ,ans[i]); printf ( "\n" ); } } |
原文:http://www.cnblogs.com/forever97/p/3561923.html