4010: [HNOI2015] food production
Time Limit:5 Sec Memory limit:512 MB
submit:426 solved:242
[Submit] [Status] [Discuss]
Description
The famous gourmet Small A is invited to the ATM Grand Hotel for its tasting dishes.
The ATM Hotel prepares N-course dishes for small A, and the hotel offers a high-to-low quality food according to the
1 to n sequential numbering, with the highest estimated quality of the dishes numbered 1. Because of the problem of taste collocation between dishes,
Some dishes must be made before other dishes, specific, with M bars as "I dish ' must '
Prior to the "J" restriction, we shortened this restriction to I , j > 。 Now, the hotel would like to ask
Make an optimal order of dishes, so that small a can try to eat the highest quality dishes: that is,
(1) On the premise of meeting all the restrictions, 1th dishes "as far as possible" priority production; (2) in meeting all restrictions, 1
Dishes "As far as possible" priority, 2nd dishes "as far as possible" priority production, (3) in meeting the Limited
1th and 2nd Dishes "as far as possible" priority, 3rd dishes "as far as possible" priority production; (4) at full
All restrictions, 1th and 2nd and 3rd dishes "as far as possible" priority, 4th dishes "as far as possible" excellent
First, (5) and so on.
Example 1: A total of 4 dishes, two restrictions on <3,1>, <4,1>, then the order of production is 3,4,1,2. Example 2: Total
5 dishes, two restrictions <5,2>, <4,3>, then the order of production is 1,5,2,4,3. Example 1, first consider 1,
Because there are restrictions <3,1> and <4,1>, so only after the production of 3 and 4 to make 1, and according to (3), 3rd
Should be "as far as possible" than the number 4th priority, so the current can be determined to determine the first three courses of production is 3,4,1; next
Consider 2 and determine the final order of production is 3,4,1,2. Example 2, the first production of 1 is not against the limit;
Down to consider 2 o'clock there are <5,2> restrictions, so the next first to make 5 re-production 2; Next consider 3 when there is
<4,3>, so the next step is to make 4 and then make 3, so the final order is 1,5,2,4,3.
Now you need to ask for the best order of the dishes to be made. No solution output "impossible!" (without quotation marks,
First letter uppercase, other lowercase letters)
Input
The first line is a positive integer d, which represents the number of data groups.
Next is the Group D data.
For each group of data:
The first line two positive integers separated by spaces N and M, respectively, indicating the number of dishes and the production sequence limit
The number of entries made.
Next m line, two positive integer x, y for each line, means "x dishes must be made before the Y dish"
The restrictions. (Note: There may be exactly the same limit in the M-bar limit)
Output
The output file contains only D rows, N integers per row, indicating the optimal order of dishes, or
"Impossible!" Represents no solution (without quotation marks).
Sample Input
3
5 4
5 4
5 3
4 2
3 2
3 3
1 2
2 3
3 1
5 2
5 2
4 3
Sample Output
1 5 3) 4 2
impossible!
1 5 2) 4 3
HINT
"Sample Interpretation"
The second set of data also requires dishes 1 before the dishes 2 production, dishes 2 before the dishes 3 production, dishes 3 before
Dishes 1 are made, and this is impossible to satisfy anyway, thus leading to no solution.
100% of the data meets n,m<=100000,d<=3.
Idea Topic + topological sort.
Note that the problem is not to ask for the smallest dictionary order, but to ask for smaller ones as far forward as possible.
The answer is 52431, and the minimum dictionary order is 43521.
Equivalent to the inverted dictionary is the largest, because the dictionary order is the largest equivalent of small as far as possible, that is, the small as far as possible forward.
So the most topological sort of dictionary order can be done backwards.
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#define MOD 1000000007#define M 100005#include <queue>#define LL Long LongUsing namespace std;priority_queue<int>Q; struct edge{int y,NE;} E[M];intDu[m],h[m],ans[m],tot,d,n,m; voidRead(int&TMP) {tmp=0; Char Ch=getchar (); for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ()); for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 ';} void Addedge (int x,int y) {E[++tot].y=y; E[tot].NE=h[x]; h[x]=tot; du[y]++;}intMain () {Read(D); while(d--) {tot=0;Read(n),Read(m); for(intI=1; i<=n;i++) du[i]=h[i]=0; for(intI=1; i<=m; i++) {int x,y;Read(x),Read(y); Addedge (y,x); }intnow=0; for(intI=1; i<=n;i++)if(!du[i])Q.Push(i); while(!Q. empty ()) {int x=Q. Top ();Q.Pop(); ans[++now]=x; for(inti=h[x];i;i=e[i].NE) {int y=e[i].y; du[y]--;if(!du[y])Q.Push(y); } }if(now==n) { for(inti=n;i>=1; i--)printf("%d ", Ans[i]);printf("\ n"); }Else printf("impossible!\n"); }return 0;}
"Bzoj 4010" [HNOI2015] dish making