标签:
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; int i,n,m; int a[505],cnt[505]; struct cmp { bool operator()(int a,int b) { return a>b; } }; vector<int> s[505]; void toposort() { priority_queue<int,vector<int>,cmp> Q; int l=0; for(int i=1;i<=n;i++) if (cnt[i]==0) Q.push(i); while(!Q.empty()) { int u=Q.top(); Q.pop(); a[++l]=u; for(int i=0;i<s[u].size();i++) { cnt[s[u][i]]--; if (cnt[s[u][i]]==0) Q.push(s[u][i]); } } return; } int main() { memset(cnt,0,sizeof(cnt)); while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) s[i].clear(); for(i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); s[x].push_back(y); cnt[y]++; } toposort(); for(i=1;i<n;i++)printf("%d ",a[i]); printf("%d\n",a[n]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5700367.html