HDU 4612 边双连通分量缩点 + 树的直径

11 篇文章 0 订阅

Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It’s always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don’t like to be isolated. So they ask what’s the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.

Input
  The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1…N.
  A line with two integers ‘0’ terminates the input.

Output
  For each case, output the minimal number of bridges after building a new channel in a line.

Sample Input
4 4
1 2
1 3
1 4
2 3
0 0

Sample Output
0

题意&&解法:
给你一个n个点m条边的无向无权图,问你在添加一条边的情况下最少有多少条桥。
首先该题存在重边,又因为当某一条边存在重边时则它一定不是桥,故重边的存在对求桥有影响,所以我们不得不处理重边。我所知道的有两种处理重边的方法,一种通过记录边的编号,另一种则是通过只走一次父边来处理,我个人比较推荐后者(写起来方便)。
处理完重边后我们只需要用Tarjan边连通分量缩点 ,缩点后的新图则是一颗树(记作T)。容易发现最终所求答案为原图中桥的数量 - T的直径。

多说两句:
求边连通分量时可以用人们常用的压栈的方法,也可以采用“能不走桥就不走桥”的染色方法。

ACODE:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef unsigned long long ll;
const int MX = 4e5 + 7;
const int MXM = 2e6 + 7;
inline ll read() {
 	ll a = 0; int f = 0; char p = getchar();
 	while (!isdigit(p)) { f |= p == '-'; p = getchar(); }
 	while (isdigit(p)) { a = (a << 3) + (a << 1) + (p ^ 48); p = getchar(); }
 	return f ? -a : a;
}

inline void write(ll x) {
 	if (x < 0) putchar('-'), x = -x;
 	if (x > 9) write(x / 10);
 	putchar(x % 10 + '0');
}
int n,m;
int tim,tot,num;//分别为时间戳 边连通分量个数 桥的个数
int head1[MX],cnt1;
int head2[MX],cnt2;

int DFN[MX],LOW[MX];
int col[MX],par[MX];

bool cut[MX],vis[MX];
int dis[MX];

struct Edge
{
    int v,next;
}G[MXM],NG[MXM];//分别为原图和缩点后的新图
inline void add1(int u,int v)
{
    G[++cnt1].v = v;
    G[cnt1].next = head1[u];
    head1[u] = cnt1;
}
inline void add2(int u,int v)
{
    NG[++cnt2].v = v;
    NG[cnt2].next = head2[u];
    head2[u] = cnt2;
}
struct tarjan{
	//初始化
    void init(){
        num = tim = tot = 0;
        cnt1 = cnt2 = 0;
        for(int i = 1;i <= n;++i){
            head1[i] = head2[i] = 0;
            par[i] = i;
            dis[i] = DFN[i] = LOW[i] = 0;
            vis[i] = cut[i] = false;
        }
    }

    inline void dfs(int u,int fa){
        par[u] = fa;
        DFN[u] = LOW[u] = ++tim;
        bool flag = true;//处理重边的标记
        for(int i = head1[u];i;i = G[i].next){
            int v = G[i].v;
            //处理重边
            if(flag && v == fa){flag = false;continue;}
            if(!DFN[v]){
                dfs(v,u);
                LOW[u] = min(LOW[u],LOW[v]);
                //求桥 cut[v] 表示节点v - fa 这条边为桥
                if(LOW[v] > DFN[u]) cut[v] = true,num++;
            }
            else  LOW[u] = min(LOW[u],DFN[v]);
        }
    }
    void pre(){
        dfs(1,-1);
    }

    inline void cdfs(int u){
        vis[u] = true;
        col[u] = tot;
        for(int i = head1[u];i;i = G[i].next){
            int v = G[i].v;
            //如果访问过/该边为桥 则跳过
            if(vis[v] || (cut[v] && par[v] == u)) continue;
            cdfs(v);
        }
    }

    //边连通分量染色
    void color(){
        for(int i = 1;i <= n;++i){
            if(!vis[i]) ++tot,cdfs(i);
        }
    }

    //边连通分量缩点
    void shrink(){
        for(int u = 1;u <= n;++u){
            for(int i = head1[u];i;i = G[i].next){
                int v = G[i].v;
                if(col[u] != col[v]){
                    add2(col[u],col[v]);
                }
            }
        }
    }

    //两次bfs求树的直径
    int bfs(int u){
        for(int i = 1;i <= tot;++i) vis[i] = false;
        queue<int>q;
        q.push(u);
        vis[u] = true;
        dis[u] = 0;
        int t;
        while(!q.empty()){
            t = q.front();q.pop();
            for(int i = head2[t];i;i = NG[i].next){
                int v = NG[i].v;
                if(vis[v])  continue;
                vis[v] = true;
                dis[v] = dis[t] + 1;
                q.push(v);
            }
        }
        return t;
    }

    //计算答案
    int sol(){
        return (num  - dis[bfs(bfs(1))]);
    }
}Tarjan;
int main(int argc, char const *argv[])
{
    while(~scanf("%d%d",&n,&m) && (n + m)){
        Tarjan.init();//初始化
        for(int i = 1;i <= m;++i){
            int u,v;u = read(),v = read();
            add1(u,v);
            add1(v,u);
        }
        Tarjan.pre();//求桥
        Tarjan.color();//染色
        Tarjan.shrink();//缩点
        int res = Tarjan.sol();
        write(res);
        putchar('\n');
    }
    return 0;
}

提供几组数据:

Input

6 6
1 2
2 3
3 4
4 5
2 6
6 2

7 8
1 2
2 3
3 4
4 5
2 6
6 2
1 3
4 7

5 4
2 1
2 3
2 4
2 5

5 5
2 1
2 3
2 4
2 5
3 4

5 5
1 2
2 3
3 4
3 4
4 5

Output
0
1
2
0
0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值