bzoj3355[usaco2004 Jan] Ordered cows
Test instructions
John's N cows were lined up in a row, with a definite order. He had a message about the order of the cows, and all the information was written in the form "A in front of B". Please help John remove as much redundant information as possible, but be sure to roll out the original order. n≤1500.
Exercises
First, the topology is sorted, and each node is set to a Bitset store which points can reach this point. And then by the end of the topological sequence ascending to the first keyword, the beginning of the topological sequence descending to the second keyword in order to insert the edge, if the current end of the Bitset in the beginning is not 1 the edge is retained, and the end of the Bitset and the beginning of the bitset merge, otherwise the edge is not preserved.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5#include <bitset>6 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 #defineMAXN 15108 using namespacestd;9 TenInlineintRead () { One CharCh=getchar ();intf=1, x=0; A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; the } - structe{intF,t,n;} es[maxn* -],es2[maxn* -];intG[maxn],tot; - voidPeintFintTintESS) {es[ess]= (e) {f,t,g[f]}; g[f]=ess;} - intN,L,TOPO[MAXN],DU[MAXN]; + BOOLCMP1 (e a,e b) {returntopo[a.t]==topo[b.t]?topo[a.f]>topo[b.f]:topo[a.t]<topo[b.t];} - BOOLCMP2 (e a,e b) {returna.f==b.f?a.t<b.t:a.f<B.f;} +queue<int>q; Bitset<maxn>F[MAXN]; A intMain () { atN=read (); L=read (); Inc (I,1, l) {intX=read (), Y=read (); PE (X,Y,I); du[y]++;} Inc (I,1, N)if(!Du[i]) Q.push (i); - while(!Q.empty ()) { - intx=Q.front (); Q.pop (); - for(intI=g[x];i;i=ES[I].N) { -du[es[i].t]--;if(!du[es[i].t]) Q.push (es[i].t), topo[es[i].t]=topo[x]+1; - } in } -Sort (es+1, es+l+1, CMP1); Inc (I,1, N) f[i][i]=1; toInc (I,1, l) {if(!F[ES[I].T][ES[I].F]) es2[++tot]=es[i]; f[es[i].t]|=f[es[i].f];} +Sort (es2+1, es2+tot+1, CMP2); printf"%d\n", tot); -Inc (I,1, tot) printf ("%d%d\n", es2[i].f,es2[i].t);return 0; the}
20161024
bzoj3355[usaco2004 Jan] Ordered cows *