标签:
input | output |
---|---|
5 0 4 5 1 0 1 0 5 3 0 3 0 |
2 4 5 3 1 |
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define inf 10000000 #define mod 10000 typedef long long ll; using namespace std; const int N=605; const int M=50000; int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;} int in[N],vis[N]; int n,m,k; vector<int>vec[N]; void dfs(int x) { printf("%d ",x); int f=0; vis[x]=1; for(int i=0;i<vec[x].size();i++){ int v=vec[x][i]; if(in[v])in[v]--; } for(int i=1;i<=n;i++)if(!in[i]&&!vis[i])f=1,dfs(i); if(!f) return; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++){ while(~scanf("%d",&m)&&m){ in[m]++; vec[i].push_back(m); } } for(int i=1;i<=n;i++){ if(!in[i]&&!vis[i]){ dfs(i); } } printf("\n"); return 0; }
timus 1022 Genealogical Tree(拓扑排序)
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5853196.html