POJ 3352

题意:给你一个无向图,问至少添加几条边能够使两点之间至少有两条边不重复的路(也就是形成一个双连通图)

思路:先求割边,然后把割边“删除”,把各个点标记为相应的双连通分量,缩点,然后把割边连上,形成一棵树,判断每个节点的度,统计度为1的节点,(ans + 1)/2就是答案

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 1100;
const int maxm = 4000;
int sum;
int t,top;
int node[maxn];
stack<int>s;
struct Side{
    int u,v,next;
}side[maxm];
void add_side(int u,int v){
    side[top] = (Side){u,v,node[u]};
    node[u] = top ++;
}
int vis[maxn],low[maxn],dfn[maxn];
int n,m;
int tot,done[maxn];
int deg[maxn];
bool mm[maxn][maxn];
vector<int>vec;
void dfs(int u,int fa){
    vis[u] = 1;
    dfn[u] = low[u] = t ++;
    for(int i = node[u];i != -1;i = side[i].next){
        int v = side[i].v;
        if(v != fa && vis[v] == 1)
            low[u] = min(low[u],dfn[v]);
        if(vis[v] == 0){
            dfs(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u]){
                mm[u][v] = mm[v][u] = true;
                vec.push_back(i);
            }
        }
    }
    vis[u] = 2;
}
void init(){
    vec.clear();
    memset(node,-1,sizeof(node));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(done,0,sizeof(done));
    memset(deg,0,sizeof(deg));
    memset(mm,0,sizeof(mm));
    t = top = sum = tot = 0;
}
void haha(int u){
    done[u] = tot;
    for(int i = node[u];i != -1;i = side[i].next){
        int v = side[i].v;
        if(mm[u][v] == false && done[v] == 0)haha(v);
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        init();
        while(m--){
            int a,b;
            scanf("%d%d",&a,&b);
            add_side(a,b);
            add_side(b,a);
        }
        for(int i = 1;i <= n;i ++)
            if(!dfn[i])dfs(i,-1);
        for(int i = 1;i <= n;i ++)
            if(!done[i]){
                tot++;
                haha(i);
            }
        for(int j = 0;j < vec.size();j ++){
            int i = vec[j];
            int u = done[side[i].u];
            int v = done[side[i].v];
            deg[u] ++;deg[v] ++;
        }
        int ans = 0;
        for(int i = 1;i <= tot;i ++){
            if(deg[i] == 1)ans ++;
        }
        if(ans == 1)cout<<0<<endl;
        else cout<<(ans+1)/2<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值