"Tarjan topology sort DP" bzoj1093: [ZJOI2007] Maximum semi-connected sub-graph

Source: Internet
Author: User

Difficulty in thinking, key test code to achieve the ability. Some of the details are wonderful.

Description

A undirected graph g= (v,e) is called a semi-connected (semi-connected), if satisfied: U,v∈v, satisfies u→v or v→u, that is, for any
Two point u,v, there is a forward path from U to V or a path from V to U. If g ' = (v ', E ') satisfies V '? V,e ' is all the side of E in relation to V ',
is called G ' is an export sub-chart of G. If G ' is the derived sub-graph of G, and G ' is semi-connected, then G ' is a semi-connected sub-graph of G. If G ' is g all semi-connected sub-graphs
Contains the largest number of nodes, the "G" is the maximum half-connected sub-graph of G. Given a graph G, the maximum number of nodes that a request out of G is owned by the largest semi-connected sub-graph K
, and the number of different maximum semi-connected sub-graphs C. Because C can be larger, only the remainder of the output C to X is required.

Input

The first line consists of two integer n,m,x. N,m represents the number of points and sides of the graph G, and the meaning of x is the next M-line, two positive integers per line, as described above
Number A, B, indicates a forward edge (A, B). Each point in the figure is numbered ... N, ensure that the same (a, b) in the input does not occur two times. N≤1
00000, m≤1000000; for 100% of data, x≤10^8

Output

Should contain two lines, the first line containing an integer k. The second line contains the integer c Mod X.

Sample Input6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4
Sample Output3
3
Problem analysis

First of all, to analyze the topic of the constraints on the exact description of a thing.

Semi-connected sub-graph?

At first glance it seems like a "semi-connected sub-graph" is a very troublesome thing, but obviously a ring is definitely a semi-connected sub-graph, so we can shrink the point first.

After the point is reduced to become a DAG, then a few more pictures will find that if a sub-graph is a semi-connected sub-graph, it must be a chain. This is actually quite obvious, there is no formal language to describe.

Therefore, the maximum semi-connected sub-graph becomes the request: After the contraction point, we should ask for the longest chain with the point right to the graph.

Number of programmes

It is easy to find the longest chain in a DAG, either with a memory search or a topological sort. The number of solutions, that is, similar to the DP method, using $g[i]$ to express the longest chain of $i$ as the end of the program number, transfer is also very convenient.

Yes, in order to count the number of scenarios, the connecting edges between the blocks need to be weighed.

From the Huang blog to learn a very clever way to go heavy (although it is easy to know, but it is very good), that is, for $ (u,v) $, each time the operation of the record $vis[v]=u$, if encountered $vis[v]=u$ exit.

So it becomes: tarjan+ topological +DP Board summary problem

I have written before the Tarjan board set up, so a bit long ... It's 3k.

1#include <bits/stdc++.h>2typedefLong Longll;3 Const intMAXN =100035;4 Const intMAXM =1200035;5 6 intn,m;7 intDeg[maxn],vis[maxn],q[maxn],qhead,qtail;8 intHead[maxn],nxt[maxm],edges[maxm],edgetot;9 ll P,COL[MAXN],COLS,SIZE[MAXN],F[MAXN],G[MAXN];Ten ll mx,cnt; One  A intRead () - { -     CharCH =GetChar (); the     intnum =0; -     BOOLFL =0; -      for(;!isdigit (ch); ch =GetChar ()) -         if(ch=='-') FL =1; +      for(; isdigit (ch); ch =GetChar ()) -num = (num<<1) + (num<<3) +ch- -; +     if(fl) num =-num; A     returnnum; at } - namespaceTarjanspace - { -     intstk[maxn],cnt; -     intA[maxn],dfn[maxn],low[maxn],tim; -     intEDGETOT,EDGES[MAXM],NXT[MAXM],HEAD[MAXN]; in     voidTarjan (intNow ) -     { toDfn[now] = Low[now] = ++tim, stk[++cnt] =Now ; +          for(intI=head[now]; i!=-1; I=Nxt[i]) -         { the             intv =Edges[i]; *             if(!Dfn[v]) $Tarjan (v), low[now] =std::min (Low[now], low[v]);Panax Notoginseng             Else if(!Col[v]) -Low[now] =std::min (Low[now], dfn[v]); the         } +         if(low[now]==Dfn[now]) A         { the:: Col[now] = ++::cols,:: size[cols] =1; +              for(; stk[cnt]!=now; CNT--,:: size[cols]++) -:: col[stk[cnt]] =:: cols; $cnt--; $         } -     } -InlinevoidAddedgeinner (intUintv) the     { -Edges[++edgetot] = V, nxt[edgetot] = Head[u], head[u] =Edgetot;Wuyi     } theInlinevoidAddedgeouter (intUintv) -     { Wudeg[v]++,:: Edges[++::edgetot] = V,:: Nxt[::edgetot] =:: Head[u],:: head[u] =:: Edgetot; -     } About     voidDealouter () $     { -          for(intI=1; i<=n; i++) -         { -             intU =Col[i]; A              for(intJ=head[i]; j!=-1; j=Nxt[j]) +                 if(u!=Col[edges[j]]) the addedgeouter (U, Col[edges[j]]); -         } $cols--; the     } the     voidSolve () the     { theMemset (Head,-1,sizeofhead); -CNT = Tim = Edgetot =0; in          for(intI=1; i<=n; i++) Addedgeinner (0, i); the          for(intI=1; i<=m; i++) the         { About             intU = Read (), V =read (); the addedgeinner (U, v); the         } theTarjan (0); + Dealouter (); -     } the }Bayi voidToposort () the { theQhead =0, Qtail =0; -      for(intI=1; i<=cols; i++) -     { the         if(!deg[i]) q[++qtail] =i; theF[i] = Size[i], g[i] =1; the     } the      while(qhead!=qtail) -     { the         intU = q[++Qhead]; the          for(intI=head[u]; i!=-1; I=Nxt[i]) the         {94             intv =Edges[i]; the             if((--deg[v]) = =0) Q[++qtail] =v; the             if(Vis[v]==u)Continue; the             if(F[v] < f[u]+Size[v])98F[V] = F[u]+size[v], g[v] =G[u]; About             Else if(f[v]==f[u]+Size[v]) -G[V] = (G[v]+g[u])%p;101VIS[V] =u;102         }103     }104 } the intMain ()106 {107Memset (Head,-1,sizeofhead);108n = Read (), M = Read (), p =read ();109 tarjanspace::solve (); the Toposort ();111      for(intI=1; i<=cols; i++) the         if(F[i] >mx)113mx = f[i], cnt =G[i]; the         Else if(f[i]==mx) theCNT = (Cnt+g[i])%p; theprintf"%lld\n%lld\n", mx,cnt);117     return 0;118}

END

"Tarjan topology sort DP" bzoj1093: [ZJOI2007] Maximum semi-connected sub-graph

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.