CodeForces - 732F Tourist Reform
Discription
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 ≤ n, uj ≠ 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
7 9
4 3
2 6
7 1
4 1
7 3
3 5
7 4
6 5
2 5
4
4 3
6 2
7 1
1 4
3 7
5 3
7 4
5 6
2 5
首先可以发现,把每个双联通分量内部的边定向之后使得这个联通分量变成强联通肯定是最优的,那么就只剩下桥了hhhh
现在图已经可以缩成一个无根树了,显然把边定向之后,至少会有一个点没有出度,并且最少的肯定是没有出度的点之一,所以我们就强行把强联通分量里点最多的联通分量作为根,把树边(也就是桥)的朝向定为从儿子到爸爸,这样就行了。
也就是我们可以直接从最大的联通分量的一个点开始随便dfs,不是桥的话最后边的朝向就是dfs时候的朝向,否则反之。
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=400005; int to[maxn*2],ne[maxn*2],hd[maxn],K,TT; int n,m,dfn[maxn],low[maxn],dc,linenum=1,pos,an; bool ban[maxn*2],ans[maxn*2],col[maxn]; inline int read(){ int x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x; } inline void add(int x,int y){ to[++linenum]=y,ne[linenum]=hd[x],hd[x]=linenum;} void dfs(int x,int fa){ dfn[x]=low[x]=++dc; for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa) if(!dfn[to[i]]){ dfs(to[i],x),low[x]=min(low[x],low[to[i]]); if(low[to[i]]>dfn[x]) ban[i]=ban[i^1]=1; } else low[x]=min(low[x],dfn[to[i]]); } void Search(int x){ TT++,col[x]=1; for(int i=hd[x];i;i=ne[i]) if(!col[to[i]]&&!ban[i]) Search(to[i]); } void TOR(int x){ col[x]=1; for(int i=hd[x];i;i=ne[i]) if(!ans[i^1]){ ans[i]=1; if(!col[to[i]]) TOR(to[i]); } } int main(){ n=read(),m=read(); int uu,vv; for(int i=1;i<=m;i++){ uu=read(),vv=read(); add(vv,uu),add(uu,vv); } dfs(1,-1); for(int i=1;i<=n;i++) if(!col[i]){ TT=0,Search(i); if(TT>an) an=TT,pos=i; } memset(col,0,sizeof(col)); TOR(pos); printf("%d\n",an); for(int i=2;i<=linenum;i++) if(ans[i]) printf("%d %d\n",to[i^1^ban[i]],to[i^ban[i]]); return 0; }