During the description Expo, Shanghai's air traffic volume exceeded peacetime, and the attendant air traffic control also occurred frequently. Recently, little X was delayed more than two hours at the airport for two consecutive times because of air control. For this, small x is very dissatisfied. On the way to Yantai, small x Unfortunately once again encountered air control. So little X began to think about the issue of air traffic control. Assume that there are currently delayed flights of N, numbered 1 to N. The airport has only one takeoff runway, and all flights need to take off in a certain order (call this sequence a take-off sequence). Define the departure sequence of a flight as the flight's location in the take-off series, which is the first flight. There are two types of restrictions in the take-off sequence:? First Class (the latest departure time limit): Flight number I is not more than Ki; Class II (relative takeoff order limit): There are some relative takeoff order restrictions (A, B), indicating that flight A's departure time must be earlier than Flight B, that is, flight A's departure sequence number must be less than Flight B's departure number. The first question in small X's thinking is whether a feasible takeoff sequence can be computed given the two types of constraints. The second question is how to find out the minimum number of departures for each flight in all feasible take-off sequences, considering two types of constraints. The first line of input contains two positive integers n and m,n represents the number of flights, and m represents the number of the second class of restrictions (relative to the takeoff order limit). The second line contains n positive integers k1, K2, ", kn. The next m line, two positive integers a and b per line, represents a pair of relative takeoff order limits (A, b), where 1≤a,b≤n indicates that flight a must take off before flight B. The output contains n integers t1, T2, ", TN, where TI represents the minimum flight number of the flights I may have, and adjacent two integers are separated by a space. Sample Input
5 5
4 5 2) 5 4
1 2
3 2
5 1
3 4
3 1
Sample Output
3 4 1) 2 1
In Sample 1:
Take-off sequence 3 5 1 4 2 All the restrictions are met, and all the takeoff sequences that meet the criteria are:
3 4 5 1 2 3 5 1 2 4 3 5 1 4 2 3 5 4 1 2
5 3 1 2 4 5 3 1 4 2 5 3 4 1 2
Due to the existence (5, 1) and (3, 1) Two restrictions, Flight 1 can only be arranged after flights 5 and 3, so the earliest
Departure time is 3 and other flights are similar.
For 30% data: n≤10;
For 60% data: n≤500;
For 100% data: n≤2,000,m≤10,000. Positive Solution: topological sort + greedyProblem Solving Report:today, when the lecture, I said this problem, in fact, I still did not knock on the ... finish the lecture and start the AC problem quickly. The difficulty of thinking has a bit big, too lazy to write a detailed explanation, the lecture was about half an hour ... to put it simply, the meaning of the edge is that it must be later than the first. The topology is then sorted to update the latest time for each flight. (Note that the update will be done after the topology sort!!!) Start always wa) after sorting, first ask each to put back as far as possible. The second question is to consider the DFS over all points that X can reach, apparently X is later than all of these nodes and re-framing the edges. Then we consider the first question from the order of the inverted put, note the details, if the current constraints but the previous unconstrained, and W equal, consider the direct skip, anyway forward to be more secure legal.
1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - #ifdef WIN32 - #defineOT "%i64d" the #else - #defineOT "%lld" - #endif - using namespacestd; +typedefLong LongLL; - Const intMAXN = .; + Const intMAXM =10011; A intn,m; at intW[MAXN]; - intFIRST[MAXN],NEXT[MAXM],TO[MAXM]; - intecnt; - BOOLVIS[MAXN]; - inttop; - intDUI[MAXN]; in intMP[MAXN][MAXN]; - to structljh{ + intVal,jilu; - }A[MAXN]; the *InlineintGetint () $ {Panax Notoginseng intw=0, q=0; - CharC=GetChar (); the while((c<'0'|| C>'9') && c!='-') c=GetChar (); + if(c=='-') q=1, c=GetChar (); A while(c>='0'&& c<='9') w=w*Ten+c-'0', c=GetChar (); the returnQ? -w:w; + } - $InlinevoidLinkintXintY) {next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;} $ -InlinevoidTopo_sort (intx) { -vis[x]=1; the for(intI=first[x];i;i=Next[i]) { - intv=To[i];Wuyi if(!Vis[v]) { the //w[v]=min (w[v],w[x]-1); - Topo_sort (v); Wu } - } Aboutdui[++top]=x; $ } - -InlineBOOLCMP (LJH q,ljh QQ) {returnq.val<Qq.val;} - AInlinevoidMake () { + for(inti=n;i>=1; i--) { the intu=Dui[i]; - for(intj=first[u];j;j=Next[j]) { $W[to[j]]=min (w[u]-1, W[to[j]]); the } the } the the for(intI=1; i<=n;i++) a[i].val=w[i],a[i].jilu=i; -Sort (A +1, a+n+1, CMP); in /*for (int i=1;i<=n;i++) { the printf ("%d", a[i].jilu); the } About printf ("\ n");*/ the } the theInlinevoidDfsintXintNow ) { +vis[now]=1; -mp[x][now]=1; the for(intI=first[now];i;i=Next[i]) {Bayi intv=To[i]; the if(!Vis[v]) { the DFS (X,V); - } - } the } the theInlinevoidGointx) { the intj=N; - for(inti=n;i>=1; i--){ the intv=A[i].jilu; the if(mp[x][v]==0&& w[v]>=j) j--; the Else if(W[V]<J) Break;94 } theprintf"%d", j); the } the 98InlinevoidWork () { AboutN=getint (); m=getint (); - for(intI=1; i<=n;i++) w[i]=getint ();101 intx, y;102 for(intI=1; i<=m;i++) {103X=getint (); y=getint ();104 link (y,x); the }106 for(intI=1; i<=n;i++)if(!Vis[i]) topo_sort (i);107 Make ();108 for(intI=1; i<=n;i++) memset (Vis,0,sizeof(Vis)), DFS (i,i);109 for(intI=1; i<=n;i++) Go (i); the }111 the intMain ()113 { the Work (); the return 0; the}
BZOJ2109 [Noi2010]plane Aviation control