菜肴制作

很容易想到思路,然而考试的时候勺的呢,没想到用priority_qq

倒序输出反图的字典序最大拓扑序

 1     #include<bits/stdc++.h>
 2     using namespace std;
 3       
 4     #define maxn 100005
 5     #define maxm 100005
 6     priority_queue<int>Q;//
 7       
 8     int n,m,T,in[maxn],sta[maxn],top;
 9     int cnt,v[maxm],next[maxm],first[maxn];
10       
11     int read(){
12         int tmp=0;
13         char ch=0;
14         while(!isdigit(ch))ch=getchar();
15         while(isdigit(ch)){
16             tmp=tmp*10+ch-'0';
17             ch=getchar();
18         }
19         return tmp;
20     }
21     void add(int st,int end){
22         v[++cnt]=end;
23         next[cnt]=first[st];
24         first[st]=cnt;
25     }
26     void topo(){
27         for(int i=1;i<=n;i++)
28             if(!in[i])Q.push(i);
29         while(!Q.empty()){
30             int x=Q.top();Q.pop();
31             sta[++top]=x;
32             for(int e=first[x];e;e=next[e]){
33                 if(!--in[v[e]]) 
34                     Q.push(v[e]);
35             }
36         }
37     }
38     int main(){
39         T=read();
40         while(T--){
41             memset(first,0,sizeof(first));
42             memset(in,0,sizeof(in));
43             cnt=top=0;
44             n=read(),m=read();
45             for(int i=1;i<=m;i++){
46                 int a,b;
47                 a=read(),b=read();
48                 add(b,a),in[a]++;
49             }
50             topo();
51             if(top!=n)printf("Impossible!
");
52             else{
53                 for(int i=top;i>=1;i--)
54                     printf("%d ",sta[i]);
55                 printf("
");
56             }   
57         }
58         return 0;
59     }
60 
View Code
原文地址:https://www.cnblogs.com/Ngshily/p/5093723.html