POJ3177 Redundant Paths 双连通分量

Redundant Paths

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

Hint

Explanation of the sample:

One visualization of the paths is:

   1   2   3   +---+---+         |   |       |   | 6 +---+---+ 4      / 5     /     /  7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

   1   2   3   +---+---+     :   |   |   :   |   | 6 +---+---+ 4      / 5  :     /     :    /      : 7 + - - - - 

Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes.

It‘s possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

题意:为了保护放牧环境,避免牲畜过度啃咬同一个地方的草皮,牧场主决定利用不断迁移牲畜进行喂养的方法去保护牧草。然而牲畜在迁移过程中也会啃食路上的牧草,所以如果每次迁移都用同一条道路,那么该条道路同样会被啃咬过度而遭受破坏。现在牧场主拥有F个农场,已知这些农场至少有一条路径连接起来(不一定是直接相连),但从某些农场去另外一些农场,至少有一条路可通行。为了保护道路上的牧草,农场主希望再建造若干条道路,使得每次迁移牲畜时,至少有2种迁移途径,避免重复走上次迁移的道路。已知当前有的R条道路,问农场主至少要新建造几条道路,才能满足要求?

题解:把F个农场看作点、路看作边构造一个无向图G时,图G不存在桥。

也就是问给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。

把每一个双连通分量(内部满足条件)缩为一个点,形成一棵树,加(n+1)/2条边就是双连通了(度为1的点个数为n)

注意:判断两个点是不是同一个双连通分量

1.无重边:low值相等就是同一个双连通分量

2.有重边:bfs结束时出栈的就是同一连通分量,好像有点麻烦

这里加了一个判断,不加重边

#include<stdio.h>
#include <algorithm>
#include <string.h>
#define N 5005
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+7;
const int MAX = 0x7ffffff;
using namespace std;
struct ed{
    int to, next;
}edge[N*5];
int head[N], top=0;
bool mp[N][N];
int pre[N], low[N], dfs_time, out[N];
void addedge(int u,int v){
    edge[top].to = v;
    edge[top].next = head[u];
    head[u] = top++;
}
void dfs(int u,int father){
    low[u] = pre[u] = dfs_time++;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v = edge[i].to;
        if(v == father) continue;
        if(!pre[v]){
            dfs(v, u);
            low[u] = min(low[v], low[u]);
        }
        else low[u] = min(low[u], pre[v]);
    }
}
int main(){
    int n, m, t, i, j, a, b;
    while(~scanf("%d%d", &n, &m)){
        memset(head, -1, sizeof(head));
        memset(pre,0,sizeof(pre));
        memset(low, 0, sizeof(low));
        memset(out, 0, sizeof(out));
        memset(mp, false, sizeof(mp));
        dfs_time = 1;top = 0;
        for(i=1;i<=m;i++){
            scanf("%d%d", &a, &b);
            if(!mp[a][b]){
            mp[a][b] = mp[b][a] = 1;
            addedge(a, b);
            addedge(b, a);
            }
        }
        dfs(1,-1);
        t = 0;
        for(i=1;i<=n;i++)
            for(j=head[i];j!=-1;j=edge[j].next){
                int v = edge[j].to;
                if(low[v] != low[i])
                    out[low[i]]++;
            }
        for(i=1;i<=n;i++)
            if(out[i] == 1)
                t++;
        printf("%d\n", (t+1)/2);
    }
}
时间: 2024-08-07 00:16:14

POJ3177 Redundant Paths 双连通分量的相关文章

POJ 3177 - Redundant Paths - 双连通分量

题目大意: 给定一个N个点,M条边的无向连通图(可能有重边),要求让任意两点间都有两条或以上的路径,且这些路径没有公共边.问至少需要加多少条边? N<=5e3,M<=1e4. 求双连通分量并缩点.详见:https://www.cnblogs.com/frog112111/p/3367039.html 注意由于本题数据允许重边(尽管讨论区有人吐槽数据太水),DFS时判断割边的条件应为low[to] > dfn[cur],且该边的重数为1. 实现的时候用了并查集来维护属于同一双联通分量的点,

POJ3177 Redundant Paths (双联通缩点)

求对于给定一个连通图,加多少条边可以变成边双连通图. 一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树.需要加的边为(leaf+1)/2 (leaf为叶子结点个数). 对于此题,有重边但重边不加入计算. 重边的话,要么在开始去掉,要么用桥来计算入度. 因为桥不属于任何一个边双连通分支,其余的边和每个顶点都属于且只属于一个边双连通分支.对于重边而言,只有一对边被标记为桥,而对于所有重边来言,belong[u]和belong[v]都是不一样的,那么如果用belong[u]!

poj3177 Redundant Paths 边双连通分量

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define eps 1e-

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

题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后的点. 代码: /*3352*/ #include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector> #include&

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       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 T

[POJ3177]Redundant Paths(双连通图,割边,桥,重边)

题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1.  构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分,其中桥的一端的割点归入当前正在划分的边双连通分量.这个处理比较麻烦: 2.  在输入图G的边时,若出现重边,则不把重边放入图G,然后在划分边双连通分量时依然用Low划分. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓

poj3177 &amp;&amp; poj3352 边双连通分量缩点

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 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

poj3177(边双连通分量+缩点)

传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点. 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,因此同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图的桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加

poj 3177 Redundant Paths (双联通)

/******************************************************* 题目:Redundant Paths (poj 2177) 链接:http://poj.org/problem?id=3177 算法:双联通+缩点 思路:先找出所有双联通分量,把这些分量缩成一个点 再找出所有度为一的点,用这些点数加一除2就可以了 ********************************************************/ #include<cs