POJ-3177-Redundant Paths【双连通分支】

3177-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

题目链接:POJ-3177

题目大意:给定一个连通的无向图 G,至少要添加几条边,才能使其变为双连通图。

题目思路:

一个有桥的连通图,如何把它通过加边变成边双连通图?

  1. 求出所有的桥,然后删除这些桥边, 剩下的每个连通块都是一个双连通子图。把每个双连通子图收缩为一个顶点 ,再把桥边加回来,最后的这 个图一定是一棵树,边连通度为 1。

  2. 统计出树中度为 1 的节点的个数,即为叶节点的个数,记为 l eaf。

  3. 则至少在树上添加(leaf+1)/2 条边,就能 使树达到边二连通,所以至少添加的边数就是(lea f+1)/2。

    具体方法为,首先把两个最近公共祖先最远的两 个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一 定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2 次, 把所有点收缩到了一起。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
/* Tarjan算法 * 复杂度O(N+M) */
const int MAXN = 5010;//点数
const int MAXM = 20010;//边数
struct Edge
{
    int to,next;
    bool cut; 
}edge[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc,Belong[i]即i属于哪个连通块
int Index,top;
int Instack[MAXN];
int block;
int bridge;
void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;head[u] = tot++;
}

void Tarjan(int u,int pre) {
    int v;
    Low[u] = DFN[u] = ++Index;  //刚搜到一个节点时Low = DFN;
    Stack[top++] = u;  //将该节点入栈
    Instack[u] = 1;  //将入栈标记设置为1
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if (v == pre) continue;
        if(!DFN[v])
        {
            Tarjan( v,u);
            if( Low[u] > Low[v] )Low[u] = Low[v];
            if (Low[v] > DFN[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }
        }
        else if(Instack[v] && Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        block++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = block;
        }
        while( v != u);
    }
}
void solve(int N)
{
    memset( DFN,0 ,sizeof(DFN));
    memset( Instack,false, sizeof(Instack) );
    Index = block = top = 0;
    Tarjan(1,0);
}
void init()
{
    tot = 0;
    memset( head, -1,sizeof (head));
}
int d[5005];
int main(){
    int n,r;
    while(cin >> n >> r)
    {
        init();
        memset(d,0,sizeof(d));
        for (int i = 0; i < r; i++)
        {
            int u,v;
            cin >> u >> v;
            addedge(u,v);
            addedge(v,u);
        }
        solve(n);
        for (int i = 1; i <= n; i++)
        {
            for (int j = head[i]; j != -1; j = edge[j].next)
            {
                if (edge[j].cut) 
                    d[Belong[i]]++;
            }
        }
        int ans = 0;
        for (int i = 1; i <= block; i++)
        {
            if (d[i] == 1) ans++;
        }
        cout << (ans + 1) / 2 << endl;
    }
    return 0;
}

你可能感兴趣的:(poj,双连通分支,3177)