POJ 3352 Road Construction

Description

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


题目翻译:
问题描述:
为了保护放牧环境,避免牲畜过度啃咬同一个地方的草皮,牧场主决定利用不断迁移牲畜进行喂养的方法去保护牧草。然而牲畜在迁移过程中也会啃食路上的牧草,所以如果每次迁移都用同一条道路,那么该条道路同样会被啃咬过度而遭受破坏。
现在牧场主拥有F个农场,已知这些农场至少有一条路径连接起来(不一定是直接相连),但从某些农场去另外一些农场,至少有一条路可通行。为了保护道路上的牧草,农场主希望再建造若干条道路,使得每次迁移牲畜时,至少有2种迁移途径,避免重复走上次迁移的道路。已知当前有的R条道路,问农场主至少要新建造几条道路,才能满足要求?
输入描述
第一行2个正整数,分别为n和m
以下m行,每行2个数,表示连接的编号
输出描述:
一行一个数,表示至少新建的道路数目。
这篇不错,存下来,给译者一个赞。
先tarjan缩点,然后记录所有度为一的点个数为cnt
ans=(cnt+1)/2;
(我是不会告诉你我也不知道为什么的)


偷偷贴代码

#include<iostream>
#include<vector>
#include<cstdio>
#include<stack>
using namespace std;
vector<int>grp[1001];
stack<int>stk;
int n,r,ans,tim,dcnt,x[1001],y[1001],belong[1001],du[1001],dfn[1001],low[1001];
void dfs(int u,int fa)
{
    int tmp;
    stk.push(u);
    dfn[u]=low[u]=++tim;
    for(int i=0;i<grp[u].size();++i)
    {
        int v=grp[u][i];
        if(dfn[v]==0)
        {
            dfs(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(v!=fa)
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        ++dcnt;
        while(1)
        {
            tmp=stk.top();
            stk.pop();
            belong[tmp]=dcnt;
            if(u==tmp)
                break;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&r);
    for(int i=1;i<=r;++i)
    {
        scanf("%d%d",&x[i],&y[i]);
        grp[x[i]].push_back(y[i]);
        grp[y[i]].push_back(x[i]);
    }
    dfs(1,0);
    for(int i=1;i<=r;++i)
        if(belong[x[i]]!=belong[y[i]])
        {
            du[belong[x[i]]]++;
            du[belong[y[i]]]++;
        }
    for(int i=1;i<=dcnt;++i)
        if(du[i]==1)
            ans++;
    printf("%d\n",(ans+1)/2);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值