HDU 4612--Warm up 【无向图边双连通求桥数 && 缩点后重建图求树的直径】

Warm up

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


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条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树。

思路:求出图中的桥的个数,然后缩点重建图必为树,求出树的最长直径,在该直径的两端点连一边,则图中的桥会最少。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#define maxn 200000+1000
#define maxm 2000000+1000
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int n ,m;

struct node {
    int u, v, next;
};

node edge[maxm];
int head[maxn], cnt;
int dfn[maxn], low[maxn];
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int dfs_clock;
int ebc_clock;
int bridge;
vector<int>Map[maxn];
int dist[maxn], vis[maxn];
int ans, nod;

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v){
    edge[cnt] = {u, v, head[u]};
    head[u] = cnt++;
}

void getmap(){
    while(m--){
        int a, b;
        scanf("%d%d", &a, &b);
        addedge(a, b);
        addedge(b, a);
    }
}
void tarjan(int u, int per){
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    Stack[top++] = u;
    Instack[u] = true;
    int have = 1;
    for(int i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        if(v == per && have){
            have = 0;
            continue;
        }
        if(!dfn[v]){
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] > dfn[u])
                bridge++;
        }
        else if(Instack[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]){
        ebc_clock++;
        do{
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = ebc_clock;
        }while(v != u);
    }
}

void suodian(){//Ëõµã½¨ÐÂͼ
    for(int i = 1; i <= ebc_clock; ++i)
        Map[i].clear();
    for(int i = 0; i < cnt; i = i + 2){
        int u = Belong[edge[i].u];
        int v = Belong[edge[i].v];
        if(u != v){
            Map[u].push_back(v);
            Map[v].push_back(u);
        }
    }
}

void find(){
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Instack, false, sizeof(Instack));
    memset(Belong, 0, sizeof(Belong));
    dfs_clock = ebc_clock = top = bridge = 0;
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])
            tarjan(i, i);
    }
}

void BFS(int x){
    queue<int>q;
    memset(vis, 0, sizeof(vis));
    memset(dist, 0, sizeof(dist));
    vis[x] = 1;
    ans = 0;
    nod = x;
    q.push(x);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = 0; i < Map[u].size(); ++i){
            int v = Map[u][i];
            if(!vis[v]){
                vis[v] = 1;
                dist[v] = dist[u] + 1;
                if(ans < dist[v]){
                    nod = v;
                    ans = dist[v];
                }
                q.push(v);
            }
        }
    }
}

int main (){
    while(scanf("%d%d", &n, &m), n || m){
        init();
        getmap();
        find();
        suodian();
        BFS(1);
        BFS(nod);
        printf("%d\n", bridge - ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值