标签:des style color java os io strong 数据
4 3 1 2 2 3 4 3
1 2 4 3
#include<iostream> # include<cstdio> # include<cstring> # include<vector> # include<queue> using namespace std; const int maxn=500+5; int du[maxn],a[maxn],tot; vector <int> G[maxn]; int n,m; int u,v; void topsort(int n) { priority_queue<int,vector<int>,greater<int> > Q; for(int i=1;i<=n;i++) if(!du[i]) Q.push(i); tot=0; while(!Q.empty()) { u=Q.top(); Q.pop(); a[++tot]=u; for(int i=0;i<G[u].size();i++) { v=G[u][i]; du[v]--; if(!du[v]) Q.push(v); } } } int main() { while(cin>>n>>m) { memset(du,0,sizeof(du)); for(int i=1;i<=n;i++) G[i].clear(); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); G[u].push_back(v); du[v]++; } topsort(n); for(int i=1;i<=n;i++) { if(i!=n) printf("%d ",a[i]); else printf("%d",a[i]); } cout<<endl; } return 0; }
标签:des style color java os io strong 数据
原文地址:http://blog.csdn.net/u013514722/article/details/38398949