Bzoj 1093: [ZJOI2007] Maximum semi-connected sub-diagram topological diagram DP Tarjan

Source: Internet
Author: User
Tags time limit

1093: [ZJOI2007] Maximum semi-connected sub-graph Time limit:30 Sec Memory limit:162 MB
submit:3240 solved:1278
[Submit] [Status] [Discuss] Description

A 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 in the graph, there is a forward path from U to V or a forward path from V to U. If g ' = (v ', E ') satisfies V '? V,e ' is all the sides in E that are related to V ', then G ' is an export sub-diagram 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 the semi-connected sub-graphs contain the most nodes, then the G ' is the largest semi-connected sub-graph of G. Given a graph G, the maximum semi-connected sub-graph of G is requested to have the number of nodes 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 respectively represents the number of points and sides of the graph G, the meaning of x as described above next m, each line two positive integers a, B, represents 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≤100000, 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 Input 6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4 Sample Output 3
3

After reading the question, it may be the following reasons.

If g ' = (v ', E ') satisfies V '? V,e ' is all the sides in E that are related to V ', then G ' is an export sub-diagram of G.

The point of this sentence is that the derivation of the sub-graph points plays a decisive role

So, don't think about the number of scenarios through a strong connected component.



Think of Tarjan

Graph into Dag

The best result is the longest chain

That's half the problem here.



How many programs do you ask?

Topology for hierarchical issues

Process a node before processing the node.

Elimination of the aftereffect, it can be in the extension tree DP

For a strongly connected component

Maintenance of best practices, best practices



Oh, right.

Note the heavy edges after handling rebuild


#include <cmath> #include <ctime> #include <cstdio> #include <cstring> #include <cstdlib> #include <complex> #include <iostream> #include <algorithm> #include <iomanip> #include < vector> #include <string> #include <bitset> #include <queue> #include <map> #include <set
> Using namespace std;
typedef long Long LL;
	inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch<= ' 9 ' &&ch>= ' 0 ') {x= (x<<1) + (x<<3) +ch-' 0 '; Ch=getchar ();}
return x*f;
	} inline void print (int x) {if (x<0) Putchar ('-'), x=-x;
if (x>=10) print (X/10);p Utchar (' 0 ' +x%10);
} const int m=1001000;
const int n=100100;
int mod,n,m,ecnt,ecnt2,last[n],last2[n];
int DFN[N],LOW[N],S[N],BEL[N],SIZE[N],TOP,CNT,SCC;
BOOL Ins[n]; struct Edge{int to,nt;}
E[M],E2[M]; inline void Add (int u,int v) {e2[++ecnt2]= (EDGE) {v,last2[u]};last2[u]=ecnt2;} inline void readd (int u,int v) {e[++ecnt]= (EDGE) {v,last[u]};last[u]=ecnt;}
	void Tarjan (int u) {dfn[u]=low[u]=++cnt;ins[u]=1;s[++top]=u;
		for (int i=last2[u];i;i=e2[i].nt) {if (!dfn[e2[i].to]) {Tarjan (e2[i].to); Low[u]=min (low[u],low[e2[i].to]);}
	else if (Ins[e2[i].to]&&dfn[e2[i].to]<low[u]) low[u]=dfn[e2[i].to];
		} if (Dfn[u]==low[u]) {Scc++;int tmp=0; do{tmp=s[top--];ins[tmp]=0;bel[tmp]=scc;size[scc]++;}
	while (TMP!=U);
}} int k,c,q[n],ind[n],f[n],g[n];
	void topo () {int head=0,tail=0,ti=1;static int mk[n];
	for (int i=1;i<=scc;i++) {if (!ind[i]) q[tail++]=i;f[i]=size[i],g[i]=1;}
		while (head!=tail) {int u=q[head++];head%=n;
			for (int i=last[u];i;i=e[i].nt) {ind[e[i].to]--;
			if (!ind[e[i].to]) q[tail++]=e[i].to,tail%=n;
			if (mk[e[i].to]==u) continue;
				if (F[u]+size[e[i].to]>f[e[i].to]) {f[e[i].to]=f[u]+size[e[i].to];
			G[e[i].to]=g[u];
			} else if (F[u]+size[e[i].to]==f[e[i].to]) (G[e[i].to]+=g[u])%=mod;
		Mk[e[i].to]=u; }}} int main () {n=read (); M=read (); mod=rEAD (); int ti=0;
	for (int i=1,u,v;i<=m;i++) {u=read (); V=read (); Add (u,v);}
	for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i); for (int i=1;i<=n;i++) {for (int j=last2[i];j;j=e2[j].nt) if (bel[i]!=bel[e2[j].to]) {readd (bel[i],bel[e2[j].to]);
	ind[bel[e2[j].to]]++;}
	} topo ();
		for (int i=1;i<=scc;i++) {if (f[i]==k) (c+=g[i])%=mod;
	if (f[i]>k) k=f[i],c=g[i];
	Print (k);p UTS ("");p rint (c);p UTS ("");
return 0; }/* 6 6 20070603 1 2 2 1 1 3 2 4 5 6 6 4 3 3 * *


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.