标签:des style blog http color io os java ar
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1161 Accepted Submission(s): 368
#include<cstdio> #include<cstring> #include<stack> #include<vector> #include<queue> using namespace std; const int maxn=30005; vector<vector<int> >mat; stack<int >ans; int outdg[maxn]; struct cmp{ bool operator ()( int &a,int &b){ return a<b; } }; priority_queue<int,vector<int>,cmp>aa; int n,m; void tp_sort(){ int len,i; while(!aa.empty()){ i=aa.top(); aa.pop(); ans.push(i); len=mat[i].size(); for(int j=0;j<len;j++){ outdg[mat[i][j]]--; if(outdg[mat[i][j]]==0)aa.push(mat[i][j]); } } } int main(){ // sync_with_stdio(1); int cas,i,a,b; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&m); mat.clear(); mat.resize(n+2); memset(outdg,0,sizeof(int)*(n+1)); for(i=0;i<m;i++){ scanf("%d%d",&a,&b); mat[b].push_back(a); outdg[a]++; } for(i=n;i>=1;i--) if(outdg[i]==0)aa.push(i); tp_sort(); printf("%d",ans.top()); ans.pop(); while(!ans.empty()){ printf(" %d",ans.top()); ans.pop(); } puts(""); } return 0; }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/gongxijun/p/3965835.html