HDU- 4612 Warm up(边强连通分量+缩点+树的直径)

题目链接
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条边连接,如果打破某条通道,那么就把某些行星与他们隔开。现在需要新开辟一条路径,问开辟之后最少还有多少个桥?
思路: 要求开辟新路径后桥的数量最少,则连接桥数量最多的那条通道即可。
缩点,记录桥的个数,再通过树的直径得到桥最多的那条通道,最后做差即可。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
const int MAXM = 1e6+10;
struct Edge
{
    int v,next;
}e[MAXM<<1];
int head[MAXN],cnt;
int low[MAXN],dfn[MAXN],tot;
int stact[MAXN],index;
int belong[MAXN],now;
int num[MAXN],dis[MAXN];
bool vis[MAXN];
int ans,p;
int n,m,isbridge;

void init()
{
    for(int i=0;i<=n;i++){
        head[i] = -1;
        num[i] = dfn[i] = low[i] = 0;
        vis[i] = false;
    }
    ans = isbridge = cnt = tot = index = now = 0;
}

void addedge(int u,int v)
{
    e[cnt].v = v;
    e[cnt].next = head[u];
    head[u] = cnt++;

    e[cnt].v = u;
    e[cnt].next = head[v];
    head[v] = cnt++;
}

void Tarjan(int u,int father)
{
    dfn[u] = low[u] = ++tot;
    stact[++index] = u;
    vis[u] = true;
    int flag = 0;
    for(int i = head[u];~i;i = e[i].next){
        int v = e[i].v;
        if(v == father && !flag){ flag = 1;continue; }
        if(!dfn[v]){
            Tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u]) isbridge++;
        }
        else if(vis[v])
            low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u]){
        ++now;
        do{
            belong[stact[index]]=now;
            vis[stact[index]] = false;
            index--;
        }while(u != stact[index+1]);
    }
}

void DFS(int u,int father)
{
    vis[u] = true;
    for(int i = head[u];~i;i = e[i].next){
        int v = e[i].v;
        if(vis[v]) continue;
        if(belong[u] != belong[v]){
            dis[belong[v]] = dis[belong[u]] + 1;
            if(ans < dis[belong[v]]){
                ans = dis[belong[v]];
                p = v;
            }
        }
        DFS(v,u);
    }
}

void Find(int x)
{
    ans = 0;
    memset(vis,false,sizeof(vis));
    dis[belong[x]] = 0;
    DFS(x,0);
}

int main()
{
    while(~scanf("%d%d",&n,&m),n+m){
        init();
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            addedge(x,y);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                Tarjan(i,-1);
        for(int i = 0;i <= n; ++i)  vis[i] = 0;
        for(int i=1;i<=n;i++){
            if(!vis[i]){
                Find(i);
                Find(p);
            }
        }
        cout<<isbridge-ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值