Redundant Paths+POJ+边双联通分量(有重边-奇偶判断法)

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8992 Accepted: 3886

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
解决方案:此题和POJ3352的思路是一样的,拿此题的代码交POJ3352完全能ac,不过这题和POJ3352又有点区别,这题有重边,所以就不能用点的判断方法来判断是否走了反边。这里用了一个奇偶的判断方案,在用插头式建图时,正反两条边的标号是一对奇偶数,即当走到一条边的时候,判断它与前一条边是不是同一条边,可对前一条边的标号与1异或,若相等,则为一对奇偶数,走的是前一条边的反边。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define MMAX 5005
using namespace std;
int F,R;
int dfn[MMAX],low[MMAX],index,belong[MMAX],degree[MMAX],cc;
bool m[MMAX][MMAX];
int head[MMAX];
struct edge
{
    int to;
    int next;
} E[2*MMAX];
int k;
void add(int from,int to)
{

    E[k].to=to;
    E[k].next=head[from];
    head[from]=k++;
}
void init()
{
    index=1,k=0;
    memset(m,false,sizeof(m));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(degree,0,sizeof(degree));
    memset(belong,0,sizeof(belong));
    memset(head,-1,sizeof(head));
}
void tarjan(int u,int fa)
{

    dfn[u]=low[u]=index++;
    for(int i=head[u]; i!=-1; i=E[i].next)
    {
        int v=E[i].to;
        if(i!=(fa^1))///根据奇偶来判断反边
        {
            if(!dfn[v])
            {
                tarjan(v,i);
                low[u]=min(low[u],low[v]);
                if(low[v]>dfn[u])
                {
                    m[u][v]=m[v][u]=true;
                }
            }
            else
            {
                low[u]=min(low[u],dfn[v]);
            }
        }

    }

}
void dfs(int u)
{

    for(int i=head[u]; i!=-1; i=E[i].next)
    {
        int v=E[i].to;
        if(!m[u][v]&&!belong[v])
        {
            belong[v]=cc;
            dfs(v);
        }
    }

}
int main()
{
    while(~scanf("%d%d",&F,&R))
    {
        init();
        for(int i=0; i<R; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        tarjan(1,-1);
        cc=0;
        for(int i=1; i<=F; i++)
        {
            if(!belong[i])
            {
                cc++;
                belong[i]=cc;
                dfs(i);
            }
        }
        for(int i=1; i<=F; i++)
        {
            for(int j=head[i]; j!=-1; j=E[j].next)
            {
                int v=E[j].to;
                if(belong[i]!=belong[v])
                {
                    degree[belong[i]]++,degree[belong[v]]++;
                }

            }
        }
        int cnt=0;
        for(int i=1; i<=cc; i++)
        {
            if(degree[i]==2)cnt++;
        }
        printf("%d\n",(cnt+1)/2);


    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值