Codeforces 732F Tourist Reform【思维+边双联通+Dfs处理后继问题】好题!

224 篇文章 2 订阅
7 篇文章 0 订阅

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个点,M条无向边,让你给这些无向边规定一个方向,使得得到的有向图中,min(F(x))最大。

F(x)表示的是x这个点可以到的点的个数。


思路:


1、很明显,对于一个边双联通分量来讲,肯定最终得到的图中,这一部分是一个强连通分量。

那么问题就在于桥的处理,我们希望min(F(x))尽可能的大,那么我们肯定希望以点数最多的某一个双联通分量作为一个基点,使得其变成有向图之后,只有从这个强连通分量连出的边,而没有连入的边。

那么这个min(F(x))==原图中max(双联通分量中点的个数);


2、那么接下来的问题就是确定最终图的边的方向了。

我们不妨将问题转化为两个子问题:

①对于边来讲,两个点属于同一双联通分量(确定方向之后的强连通分量);

②对于边来讲,两个点不属于同一双联通分量(也就是桥);

对于问题①,其实处理起来非常简单,直接按照Tarjan过程中搜的顺序确定即可。

对于问题②,我们将图重新建立,缩点染色之后.我们以点数最多的某一个双联通分量作为一个基点,向外去搜,并且确定每个缩点的编号,

基点的编号设定为0(son【u】=0).并且有:son【v】=son【u】+1;

过程维护一下。

那么对于第②类边,我们确定son【color【u】】和son【color【v】】的大小即可。我们肯定希望son【】大的连向小的。


3、细节处理好,就没有其他什么了。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
struct node
{
    int v;int pos;
}now;
vector<node >mp[400500];
vector<int  >mp2[400500];
int x[400500];
int y[400500];
int dfn[400500];
int vis[400500];
int low[400500];
int in[400500];
int out[400500];
int stack[400500];
int color[400500];
int contz[400500];
int use[400500];
int ans[400500][2];
int son[400500];
int cnt,sig,tot;
int n,m,maxncolor,root;
void init()
{
    sig=0;
    cnt=1;
    tot=-1;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(contz,0,sizeof(contz));
    memset(vis,0,sizeof(vis));
    memset(color,0,sizeof(color));
    memset(stack,0,sizeof(stack));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
}
void Tarjan(int u,int from)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tot]=u;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i].v;
        if(v==from)continue;
        if(vis[v]==0)
        {
            Tarjan(v,u);
            if(use[mp[u][i].pos]==0)
            use[mp[u][i].pos]=1,ans[mp[u][i].pos][0]=u,ans[mp[u][i].pos][1]=v;
            low[u]=min(low[u],low[v]);
        }
        else
        {
            if(use[mp[u][i].pos]==0)
            use[mp[u][i].pos]=1,ans[mp[u][i].pos][0]=u,ans[mp[u][i].pos][1]=v;
            low[u]=min(low[u],low[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        sig++;
        do
        {
            color[stack[tot]]=sig;
            vis[stack[tot]]=-1;
        }
        while(stack[tot--]!=u);
    }
}
void Slove()
{
    init();
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)Tarjan(i,-1);
    }
    int output=0;
    for(int i=1;i<=n;i++)
    {
        contz[color[i]]++;
        if(contz[color[i]]>output)
        {
            output=contz[color[i]];
            maxncolor=color[i];
        }
    }
    printf("%d\n",output);
}
void Dfs(int u,int from)
{
    for(int i=0;i<mp2[u].size();i++)
    {
        int v=mp2[u][i];
        if(v==from)continue;
        son[v]=son[u]+1;
        Dfs(v,u);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            now.v=y[i];now.pos=i;
            mp[x[i]].push_back(now);
            now.v=x[i];now.pos=i;
            mp[y[i]].push_back(now);
        }
        Slove();
        for(int i=1;i<=n;i++)mp2[i].clear();
        for(int i=0;i<m;i++)
        {
            if(color[x[i]]==color[y[i]])continue;
            else
            {
                int u=x[i];int v=y[i];
                mp2[color[u]].push_back(color[v]);
                mp2[color[v]].push_back(color[u]);
            }
        }
        son[maxncolor]=0;
        Dfs(maxncolor,-1);
        for(int i=0;i<m;i++)
        {
            if(color[x[i]]!=color[y[i]])
            {
                int u=x[i];
                int v=y[i];
                if(son[color[u]]<son[color[v]])
                {
                    ans[i][0]=v,ans[i][1]=u;
                }
                else ans[i][0]=u,ans[i][1]=v;
            }
        }
        for(int i=0;i<m;i++)
        {
            printf("%d %d\n",ans[i][0],ans[i][1]);
        }
    }

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值