CodeForces - 732F Tourist Reform tarjan求bcc连通分量

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中的最小值最大。
思路:规定方向后,应该使得尽量多的点相互可达,而每一个边双连通分量,就是在规定方向之后能够变为强连通分量的最大点集。要使最小的ri最大,就令含有结点数最多的bcc连通分量作为ri,将这个bcc作为树的根节点,所有的桥作为树的边,其他bcc是树的结点,取这个bcc中的一个点rt开始dfs,在这个bcc内部,是这个点可以到达所有点,即ri=bcc结点个数,在这个bcc和其他bcc之间,令所有的方向都是从其他bcc到这个bcc的点rt,这样,从其他任何点开始都可以到达rt,保证其他所有rj>ri。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=400005;
struct Edge
{
    int next;
    int to;
    int w;
    int fro;
    int is;
    Edge()
    {

    }
    Edge(int u,int v)
    {
        this->fro=u;
        this->to=v;

    }
}ed[N*3];
int head[N],cnt;
vector  ans;
void add(int u,int v)  
{  
    ed[cnt].fro=u;   
    ed[cnt].to = v;  
    ed[cnt].next = head[u];  
    head[u] = cnt++;  
    ed[cnt].fro=v;   
    ed[cnt].to = u;  
    ed[cnt].next = head[v];  
    head[v] = cnt++;  
}

int dfn[N],low[N],st[N],bcc[N],bccnum,index,tp;
int num[N];
int ef[N*2],edir[N*2];
bool visside[N],is_bri[N*2],vis[N*2];
void tarjan(int root,int fa){
    int i;
    dfn[root]=low[root]=++index;
    st[++tp]=root;
    for(i=head[root];~i;i=ed[i].next){
        int v=ed[i].to;
        if(ef[i])continue;
        ef[i]=ef[i^1]=1;
        if(!dfn[v]){
            tarjan(v,root);
            low[root]=min(low[root],low[v]);
            if(dfn[root]//桥 
                //printf("exm??????  %d  %d\n",ed[i].fro,ed[i].to);
                is_bri[i]=is_bri[i^1]=1;
            }

        }
        else if(dfn[v]if(low[root]==dfn[root]){
        bccnum++;
        for(;;){
            int x=st[tp--];
            bcc[x]=bccnum;
            num[bccnum]++;
            if(x==root)break;
        }
    }
}

void dfs(int rt)//确定各边的方向 
{
    vis[rt]=1;

    for(int i=head[rt];~i;i=ed[i].next)
    {
        int v=ed[i].to; 
        if(visside[i]) continue;//边不重复,每一条边都搜索到且只搜索到一次  
        if(bcc[rt]==bcc[v])//同一个连通分量内部边的方向与dfs的方向一致 ,保证内部形成强连通分量 
        {
            ed[i].is=1;         
        }
        else ed[i^1].is=1;//不同bcc之间的边的方向与dfs方向相反 ,使其他bcc指向作为根结点的bcc 
        visside[i]=visside[i^1]=1;      
        if(!vis[v]) dfs(v);
    } 
}
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    {
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(bcc,0,sizeof(bcc));
        memset(head,-1,sizeof(head));
        memset(is_bri,0,sizeof(is_bri));
        memset(vis,0,sizeof(vis));
        memset(edir,0,sizeof(edir));
        memset(ef,0,sizeof(ef));
        bccnum=0;
        index=0;
        tp=0;
        cnt=0;

        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                tarjan(i,-1);
            }

        }

        int rt,mx=0;
        for(int i=1;i<=bccnum;i++)
        {           
            if(num[i]>mx) 
            mx=num[i],rt=i;
        }
        for(int i=1;i<=n;i++)
        {
            if(bcc[i]==rt)
            {
                rt=i;
                break;
            }
        }
        memset(vis,0,sizeof(vis));
        memset(visside,0,sizeof(visside));
        dfs(rt);
        memset(vis,0,sizeof(vis));
        printf("%d\n",mx);
        for(int i=0;iif(vis[i]) continue;
            if(ed[i].is==1)
            printf("%d %d\n",ed[i].fro,ed[i].to);
            else printf("%d %d\n",ed[i^1].fro,ed[i^1].to);
            vis[i]=vis[i^1]=1;
        }

    }
    return 0;
}

你可能感兴趣的:(codeforces,tarjan)