HDU 4738 Caocao's Bridges(双联通分量+并查集)

大意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥,使得这n座岛不连通,求最少要派多少人去。

思路:我们就是要缩点后直接求桥上人的最少数量。(PS:1、注意图如果不联通直接输出0。2、如果图中的桥上人为0,个那么要让一个人去。3、重边的问题。这里可以忽略)

#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL int
#define inf 0x3f3f3f3f
#define eps 1e-8
#include<vector>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1

using namespace std;

const int Ma = 1100;
struct node{
    int to,w,next;
}q[Ma*Ma];

int head[Ma*Ma],dfn[Ma],num[Ma],du[Ma],stk[Ma*5],vis[Ma],low[Ma];
int cnt,top,tim,scc,out[Ma],f[Ma],n,mi;

void Add(int a,int b,int c){
    q[cnt].to = b;
    q[cnt].w = c;
    q[cnt].next = head[a];
    head[a] = cnt++;
}

void init(){
    scc =  cnt = top = 0;
    tim =  1;
    mi = inf;
    memset(head,-1,sizeof(head));
    for(int i = 1;i <= n;++ i){
        f[i] = i;
        low[i] = vis[i] = out[i] = num[i] = dfn[i] = 0;
    }
}

void Tarjan(int u,int To){
    low[u] = dfn[u] = tim++;
    vis[u] = 1;
    stk[top++] = u;
    for(int i = head[u]; ~i ; i = q[i].next){
        int v = q[i].to;
        if(i == (To^1)) continue;
        if(!vis[v]){
            Tarjan(v,i);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u])
                if(q[i].w < mi)
                    mi = q[i].w;
        }
        else
            low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u]){
        scc++;
        while(top > 0&&stk[top] != u){
            top --;
            vis[stk[top] ] = 1;
            num[stk[top] ] = scc;
        }
    }
}

int fi(int x){
    return f[x] == x ? x:f[x]=fi(f[x]);
}

void mer(int a,int b){
    int x = fi(a);
    int y = fi(b);
    x > y ? f[x] = y:f[y] = x;
}

int main(){
    int m,i,j,k,a,b,c,cla;
    while(~scanf("%d%d",&n,&m)){
        if(!n&&!m) break;
        init();
        for(i = 0;i < m;++ i){
            scanf("%d%d%d",&a,&b,&c);
                Add(a,b,c);
                Add(b,a,c);
                mer(a,b);
        }
        int tmp = 0;
        for(i = 1;i <= n;++ i)
            if(f[i] == i){
                tmp++;
                if(tmp > 1) break;
            }
        if(tmp > 1){
            puts("0");continue;
        }
        Tarjan(1,-1);
        for(i = 1;i <= n;++ i){
            for(j = head[i]; ~j ; j=q[j].next){
                int v = q[j].to;
                if(num[i]!=num[v]){
                    out[num[i]]++;
                }
            }
        }
        int ans = 0;
        for(i = 1;i <= scc;++ i){
            if(out[i]==0)
                ans++;
        }
        if(ans==1){
            puts("-1");
        }
        else if(!mi){
            puts("1");
        }
        else
            printf("%d\n",mi);
    }
    return 0;
}

HDU 4738 Caocao's Bridges(双联通分量+并查集)

时间: 2024-10-29 10:45:50

HDU 4738 Caocao's Bridges(双联通分量+并查集)的相关文章

HDU 4738 --Caocao&#39;s Bridges 【无向图边双联通 &amp;&amp; 求权值最小的桥 &amp;&amp; 模板】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2641    Accepted Submission(s): 855 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDU 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

HDU 4738 Caocao&#39;s Bridges(找割边)

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[N

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

hdu 4738 Caocao&#39;s Bridges tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good

Hdu 4738 Caocao&#39;s Bridges (连通图+桥)

题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好. 看起来很简单的样子哦!但是这个题目有很多的细节: A:题目中有重边,以后写Tarjan还是清一色判断重边吧.(除非题目特别要求) B:m个桥有可能连通不了这n个桥,这个时候不需要花费. C:当最小花费桥的花费