码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-18 22:52:11      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/hpuhjh/article/details/47760119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!