codeforces CF732F Tourist Reform Tarjan边双连通分量

$ ightarrow $ 戳我进CF原题

F. 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 $ r_i $ 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,
$ r_i $ 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 $ r_i $ .
 
Help the government of Berland make the reform to maximize the minimum of $ r_i $ .
 

Input

The first line contains two integers $ n, m (2 le n le 400 000, 1 le m le 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 $ u_j $ and $ v_j (1 le u_j, v_j le n, u_j  ot= v_j) $ ,
where $ u_j $ and $ v_j $ 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 $ min_{1 le i le 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 $ u_j, v_j $ ,
it means that the $ j $ -th road will be directed from the city $ u_j $ to the city $ v_j $ .
Print roads in the same order as they are given in the input data.
 

Examples

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

 

题目大意

  • 一张有向图中,设 $ r_i $ 为从点 $ i $ 出发能够到达的点的数量。

  • 定义有向图的“改良值”为 $ r_i $ 的最小值。

  • 现给出一张无向图,要求给每条边定一个方向,使产生的有向图“改良值”最大。

  • $ n,m le 400000 $
     

题解

  • 对于无向图的每个“边双连通分量”,一定存在一种定向方法,使其改良值等于其大小

  • 把无向图缩点后,以最大的 $ e-DCC $ 为零出度点(终点) $ BFS $ 定向

  • 每个 $ e-DCC $ 内部 $ DFS $ 定向

这个定向其实就是瞎定,原题spj会看最大的强连通分量大小而已,不必在意和答案不一样
 

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define maxn 400005
#define PP pair<int,int>
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
vector<PP>e[maxn];
stack<int>s;
int low[maxn],dfn[maxn],tim,mxsz,rt;
bool vis[maxn];
int scc,bel[maxn];
void tarjan(int u,int fa){
	low[u]=dfn[u]=++tim; s.push(u); vis[u]=1;
	for(int i=0;i<e[u].size();++i){
		int v=e[u][i].fi;
		if(v==fa) continue;
		if(!dfn[v]){
			tarjan(v,u);
			low[u]=min(low[v],low[u]);
		} else if(vis[v])
			low[u]=min(dfn[v],low[u]);
	}
	if(low[u]==dfn[u]){
		int sz=0,x=0; ++scc;
		do{
			++sz;
			x=s.top(); s.pop(); vis[x]=0;
			bel[x]=scc;
		}while(low[x]!=dfn[x]);
		if(sz>mxsz){ mxsz=sz; rt=u; }
	}
}
int fu[maxn],fv[maxn];
void dfs(int u){
	dfn[u]=0;
	//使用dfn数组代替vis,省掉memset的时间 
	for(int i=0;i<e[u].size();++i){
		int v=e[u][i].fi,id=e[u][i].se;
		if(dfn[v]){
			if(bel[v]!=bel[u]){ fu[id]=v; fv[id]=u; }
			//当两点不属于一个连通分量,让边从另一边拉过来 
			else{ fu[id]=u; fv[id]=v; }
			//否则就是连通分量上的点,顺势从u到v 
			dfs(v);
		} else {
			fu[id]=u; fv[id]=v;
			//这种情况就是连通分量的终点,依旧从u到v 
		}
	}
}
int n,m;	
int main(){
	scanf("%d %d",&n,&m);
	for(int u,v,i=1;i<=m;++i){
		scanf("%d %d",&u,&v);
		e[u].push_back(mp(v,i));
		e[v].push_back(mp(u,i));
	}
	tarjan(1,0);
	printf("%d
",mxsz);
	//这里我直接dfs定向了 
	dfs(rt);
	for(int i=1;i<=m;++i)
		printf("%d %d
",fu[i],fv[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/PotremZ/p/CF732F.html