POJ3177 Redundant Paths

本文介绍了一种通过Tarjan算法缩点并在树上消除桥的方法来确保任意两点间至少存在两条不同路径的算法实现。该算法能够处理重复边的情况,并且能够应用于解决特定问题如poj3352。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

和poj3352 Road Constriction很像(传送门),还是用tarjan缩点之后在树上消去桥。具体做法见【这里】
不同的地方是,这里有重边,而且被看成两条不同的路径,所以记父亲的时候不能记结点,要记边。
【顺便说,这题的代码是可以AC3352的,但是3352的代码A不了这个题。】

#include<cstring>
#include<cstdio>
int fir[1010],ne[3010],to[3010],bel[1010],dfn[1010],low[1010],tot,cnt,sta[1010],top,du[1010];
void dfs(int p,int fr)
{
    int i,j,k,x,y,z;
    dfn[p]=low[p]=++cnt;
    sta[++top]=p;
    for (i=fir[p];i;i=ne[i])
      if (!dfn[to[i]])
      {
        dfs(to[i],i);
        if (low[to[i]]<low[p]) low[p]=low[to[i]];
      }
      else
        if (i!=(fr^1)&&dfn[to[i]]<low[p]) low[p]=dfn[to[i]];
    if (dfn[p]==low[p])
    {
        tot++;
        do
        {
            x=sta[top--];
            bel[x]=tot;
        }
        while (x!=p);
    }
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z,ans;
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        ne[2*i]=fir[x];
        fir[x]=2*i;
        to[2*i]=y;
        ne[2*i+1]=fir[y];
        fir[y]=i*2+1;
        to[2*i+1]=x; 
    }
    dfs(1,-1);
    for (i=1;i<=n;i++)
      for (j=fir[i];j;j=ne[j])
        if (bel[i]!=bel[to[j]])
          du[bel[i]]++;
    ans=0;
    for (i=1;i<=tot;i++)
      if (du[i]==1) ans++;
    printf("%d\n",(ans+1)/2);
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值