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

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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值