POJ3177:Redundant Paths(并查集+桥)

Posted heyuhhh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3177:Redundant Paths(并查集+桥)相关的知识,希望对你有一定的参考价值。

Redundant Paths

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19316   Accepted: 8003

题目链接:http://poj.org/problem?id=3177

Description:

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input:

Line 1: Two space-separated integers: F and R 

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output:

Line 1: A single integer that is the number of new paths that must be built.

Sample Input:

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output:

2

题意:

给出一个无向图,问最少加多少条边,图中不存在桥。

 

题解:

还是先对图进行缩点,然后将图变成一颗树,然后我们考虑加最少的边让这个图不存在桥。

这时,我们加边的话,就会形成一条环,环上面所有的边都不为桥。那么我们考虑尽量加边形成较大的环。

这样,其实就只需要把入度为1的点找出来,假设其个数为cnt,那么答案就是(cnt+1)/2了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 5005, M = 20005;
int n,m,cnt;
struct Edge{
    int u,v,next;
    bool operator < (const Edge &A)const{
        if(u==A.u) return v<A.v;
        return u<A.u;
    }
}e[M<<1],g[M<<1];
int T,tot;
int dfn[N],low[N],cut[N],f[N],d[N],num[N],head[N];
void adde(int u,int v){
    e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
void init(){
    T=0;tot=0;cnt=0;
    memset(head,-1,sizeof(head));
    memset(cut,0,sizeof(cut));
    memset(dfn,0,sizeof(dfn));
}
int find(int x){
    return f[x]==x ? f[x] : f[x]=find(f[x]);
}
int same(int x,int y){
    return find(x)==find(y);
}
void Union(int x,int y){
    int fx=find(x),fy=find(y);
    if(fx!=fy) f[fx]=fy;
}
void Tarjan(int u,int pre){
    dfn[u]=low[u]=++T;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==pre) continue ;
        if(!dfn[v]){
            Tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]){
                cut[v]=1;
            }else Union(u,v);
        }else{
            low[u]=min(low[u],dfn[v]);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    init();
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        if(u>v) swap(u,v);
        adde(u,v);adde(v,u);
        g[i].u=u;g[i].v=v;
    }
    sort(g+1,g+m+1);
    for(int i=1;i<=n;i++) f[i]=i;
    Tarjan(1,0);
    for(int i=1;i<=m;i++){
        int u=g[i].u,v=g[i].v;
        if(g[i].u==g[i-1].u&&g[i].v==g[i-1].v) continue ;
        if(same(u,v)) continue ;
        int fx=find(u),fy=find(v);
        if(!num[fx]) num[fx]=++cnt;
        if(!num[fy]) num[fy]=++cnt;
        d[num[fx]]++;d[num[fy]]++;
    }
    int ans = 0;
    for(int i=1;i<=cnt;i++) if(d[i]==1) ans++;
    cout<<(ans+1)/2;
    return 0;
}

 

以上是关于POJ3177:Redundant Paths(并查集+桥)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3177 - Redundant Paths - 双连通分量

POJ3177 Redundant Paths 双连通分量

无向图缩点poj3177 Redundant Paths

[双连通分量] POJ 3177 Redundant Paths

poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

poj3177:Redundant Paths——题解