Codeforces 732F tarjan

本文探讨了如何通过合理规划无向图转换为有向图的方式,使每个节点可达节点数的最小值最大化,以此来提高虚拟国家Berland的旅游吸引力。文章提出了一种基于强连通分量的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tourist Reform
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland is a tourist country! At least, it can become such — the government of Berland is confident about this.

There are n cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is possible to get from any city to any other city using given two-ways roads.

According to the reform each road will become one-way. It will be oriented to one of two directions.

To maximize the tourist attraction of Berland, after the reform for each city i the value ri will be calculated. It will equal to the number of cities x for which there is an oriented path from the city i to the city x. In other words, ri will equal the number of cities which can be reached from the city i by roads.

The government is sure that tourist's attention will be focused on the minimum value of ri.

Help the government of Berland make the reform to maximize the minimum of ri.

Input

The first line contains two integers n, m (2 ≤ n ≤ 400 000, 1 ≤ m ≤ 400 000) — the number of cities and the number of roads.

The next m lines describe roads in Berland: the j-th of them contains two integers uj and vj (1 ≤ uj, vj ≤ nuj ≠ vj), where uj and vj are the numbers of cities which are connected by the j-th road.

The cities are numbered from 1 to n. It is guaranteed that it is possible to get from any city to any other by following two-ways roads. In Berland there are no roads which connect the same pair of cities.

Output

In the first line print single integer — the maximum possible value min1 ≤ i ≤ n{ri} after the orientation of roads.

The next m lines must contain the description of roads after the orientation: the j-th of them must contain two integers uj, vj, it means that the j-th road will be directed from the city uj to the city vj. Print roads in the same order as they are given in the input data.

Example
input
7 9
4 3
2 6
7 1
4 1
7 3
3 5
7 4
6 5
2 5
output
4
4 3
6 2
7 1
1 4
3 7
5 3
7 4
5 6
2 5


题意:给你一个无向图,现在让你转化为有向图,也就是给每条边赋方向,使得每个点能到达的点数的最小值最大,求出这个值,然后输出路的方向。


题解:对于一个强联通分量,里面的点是可以相互到达的,对于两个相邻的强连通分量,设大小分别为x y,x>y,则如果x指向y,那么ans为y,否则答案为x,所以可得应该把小的分量指向大的分量,所以ans即为max(bcc i),也就是强连通分量最大的那个值。此时其它的联通分量方向都指向这个最大的。

边的方向:对于一个强连通分量,随便找个点,然后dfs出去,每次都是to指向from,即可构成环,所以我们可以从最大的那个强联通分量dfs出去,这样就可以使得所有联通分量指向这个最大的。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int head[400005],cnt=0,n,m,bccnum,ef[800005],st[800005],index,top,dfn[800005],low[800005],bcc[800005],num[800005],now,ans,vis[800005];
struct node{
	int from,to,nex;
}edge[800005];
void add(int u,int v){
	edge[cnt].to=v;
	edge[cnt].from=u;
	edge[cnt].nex=head[u];
	head[u]=cnt++;
}
void tarjan(int root){//求强连通分量
    dfn[root]=low[root]=++index;
    st[++top]=root; 
    for(int i=head[root];~i;i=edge[i].nex){
        int v=edge[i].to;
        if(ef[i])continue;
        ef[i]=ef[i^1]=1;
        if(!dfn[v]){
            tarjan(v);
            low[root]=min(low[root],low[v]);
        }
        else low[root]=min(low[root],dfn[v]);
    }
    if(low[root]==dfn[root]){
        bccnum++;
        for(;;){
            int x=st[top--];
            bcc[x]=bccnum;
            num[bccnum]++;
            if(x==root)break;
        }
        if(num[bccnum]>ans){
        	now=root;//定义now为起始dfs点
			ans=max(ans,num[bccnum]);
        }
    }
}
void dfs(int root){
	if(vis[root])return ;//不加剪枝会t
	vis[root]=1;
	for(int i=head[root];~i;i=edge[i].nex){
		int v=edge[i].to;
		if(ef[i])continue;
		ef[i]=1;//代表走过
		ef[i^1]=2;//代表to指向from
		dfs(v);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	int x,y,i,j;
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	tarjan(1);
	memset(ef,0,sizeof(ef));
	dfs(now);
	printf("%d\n",ans);	
	for(i=0;i<cnt;i++){
		if(ef[i]==2)printf("%d %d\n",edge[i].from,edge[i].to);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值