HDU 4612 Warm up(边双联通+直径【手动扩栈】)

36 篇文章 4 订阅


Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 7063    Accepted Submission(s): 1640


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
 

Author
SYSU
 

Source
 

Recommend
zhuyuanchen520
 

Statistic |  Submit |  Discuss |  Note


题意:有N个点,M条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树。
思路: 边双联通缩点, 这样每个联通块都没有桥, 重新建图,必定一个树, 然后在树上跑个直径就好了, 因为加的边肯定是在直径上,让直径是一个环,这样切掉哪个边都不会不联通,这题求直径的时候要bfs, dfs会t,可能我用了两个vec的原因

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 2e5 + 5;
int low[maxn], dfn[maxn], id[maxn], bcc_cnt, dfs_cnt;
int top, Stack[maxn], n, m, ans, s, len[maxn];
vector<int> v[maxn], g[maxn];
void init()
{
    memset(low, 0, sizeof(low));
    memset(id, 0, sizeof(id));
    memset(dfn, 0, sizeof(dfn));
    bcc_cnt = dfs_cnt = top = 0;
    for(int i = 0; i < maxn; i++)
        v[i].clear();
    for(int i = 0; i < maxn; i++)
        g[i].clear();
}
void tarjan(int x, int f)
{
    dfn[x] = low[x] = ++dfs_cnt;
    Stack[top++] = x;
    int k = 0;
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if(to == f && !k) //处理重边
        {
            k++;
            continue;
        }
        if(!dfn[to])
        {
            tarjan(to, x);
            low[x] = min(low[x], low[to]);
        }
        else if(!id[to])
            low[x] = min(low[x], dfn[to]);
    }
    if(low[x] == dfn[x])
    {
        bcc_cnt++;
        while(top > 0)
        {
            int u = Stack[--top];
            id[u] = bcc_cnt;
            if(u == x) break;
        }
    }
}
void bcc()
{
    for(int i = 1; i <= n ; i++)
        if(!dfn[i])
            tarjan(i, -1);
//    tarjan(1, -1); //因为图是联通的,只要DFS一个点,全图都可以搜到
}
//void dfs(int x,int f, int len)
//{
//    if(ans <= len)
//    {
//        ans = len;
//        s = x;
//    }
//    for(int i = 0; i < g[x].size(); i++)
//    {
//        int to = g[x][i];
//        if(to == f) continue;
//        dfs(to, x, len+1);
//    }
//}
int Q[maxn];
void BFS(int x)
{
    int frnt, rear;
    frnt = rear = 0;
    Q[rear++] = x;
    memset(len, -1, sizeof(len));
    len[x] = 0;
    while(frnt != rear)
    {
        int u = Q[frnt++];
        for(int i = 0; i < g[u].size(); i++)
        {
            int to = g[u][i];
            if(len[to] == -1)
            {
                len[to] = len[u] + 1;
                if(ans < len[to])
                {
                    ans = len[to];
                    s = to;
                }
                Q[rear++] = to;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d", &n, &m), n+m)
    {
        init();
        int x, y;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        bcc();
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < v[i].size(); j++)
            {
                int to = v[i][j];
                if(id[i] != id[to])
                {
                    g[id[i]].push_back(id[to]);
                    g[id[to]].push_back(id[i]);
                }
            }
        }
        ans = 0;
        BFS(1);
        BFS(s);
//        dfs(1, -1, 0);
//        dfs(s, -1, 0);
//        cout << ans << endl;
        printf("%d\n", bcc_cnt-1-ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值