[bzoj1093][ZJOI2007]最大半连通子图 Tarjan,DP

xiaoxiao2021-02-28  104

1093: [ZJOI2007]最大半连通子图

Time Limit: 30 Sec   Memory Limit: 162 MB [ Submit][ Status][ Discuss]

Description

  一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路径。若G'=(V',E')满足V'?V,E'是E中所有跟V'有关的边, 则称G'是G的一个导出子图。若G'是G的导出子图,且G'半连通,则称G'为G的半连通子图。若G'是G所有半连通子图 中包含节点数最多的,则称G'是G的最大半连通子图。给定一个有向图G,请求出G的最大半连通子图拥有的节点数K ,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。

Input

  第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整 数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤1 00000, M ≤1000000;对于100%的数据, X ≤10^8

Output

  应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.

Sample Input

6 6 20070603 1 2 2 1 1 3 2 4 5 6 6 4

Sample Output

3 3

HINT

缩点以后,最大半连通子图明显是一条链,再跑一遍拓扑序即可 #include<iostream> #include<cstdio> using namespace std; const int N = 100005; int n,m,X,cnt,scnt,top,ans,mx,id; int last[N],las[N],dfn[N],inq[N],low[N],hav[N],bel[N],q[N],f[N],in[N],g[N],fa[N]; struct Edge{ int to,next; }e[N*20],ee[N*20]; void insert( int u, int v ){e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;} void ins( int u, int v ){ee[++cnt].to = v; ee[cnt].next = las[u]; las[u] = cnt; in[v]++;} void tarjan( int x ){ dfn[x] = low[x] = ++id; int now=0;; q[++top] = x; inq[x] = 1; for( int i = last[x]; i; i = e[i].next ) if( !dfn[e[i].to] ) tarjan(e[i].to), low[x] = min(low[x],low[e[i].to]); else if( inq[e[i].to] ) low[x] = min(low[x],dfn[e[i].to]); if( low[x] == dfn[x] ){ scnt++; while( now != x ){ now = q[top--]; inq[now] = 0; hav[scnt]++; bel[now] = scnt; } } } void rebuild(){ cnt = 0; for( int i = 1; i <= n; i++ ) for( int j = last[i]; j; j = e[j].next ) if( bel[i] != bel[e[j].to] ) ins(bel[i],bel[e[j].to]); } void bfs(){ int head = 0, tail = 0; for( int i = 1; i <= scnt; i++ ){ if( !in[i] ) q[tail++] = i; f[i] = hav[i]; g[i] = 1; } while( head != tail ){ int now = q[head++]; for( int i = las[now]; i; i = ee[i].next ){ if( (--in[ee[i].to]) == 0 ) q[tail++] = ee[i].to; if( fa[ee[i].to] == now ) continue; if( f[now] + hav[ee[i].to] > f[ee[i].to] ){ f[ee[i].to] = f[now] + hav[ee[i].to]; g[ee[i].to] = g[now]; } else if( f[now] + hav[ee[i].to] == f[ee[i].to] ) (g[ee[i].to] += g[now] ) %= X; fa[ee[i].to] = now; } } } int main(){ scanf("%d%d%d", &n, &m, &X); for( int i = 1,u,v; i <= m; i++ ){ scanf("%d%d", &u, &v); insert(u,v); } for( int i = 1; i <= n; i++ ) if( !dfn[i] ) tarjan(i); rebuild(); bfs(); for( int i = 1; i <= scnt; i++ ){ if( f[i] > mx ) mx = f[i], ans = g[i]; else if( f[i] == mx ) (ans += g[i]) %= X; } printf("%d\n%d", mx, ans); return 0; }
转载请注明原文地址: https://www.6miu.com/read-30154.html

最新回复(0)