poj 3352 Road Construction

It’s almost summer time, and that means that it’s almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3
Sample Output
Output for Sample Input 1
2

Output for Sample Input 2
0
题意:城市之间修有道路,现在修路,每条路施工的时候不能通行,现在给出以有道路,让你确定最少还要修几条路从才能使修任意一条道路时,仍可使用余下的道路在任何两个旅游景点之间行走
思路:缩点的思想形成新图并添边使新图强连通(找新图度为1的节点的个数sum,添边个数就是(sum+1)/2)
AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int SIZE=100010;
int head[SIZE],ver[SIZE*2],Next[SIZE*2];
int dfn[100010],low[SIZE],n,m,tot,num=0,ans=0,du[SIZE*2],sum=0;
bool bridge[SIZE*2];
int hc[SIZE],vc[SIZE*2],nc[SIZE*2],tc;
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}
void add_c(int x,int y)
{
    vc[++tc]=y;
    nc[tc]=hc[x];
    hc[x]=tc;
}
void tarjan(int x,int in_edge)
{
    dfn[x]=low[x]=++num;
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(!dfn[y])
        {
            tarjan(y,i);
            low[x]=min(low[x],low[y]);
            if(low[y]>dfn[x])
            {
                bridge[i]=bridge[i^1]=true;
                sum++;
            }
        }
        else
            {
                if(i!=(in_edge^1))
            low[x]=min(low[x],dfn[y]);
            }
    }
}
int c[SIZE],dcc;
void dfs(int x)
{
    c[x]=dcc;
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(c[y]||bridge[i])continue;
        dfs(y);
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(head,0,sizeof(head));
        memset(ver,0,sizeof(ver));
         memset(Next,0,sizeof(Next));
        memset(bridge,0,sizeof(bridge));
         memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
         memset(hc,0,sizeof(hc));
        memset(vc,0,sizeof(vc));
         memset(nc,0,sizeof(nc));
         memset(c,0,sizeof(c));
          memset(du,0,sizeof(du));
        num=0;
        ans=0;
        sum=0;
        dcc=0;
        tot=1;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
        tarjan(i,0);
    for(int i=1;i<=n;i++)
    {
        if(!c[i])
        {
            ++dcc;
            dfs(i);
        }
    }
    tc=1;
    for(int i=1;i<=tot;i++)
    {
        int x=ver[i^1],y=ver[i];
        if(c[x]==c[y])continue;
        add_c(c[x],c[y]);
        add_c(c[y],c[x]);
        du[c[x]]++;
        du[c[y]]++;
    }
    for(int i=1;i<=dcc;i++)
    {
        if(du[i]==2)
            ans++;
    }
    if(ans==0)
        cout<<0<<endl;
    else
    cout<<(ans+1)/2<<endl;
    }
}

一开始想到把桥的边再加一条,然而想了想不是最优,后来画了画图想把每个度为一的点都一一链接起来,然而一直WA,后来多画了几个点才发现它就使想要个联通图,把每个度为1的点都消灭就行了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值