codeforces round 377 div2 F Tourist Reform tarjan求邊雙連通分量

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


題目描述:給出一個有n(0 < n < 4e5)個點和m(0 < m < 4e5)條邊的連通的無重邊無向圖,現在給每條邊規定一個方向,圖中所有邊都有了方向之後,ri表示點i所能直接或間接到達的點的數量,要求給出一種規定方向的方式,使得所有ri中的最小值最大。


題目思路:規定方向後,應該使得儘量多的點相互可達,而每一個邊雙連通分量,就是在規定方向之後能夠變爲強連通分量的最大點集。因此,首先要將所有的邊雙連通分量都求出來,然後在每個邊雙連通分量內部dfs一次將邊雙連通分量變爲強連通分量。將一個邊雙連通分量看作一個節點的話,圖中剩下的就是一棵樹,找到包含點的個數最多的邊雙連通分量,以他所在的節點作爲根,dfs這棵樹,讓所有的樹邊都指向父親節點,這樣標記方向就滿足題意了。在這種構圖下ri的最小值就是點數最多的邊雙連通分量中所包含的點的個數。


收穫:原來這就叫tarjan縮點...

#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 4e5 + 5;
struct Edge
{
    int u , v , is;
    Edge(int u = 0 , int v = 0 , int is = 0) : u(u) , v(v) , is(is){}
}e[maxn * 3];
int  n , m , ecnt , stamp , dfn[maxn] , low[maxn] , bccno[maxn] ,num[maxn] ,  bcc_cnt ;
vector<int>vec[maxn] , bcc[maxn];
vector<Edge>ans;
bool isbridge[maxn * 2] , vis[maxn * 2];
void add_edge(int u , int v)
{
    Edge e1 = Edge(u , v);
    e[ecnt++] = e1;
    vec[u].push_back(ecnt - 1);
    Edge e2 = Edge(v , u);
    e[ecnt++] = e2;
    vec[v].push_back(ecnt - 1);
}
void tarjan(int index , int fa)
{
    int tmp ;
    dfn[index] = low[index] = ++stamp;
    for(int i = 0 ; i < vec[index].size() ; i++){
        tmp = e[vec[index][i]].v;
        if(!dfn[tmp]){
            tarjan(tmp , index);
            low[index] = min(low[index] , low[tmp]);
            if(low[tmp] > dfn[index]){
                isbridge[vec[index][i]] = isbridge[vec[index][i] ^ 1] = 1;
            }
        }
        else if(dfn[tmp] < dfn[index] && tmp != fa){
            low[index] = min(low[index] , dfn[tmp]);
        }
    }
}

void dfs(int index)
{
    dfn[index] = 1;
    bccno[index] = bcc_cnt;
    ++num[bcc_cnt];
    for(int i = 0 ; i < vec[index].size() ; i++){
        int tmp = vec[index][i];
        if(isbridge[tmp])       continue;
        if(!dfn[e[tmp].v])
            dfs(e[tmp].v);
    }
}

void find_ebcc()
{
    bcc_cnt = stamp = 0;
    mem(dfn , 0);
    mem(low , 0);
    mem(isbridge , 0);
    mem(bccno , 0);
    mem(bcc , 0);
    for(int i = 1 ; i <= n ; i++){
        if(!dfn[i])
            tarjan(i , -1);
    }
    mem(dfn , 0);
    for(int i = 1 ; i<= n ; i++){
        if(!dfn[i]){
            bcc_cnt ++;
            dfs(i);
        }
    }
}

void dfs2(int cur)      //點可以重複,邊不能重複,每條邊被訪問兩次
{
    for(int i = 0 ; i< vec[cur].size() ; i++){
        int tmp = vec[cur][i];
        if(vis[tmp])        continue;
        Edge &ed = e[vec[cur][i]];
        int nt = ed.v;
        if(bccno[nt] != bccno[cur])     continue;
        ed.is = 1;
        vis[tmp ^ 1] = vis[tmp] = 1;
        dfs2(nt);
    }
}

void dfs3(int cur)
{
    dfn[cur] = 1;
    for(int i = 0 ; i < vec[cur].size() ; i++){
        int tmp = vec[cur][i];
        Edge ed = e[tmp];
        int nt = ed.v;
        if(dfn[nt])     continue;
        if(isbridge[tmp]){
            e[tmp ^ 1].is = 1;
        }
        dfs3(nt);
    }
}
int main()
{
    int u , v , res = -1 , max_id;
    ecnt = 0;
    scanf("%d %d" , &n , &m);
    for(int i = 1 ; i <= m ; i++){
        scanf("%d %d" , &u , &v);
        add_edge(u , v);
    }
    find_ebcc();
    mem(vis , 0);
    for(int i = 1 ; i <= n ; i++){
        dfs2(i);
    }
    for(int i = 1 ; i <= bcc_cnt ; i++){        //找到最大的邊雙連通分量
        if(num[i] > res){
            max_id  =  i;
            res = num[i];
        }
    }
    for(int i = 1 ; i<= n ; i++){       //找到雙連通分量的一個點以進行dfs3
        if(bccno[i] == max_id){
            max_id = i;     break;
        }
    }
    mem(dfn , 0);
    mem(vis , 0);
    dfs3(max_id);
    for(int i = 0 ; i < ecnt ; i++){
        if(vis[i])      continue;
        if(e[i].is == 1 )
            ans.push_back(e[i]);
        else
            ans.push_back(e[i ^ 1]);
        vis[i] = vis[i ^ 1] = 1;
    }
    printf("%d\n",res);
    for(int i =0 ; i < ans.size() ; i++){
        printf("%d %d\n" , ans[i].u , ans[i].v);
    }
    return 0;
}


發佈了138 篇原創文章 · 獲贊 24 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章