码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3352 Road Construction(图论-tarjan)

时间:2014-08-05 22:43:00      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

Road Construction
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8647   Accepted: 4318

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

Source


题目大意:

给定n个点m条边的无向连通图,问你至少添加多少条边,使得这个图去掉任意一条边依然连通。


解题思路:

首先环中的边去掉依然连通,所以环缩成点,然后将度数为1的点肯定要两两相连,否则去掉这个点所在的边就肯定不连通了。

因此,答案就是:(度数为1的点的个数+1)/ 2


解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int maxm=510000;
const int maxn=1100;

struct edge{
    int u,v,next;
    edge(int u0=0,int v0=0){
        u=u0;v=v0;
    }
}e[maxm];

int n,m,head[maxn],color[maxn],dfn[maxn],low[maxn],cnt,nc,index,num[maxn];
bool mark[maxn];
vector <int> vec;

void addedge(int u,int v){
    e[cnt]=edge(u,v);e[cnt].next=head[u];head[u]=cnt++;
}

void input(){
    vec.clear();
    cnt=nc=index=0;
    for(int i=0;i<=n;i++){
        color[i]=head[i]=-1;
        mark[i]=false;
        num[i]=dfn[i]=0;
    }
    for(int i=0;i<m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
}

void tarjan(int s,int father){
    dfn[s]=low[s]=++index;
    mark[s]=true;
    vec.push_back(s);
    for(int i=head[s];i!=-1;i=e[i].next){
        int d=e[i].v;
        if(d==father) continue;
        if(!dfn[d]){
            tarjan(d,s);
            low[s]=min(low[d],low[s]);
        }else if(mark[d]){
            low[s]=min(low[s],dfn[d]);
        }
    }
    if(dfn[s]==low[s]){
        nc++;
        int d;
        do{
            d=vec.back();
            vec.pop_back();
            color[d]=nc;
            mark[d]=false;
        }while(d!=s);
    }
}

void solve(){
    for(int i=1;i<=n;i++){
        if(!dfn[i]) tarjan(i,-1);
    }
    for(int i=0;i<cnt;i++){
        int x=color[e[i].u],y=color[e[i].v];
        if(x!=y){
            num[y]++;
        }
    }
    int ans=0;
    for(int i=1;i<=nc;i++){
        if(num[i]==1) ans++;
    }
    printf("%d\n",(ans+1)/2);
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        input();
        solve();
    }
    return 0;
}



POJ 3352 Road Construction(图论-tarjan),布布扣,bubuko.com

POJ 3352 Road Construction(图论-tarjan)

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/a1061747415/article/details/38390187

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!