Topological sorting
The problem is a topological order that requires n points, and satisfies the following conditions: The position of the number 1 is as high as possible, on this basis the position of the number 2 is as far as possible.
I see the first feeling of this problem: the queue for topology sorting is changed to priority queue, the smaller the number the sooner it comes out.
But even the sample is not enough = = because this is "the dictionary order of the smallest", and does not necessarily meet the conditions of the topic (see Sample to know, so in fact, the early team element number as small as possible , not exactly the number of small early out of the team)
So what's wrong? Don't you want us to come back? >_>
Turn all sides backwards! Reverse the topological sequence! This time we let the early team elements of the larger the better, that is, the smaller the number as far as possible , because in the original order in the front, is in the topological reverse.
Then the AC is hot ~
In fact, Konjac Konjac will not prove ... Perceptual understanding is: For the number of small elements, as far as possible with a number than his big delay, let him late out of the team, should be a bit greedy thought it = =
P.S. This should be one of the simplest of the Hu strategy. Instead put it in the position of the C question ...
1 /**************************************************************2 problem:40103 User:tunix4 language:c++5 result:accepted6 time:820 Ms7 memory:4520 KB8 ****************************************************************/9 Ten //huce #7 C One#include <queue> A#include <vector> -#include <cstdio> -#include <cstdlib> the#include <cstring> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + using namespacestd; A at intGetint () { - intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9') {if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } intypedefLong LongLL; - Const intn=100010, inf=~0u>>2; to /*******************template********************/ + intto[n<<1],next[n<<1],head[n],cnt; - voidAddintXinty) { theTo[++cnt]=y; NEXT[CNT]=HEAD[X]; head[x]=CNT; * } $ intN,m,du[n];Panax Notoginseng voidinit () { -N=getint (); m=getint (); theCnt=0; Memset (Head,0,sizeofhead); +Memset (Du,0,sizeofdu); A intx, y; theF (I,1, M) { +X=getint (); y=getint (); - swap (x, y); $Add (x, y); du[y]++; $ } - } - intAns[n],tot; the voidsolve () { -priority_queue<int>Q;WuyiF (I,1, N)if(!Du[i]) Q.push (i); thetot=0; - while(!Q.empty ()) { Wu intx=Q.top (); Q.pop (); -ans[++tot]=x; About for(intI=head[x];i;i=Next[i]) { $du[to[i]]--; - if(du[to[i]]==0) Q.push (To[i]); - } - } A if(tot==n) {D (I,tot,1) printf ("%d", Ans[i]); Puts"");} + ElsePuts"impossible!"); the } - $ intMain () { the #ifndef Online_judge theFreopen ("c.in","R", stdin); the //freopen ("Output.txt", "w", stdout); the #endif - intt=getint (); in while(t--){ the init (); the solve (); About } the return 0; the}
View Code 4010: [HNOI2015] Food production time limit:5 Sec Memory limit:512 MB
submit:267 solved:159
[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 [Submit] [Status] [Discuss]
"Bzoj" "4010" "HNOI2015" dishes made