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<stdio.h>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<vector>
#include<iostream>
#include<set>
#include<queue>
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 <Edge> 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]<low[v]){
                //桥 
                //printf("exm??????  %d  %d\n",ed[i].fro,ed[i].to);
                is_bri[i]=is_bri[i^1]=1;
            }

        }
        else if(dfn[v]<dfn[root]&&v!=fa)
         low[root]=min(low[root],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;i<cnt;i++)
        {
            if(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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值