4010: [HNOI2015] Food production time limit:5 Sec Memory limit:512 MB
submit:446 solved:251
[Submit] [Status] [Discuss] Description
The famous gourmet Small A is invited to the ATM Grand Hotel for its tasting dishes.
The ATM hotel has a N-course menu for small A, and the hotel has a sequential number from 1 to n for the estimated quality of the dishes, with the highest estimated quality of the dishes numbered 1. Due to the problem of the taste collocation of the dishes, some dishes must be made before other dishes, specifically, there is a total of M bar such as "I dish" must be ' prior to the production of J "limit, we will be abbreviated as <i,j>. Now, the hotel would like to find an optimal order of the dishes, so that small a can try to eat the highest quality dishes: that is, (1) in order to meet all the restrictions on the premise, 1th dishes "as far as possible" priority production, (2) in order to meet all the restrictions, 1th dishes "as far as possible" priority, 2nd dishes " (3) In the case of all restrictions, 1th and 2nd dishes "as far as possible" priority, 3rd dishes "as far as possible" priority production, (4) in meeting all the restrictions, 1th and 2nd and 3rd dishes "as far as possible" priority, 4th dishes "as far as possible" first production, (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: A total of 5 dishes, two restrictions on <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 4th priority, so the current can determine the first three courses of production sequence is 3,4,1; next consider 2, Determine the final order of production is 3,4,1,2. Example 2, the first production of 1 is not against the limit, the next consideration of the 2 o'clock has a <5,2> limit, so the next first to make 5 and then make 2, the next 3 when there are <4,3> restrictions, so the next first 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, uppercase letters, other lowercase) Input
The first line is a positive integer d, which represents the number of data groups.
Next is the Group D data. For each set of data: the first row of two positive integers separated by spaces N and M, respectively, indicating the number of dishes and the order limit of the entries. Next M-line, two positive integers x, y for each line, indicates that the "X-dishes must precede the production of the Y-dishes" limit. (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 Input3
5 4
5 4
5 3
4 2
3 2
3 3
1 2
2 3
3 1
5 2
5 2
4 3Sample Output1 5 3) 4 2
impossible!
1 5 2) 4 3HINT
"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.
Source
Problem: At first glance, do the dishes must follow a certain order, then the basic can be determined and topsort related, and then look at the topic, it seems to be as small as possible before (Hansbug: Special note--is the trumpet as far as possible, not the smallest dictionary order, the two have a very big difference), So at first my reaction was to ask for the smallest topological sequence of the dictionary order, and then make a run on the rough line of the previous case = =
However, this problem is not difficult to solve, since there is no way, it is better to--1. Reverse build Figure 2. Reverse dictionary order the largest, then snapped out, and then inverted the output, there is no
Ps:1. Ben Konjac Konjac because too weak, a moment did not think of the ordinary heap how to write (Hansbug: Only will write left side tree has the TT), so tease than the kind of a tree segment, the function is a single point of modification + query the maximum position of 0 (hansbug: ok i compare tease please ignore Qaq)
2. Pay particular attention to the fact that this data seems to be in each row of non-impossible! output finally seemingly want to keep a space, I did not retain the results in the first PE
1/**************************************************************2Problem:40103 User:hansbug4 language:pascal5 result:accepted6Time:2904Ms7Memory:11180KB8****************************************************************/9 Ten type OnePoint=^node; ANode=Record - G:longint; - Next:point; the End; - var -A:Array[0..200000] ofPoint ; -E,b,c,f:Array[0..100005] ofLongint; +D:Array[0..1000005] ofLongint; - I,j,k,l,m,n:longint; + functionMax (x,y:longint): Longint; A begin at ifX>y ThenMax:=xElsemax:=y; - End; - functionmin (x,y:longint): Longint; - begin - ifX<y ThenMin:=xElsemin:=y; - End; in procedureSwapvarx,y:longint); - varZ:longint; to begin +z:=x;x:=y;y:=Z; - End; the procedureAdd (x,y:longint); * varP:point; $ beginPanax NotoginsengNew (p);p ^.g:=y;p^.next:=a[x];a[x]:=p; - End; the procedurebuilt (z,x,y:longint); + begin A ifX=y Then the begin +d[z]:=E[x]; -b[x]:=Z; $ End $ Else - begin -Built (z*2, X, (X+y)Div 2); theBuilt (z*2+1, (X+y)Div 2+1, y); -D[z]:=min (d[z*2],d[z*2+1]);Wuyi End; the End; - functionGetzero (z,x,y:longint): Longint; Wu begin - ifD[z]<>0 ThenExit (-1); About if(x=y) and(d[z]=0) Thenexit (x); $ ifd[z*2+1]=0 ThenExit (Getzero (z*2+1, (X+y)Div 2+1, y)) - ElseExit (Getzero (z*2, X, (X+y)Div 2)); - End; - procedurebeep (x,y:longint); A begin +x:=B[x]; the ifY=maxlongint ThenD[x]:=maxlongintElseInc (D[X],Y); - whileX>1 Do $ begin theX:=xDiv 2; theD[x]:=min (d[x*2],d[x*2+1]); the End; the End; - procedurePD; in varI,j,k,l,n,m:longint;p:point; the begin the readln (n,m); AboutFillchar (E,sizeof (E),0); theFillchar (F,sizeof (f),0); theFillchar (C,sizeof (c),0); the fori:=0 toN Doa[i]:=Nil; + fori:=1 toM Do - begin the readln (j,k);Bayi Add (K,J); Inc (E[j]); the End; theBuilt1,1, n); - fori:=1 toN Do - begin theF[i]:=getzero (1,1, n); the iff[i]=-1 Then the begin theWriteln ('impossible!'); - exit; the End; thec[f[i]]:=1; beep (f[i],maxlongint); thep:=A[f[i]];94 whileP<>Nil Do the begin the ifc[p^.g]=0 ThenBeep (p^.g,-1); thep:=P^.next;98 End; About End; - forI:=nDownto 1 DoWrite (F[i],' ');101 Writeln;102i:=0;103 End;104 begin the READLN (n);106 fori:=1 toN DoPD;107 Readln;108 109 End.
4010: [HNOI2015] food production