标签:
---恢复内容开始---
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4875 | Accepted: 3236 | Special Judge |
Description
Input
Output
Sample Input
5 0 4 5 1 0 1 0 5 3 0 3 0
Sample Output
2 4 5 3 1
Source
// // main.cpp // poj2367 // // Created by Candy on 9/10/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=105; int n,ch[N][N],t; int topo[N],vis[N]; bool dfs(int u){ vis[u]=-1;int cnt=ch[u][0]; for(int i=1;i<=cnt;i++){ int v=ch[u][i]; if(vis[v]==-1) return false; if(!vis[v]&&!dfs(v)) return false; } vis[u]=1;topo[t--]=u; return true; } bool toposort(){ t=n; for(int i=1;i<=n;i++) if(!vis[i]) if(!dfs(i)) return false; return true; } int main(int argc, const char * argv[]) { scanf("%d",&n); for(int i=1;i<=n;i++){ int a,cnt=0; while(true){ scanf("%d",&a);if(a==0) break; ch[i][++cnt]=a; } ch[i][0]=cnt; } toposort(); for(int i=1;i<=n;i++) printf("%d ",topo[i]); return 0; }
拓扑排序 POJ2367Genealogical tree[topo-sort]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5861467.html