Test instructions: There are n points, M-edge of the graph, need to divide these points into several blocks, requires: if there are paths between two points can be reached, then the two points must be divided into the same block, any two points within the same piece of each other at least one path to reach, that is, u to V or V to reach U ; Each point can only exist within a single block. Ask for the minimum number of blocks to divide.
First of all, if you can reach each other between two points must be in the same piece, in fact, in the same strong connected component of the point must be in the same block, so the first is strongly connected to the contraction point. Then there is a path between any two points in the same piece, so it is actually a strong connected component within a piece, at least one route through all components. And all the strong connected components on this path can form the same piece. So in fact, we just need to find the least of such a path to cover all the points all over again, is to do the smallest path coverage.
Minimum path overrides = Points-the maximum number of matches after the break.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=10005;8 Const intmaxm=2e5+5;9 Ten inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2]; One intn,t,scccnt; A intSTX[MAXN],LOW[MAXN],SCC[MAXN]; - intVIS[MAXN],MATCH[MAXN]; -stack<int>S; the - voidinit () { -memset (head,-1,sizeof(head)); -size[0]=size[1]=0; + } - + voidAddintAintBintC=0){ Apoint[c][size[c]]=b; atnxt[c][size[c]]=Head[c][a]; -head[c][a]=size[c]++; - } - - voidDfsints) { -stx[s]=low[s]=++T; in S.push (S); - for(inti=head[0][s];~i;i=nxt[0][i]) { to intj=point[0][i]; + if(!Stx[j]) { - Dfs (j); thelow[s]=min (low[s],low[j]); * } $ Else if(!Scc[j]) {Panax Notoginsenglow[s]=min (low[s],stx[j]); - } the } + if(low[s]==Stx[s]) { Ascccnt++; the while(1){ + intu=S.top (); S.pop (); -scc[u]=scccnt; $ if(S==u) Break; $ } - } - } the - voidSETSCC () {Wuyimemset (STX,0,sizeof(STX)); thememset (SCC,0,sizeof(SCC)); -T=scccnt=0; Wu for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); - for(intI=1; i<=n;++i) { About for(intj=head[0][i];~j;j=nxt[0][j]) { $ intk=point[0][j]; - if(scc[i]!=Scc[k]) { -Add (scc[i],scc[k]+scccnt,1); - } A } + } the } - $ intDFS1 (intk) { the for(inti=head[1][k];~i;i=nxt[1][i]) { the if(!vis[point[1][i]]) { the intp=point[1][i]; thevis[p]=1; - if(match[p]==-1||DFS1 (Match[p])) { inmatch[p]=K; the return 1; the } About } the } the return 0; the } + - the intMain () {Bayi intT; thescanf"%d",&T); the while(t--){ - intm; -scanf"%d%d",&n,&m); the init (); the while(m--){ the intb; thescanf"%d%d",&a,&b); - Add (A, b); the } the SETSCC (); the intans=0;94memset (match,-1,sizeof(Match)); the for(intI=1; i<=2*scccnt;++i) { thememset (Vis,0,sizeof(Vis)); the if(DFS1 (i) = =1) ans++;98 } Aboutprintf"%d\n", scccnt-ans); - }101 return 0;102}
View Code
hdu3861 strong connectivity + Minimum path coverage