POJ3177 Redundant Paths

来源:互联网 发布:淘宝上什么东西没有卖 编辑:程序博客网 时间:2024/06/05 16:36

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);} 
0 0
原创粉丝点击