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

9 篇文章 0 订阅

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

Time Limit: 30 Sec   Memory Limit: 162 MB
Submit: 2917   Solved: 1151
[ 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

Source

[ Submit][ Status][ Discuss]



根据题中的概念,一张有向图中的任意边双都是半连通子图

不妨先把所有边双缩起来。。这样剩下一个DAG

等于是要找DAG上的权值最大链,以及求这样的链的方案数

直接dp就行了

不过要注意,,新图每两个连通块之间最多只能剩一条边,否则是不符合题目的定义的

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

const int maxn = 1E5 + 10;

int n,m,p,dfs_clock,Ans,tot,cnt,Cnt,du[maxn],bel[maxn],vis[maxn]
	,dfn[maxn],low[maxn],siz[maxn],Max[maxn],ans[maxn];

vector <int> v[maxn],v2[maxn];
queue <int> Q;
stack <int> s;

int Add(const int &x,const int &y) {return (x + y) % p;}

void Dfs(int x)
{
	dfn[x] = low[x] = ++dfs_clock; s.push(x);
	for (int i = 0; i < v[x].size(); i++)
	{
		int to = v[x][i];
		if (!dfn[to]) Dfs(to),low[x] = min(low[x],low[to]);
		else if (!bel[to]) low[x] = min(low[x],dfn[to]);
	}
	
	if (dfn[x] == low[x])
	{
		++cnt;
		for (;;)
		{
			int tp = s.top(); s.pop();
			bel[tp] = cnt; ++siz[cnt];
			if (tp == x) break;
		}
	}
}

int getint()
{
	char ch = getchar(); int ret = 0;
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9')
		ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	n = getint(); m = getint(); p = getint();
	while (m--)
	{
		int x = getint(),y = getint();
		v[x].push_back(y);
	}
	for (int i = 1; i <= n; i++)
		if (!dfn[i]) Dfs(i);
	
	for (int i = 1; i <= n; i++)
		for (int j = 0; j < v[i].size(); j++)
		{
			int x = bel[i],y = bel[v[i][j]];
			if (x == y) continue;
			v2[x].push_back(y); ++du[y];
		}
	for (int i = 1; i <= cnt; i++)
		if (!du[i]) Q.push(i),ans[i] = 1;
	
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop(); 
		Max[k] += siz[k]; ++Cnt;
		for (int i = 0; i < v2[k].size(); i++)
		{
			int to = v2[k][i]; --du[to];
			if (vis[to] != Cnt)
			{
				if (Max[to] < Max[k]) Max[to] = Max[k],ans[to] = ans[k];
				else if (Max[to] == Max[k]) ans[to] = Add(ans[to],ans[k]);
				vis[to] = Cnt;
			}
			if (!du[to]) Q.push(to);
		}
	}
	for (int i = 1; i <= cnt; i++)
		if (Max[i] > Ans) Ans = Max[i],tot = ans[i];
		else if (Max[i] == Ans) tot = Add(tot,ans[i]);
	cout << Ans << endl << tot;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值