★【双连通分量】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.
此题考查边双连通分量的求法,用边双连通分量进行缩点,以及构造双联通子图。
首先求一次双连通分量,即把桥找出来后,剩下的不相连的几个部分一定各为一个边双连通分量,再加回桥边,得到一棵树,然后统计这棵树中度为1的点的个数,记为leaf,若leaf=1,即只有一个双连通分量,那么不需要加边。否则,每次两两配对,最终需要加(leaf + 1) / 2条边。
程序有一点Bug,但不知为什么会过……

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>

const int maxN = 5010;
struct Edge
{
    int v; bool bridge;
    Edge *next, *back; Edge() {}
    Edge(int v, Edge *next):
        v(v), next(next), bridge(0) {}
} *edge[maxN], *bridge[maxN];
bool marked[maxN];
int DFN[maxN], Low[maxN], stack[maxN];
int Belong[maxN], Degree[maxN];
int root, Dcnt, Ind, leaf, top, n, m;

inline int getint()
{
    int res = 0; char tmp;
    while (!isdigit(tmp = getchar()));
    do res = (res << 3) + (res << 1) + tmp - '0';
    while (isdigit(tmp = getchar()));
    return res;
}

void tarjan(int u, int Last)
{
    DFN[u] = Low[u] = ++Ind;
    marked[stack[++top] = u] = 1;
    for (Edge *p = edge[u]; p; p = p -> next)
    if (p -> v != Last)
    {
        int v = p -> v;
        if (!DFN[v])
        {
            tarjan(v, u);
            Low[u] = std::min(Low[u], Low[v]);
            if (DFN[u] < Low[v])
            {
                p -> back -> bridge = p -> bridge = 1;
                ++Dcnt; int tmp = v;
                do
                {
                    tmp = stack[top--];
                    marked[tmp] = 0;
                    Belong[tmp] = Dcnt;
                } while (tmp - v);
            }
        }
        else if (marked[v])
            Low[u] = std::min(Low[u], DFN[v]);
    }
    return;
}

int main()
{
    freopen("Redundant_Paths.in", "r", stdin);
    freopen("Redundant_Paths.out", "w", stdout);
    n = getint(); m = getint();
    while (m--)
    {
        int u = getint(), v = getint();
        edge[u] = new Edge(v, edge[u]);
        edge[v] = new Edge(u, edge[v]);
        edge[u] -> back = edge[v];
        edge[v] -> back = edge[u];
    }
    tarjan(1, -1); int tmp = 1; ++Dcnt;
    do
    {
        tmp = stack[top--];
        marked[tmp] = 0;
        Belong[tmp] = Dcnt;
    } while (tmp - 1);
    for (int u = 1; u < n + 1; ++u)
    for (Edge *p = edge[u]; p; p = p -> next)
    if (p -> bridge)
    {
        int v = p -> v;
        bridge[Belong[u]] = new
            Edge(Belong[v], bridge[Belong[u]]);
    }
    int leaf = 0;
    for (int u = 1; u < Dcnt + 1; ++u)
    {
        for (Edge *p = bridge[u]; p; p = p -> next)
            ++Degree[u];
        if (Degree[u] == 1) ++leaf;
    }
    printf("%d\n", leaf == 1 ? 0 : (leaf + 1 >> 1));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值