Uva--10305 (拓扑排序)
2014-07-09 02:27:23
题意&思路:裸拓扑,不赘述。
1 #include <cstring> 2 #include <iostream> 3 using namespace std; 4 int main(){ 5 int g[105][105],n,m,inc[105],ans[105],cnt,a,b,i,j; 6 while(cin >> n >> m && (n || m)){ 7 memset(g,0,sizeof(g)); 8 memset(inc,0,sizeof(inc)); 9 cnt = 0; 10 for(i = 1; i <= m; ++i) 11 cin >> a >> b , g[a][b] = 1 , ++inc[b]; 12 for(i = 1; i <= n; ++i){ 13 for(j = 1; j <= n; ++j) 14 if(inc[j] == 0) 15 break; 16 inc[j] = -1; //delete this node 17 ans[cnt++] = j; 18 for(int k = 1; k <= n; ++k) 19 if(g[j][k]) --inc[k]; 20 } 21 cout << ans[0]; 22 for(i = 1; i < cnt; ++i) cout << ' ' << ans[i]; 23 cout << endl; 24 } 25 return 0; 26 }