POJ3177Redundant Paths边-双联通分量

http://poj.org/problem?id=3177

问加多少边可以形成边双联通图

答案是叶子数加1除以2

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.

Source

 

#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int n;
struct map
{
    int s,t,x;
    int next;
} a[150001];
int edge;
int head[10001];
int scc,cnt;
bool v[10001];
int s[10001],top;
int dfn[10001],low[10001],belong[10001];
int fa[10001];
int indeg[10001];
int tot=0;
inline void add(int s,int t)
{
    a[edge].next=head[s];
    head[s]=edge;
    a[edge].s=s;
    a[edge].t=t;
    a[edge].x=tot;
}
inline int min(int x,int y)
{
    if(x<y)
        return x;
    return y;
}
void tarjan(int d)
{
    int i,x;
    cnt++;
    dfn[d]=cnt;
    low[d]=cnt;
    top++;
    s[top]=d;
    v[d]=true;
    for(i=head[d]; i!=0; i=a[i].next)
    {
        x=a[i].t;
        if(a[i].x==fa[d])
            continue;
        if(dfn[x]==0)
        {
            fa[x]=a[i].x;
            tarjan(x);
            low[d]=min(low[d],low[x]);
        }
        else if(v[x]&&low[d]>dfn[x])//v在栈中,修改low[u]
            low[d]=dfn[x];
    }
    if(dfn[d]==low[d])//u为该强连通分量中遍历所成树的根
    {
        scc++;
        x=s[top];
        top--;
        while(x!=d)
        {
            v[x]=false;
            belong[x]=scc;
            x=s[top];
            top--;
        }
        v[x]=false;
        belong[x]=scc;
    }
}
inline void count_edge()
{
    int i;
    for(i=1; i<=edge; i=i+2)
    {
        if(belong[a[i].s]==belong[a[i].t])
            continue;
        indeg[belong[a[i].s]]++;
        indeg[belong[a[i].t]]++;
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int s,t;
    int i,j;
    for(i=1; i<=m; i++)
    {
        scanf("%d%d",&s,&t);
        edge++;
        tot++;
        add(s,t);
        edge++;
        add(t,s);
    }
    memset(v,false,sizeof(v));
    for(i=1; i<=n; i++)
        if(belong[i]==0)
            tarjan(i);
    count_edge();
    int leaf=0;
    for(i=1; i<=scc; i++)
        if(indeg[i]==1)
            leaf++;
    int ans=(leaf+1)/2;
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值