标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
思路:由于1的优先级大于2;意思就是你尽量让1在前面,然后再考虑2。。。。然不是字典序,所以这题不可以按板子题写;
逆向建图,然后把大的尽量往后放,最后逆向输出;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; int du[N]; int flag[N]; int ans[N],len; void init() { len=0; memset(du,0,sizeof(du)); memset(flag,0,sizeof(flag)); } int main() { int x,y,z,i,t; int T; scanf("%d",&T); while(T--) { init(); vector<int>v[N]; priority_queue<int>q; scanf("%d%d",&x,&y); for(i=0;i<y;i++) { int u,vv; scanf("%d%d",&u,&vv); v[vv].push_back(u); du[u]++; } for(i=1;i<=x;i++) { if(du[i]==0) { q.push(i); flag[i]=1; } } while(!q.empty()) { int vv=q.top(); q.pop(); ans[len++]=vv; for(i=0;i<v[vv].size();i++) { du[v[vv][i]]--; if(du[v[vv][i]]==0&&!flag[v[vv][i]]) q.push(v[vv][i]); } } printf("%d",ans[x-1]); for(i=x-2;i>=0;i--) printf(" %d",ans[i]); printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5597746.html