Redundant Paths(Trajan 加边双连通)

Problem 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)/2,用trajan算法求双联通分量

#include
#include
#include
#include
#include
#include
using namespace std;
#define r 10005
struct node
{
    int v,cut,next;
}edge[2*r];
int cnt;
int head[r];
int stack[r];
int instack[r];
int low[r];
int dfn[r];
int belong[r];
int du[r];
int block;
int index;
int bridge;
int top;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addEdge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    edge[cnt].cut=0;
    head[u]=cnt++;
}
void tarjan(int u,int pre)
{low[u]=dfn[u]=++index;
stack[top++]=u;
instack[u]=1;
int v;
for(int i=head[u];i!=-1;i=edge[i].next)
{
    v=edge[i].v;
    if(pre==v)
    continue;
    if(!dfn[v])
    {tarjan(v,u);
    low[u]=min(low[u],low[v]);
    if(low[v]>dfn[u])
    {
        bridge++;
        edge[i].cut=1;
        edge[i^1].cut=1;
    }
    }
    else if(low[u]>dfn[v]&&instack[v])
    low[u]=dfn[v];

}
if(low[u]==dfn[u])
{
    block++;
    do
    {
        v=stack[--top];
        instack[v]=0;
        belong[v]=block;

    }while(v!=u);
}
}
void solve(int n)
{
    int i,j;
    int index=0;
    bridge=0;
    top=0;
    block=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(belong,0,sizeof(belong));
    memset(instack,0,sizeof(instack));
    tarjan(1,0);
    for(i=1;i<=n;i++)
    {
        for(j=1;j!=-1;j=edge[j].next)
        {
            if(edge[j].cut)
            du[belong[i]]++;
        }
    }
    int result=0;
    for(i=1;i<=block;i++)
    {
        if(du[i]==1)
        result++;

    }
printf("%d\n",(result+1)/2);

}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i

 

你可能感兴趣的:(Trajan)