Bzoj 4010: [HNOI2015] dishes making topological sorting

Source: Internet
Author: User

4010: [HNOI2015] Food production

Time limit:20 Sec Memory limit:256 MB

Topic Connection

Http://uoj.ac/problem/67

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>, &LT;4,1&GT, then the order of production is 3,4,1,2. Example 2: A total of 5 dishes, two restrictions on <5,2>, &LT;4,3&GT, 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&gt, 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 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

Test Instructions

Exercises

Topological ordering problem, because he wants to output a dictionary order of the smallest, then we will use the Priority queue optimization is good.

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10;    while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;}  while(x>0) ch[++num]=x%Ten, x/=Ten;  while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************intHEAD[MAXN];inttop;intD[MAXN];p riority_queue<int>Q;structedge{intV,next;} E[MAXN];intCNT;intANS[MAXN];intn,m;voidInsertintUintv) {E[CNT].V=v; E[cnt].next=Head[u]; Head[u]=CNT; CNT++;}voidSolveintx)    {Q.pop (); ans[++top]=x;  for(inti=head[x];i>=0; i=E[i].next) {D[E[I].V]--; if(d[e[i].v]==0) Q.push (E[I].V); }}intMain () {//freopen ("Test.txt", "R", stdin);    intt=read ();  while(t--) {n=read (), m=read (); CNT=top=0; memset (Head,-1,sizeof(head)); memset (d,0,sizeof(d));  for(intI=1; i<=m;i++)        {            intU=read (), v=read ();            Insert (V,U); D[u]++; }         for(intI=1; i<=n;i++)            if(!D[i]) Q.push (i);  while(!Q.empty ())        {Solve (Q.top ()); }        if(top!=N) printf ("impossible!\n"); Else        {             for(inti=n;i;i--) printf ("%d", Ans[i]); printf ("\ n"); }    }    return 0;}

Bzoj 4010: [HNOI2015] dishes making topological sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.