POJ 3352 Road Construction(图论-tarjan)

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

CCC 2007

题目大意:

给定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

时间: 2024-10-05 08:03:50

POJ 3352 Road Construction(图论-tarjan)的相关文章

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

POJ 3352 Road Construction 使得无向图边变双连通图

点击打开链接 Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8168   Accepted: 4106 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

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 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 ro

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 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 r

POJ 3352 Road Construction(无向连通图)

题目地址:POJ 3352 这题就是求最少加多少条边可以使得图为无向双连通图.方法是找度数为1的连通分量,可以只用low来找.然后根据公式(叶子结点数+1)/2即可得到答案.原因是在图中将每两个度数为1的都连起来,度数为2的显然已经可以形成双联通了,因为是无向边,只要加一条,就相当于加了两条有向边,很显然,结果数就是那个公式. 代码如下: #include <iostream> #include <cstdio> #include <string> #include &

POJ 3352 Road Construction(边双连通分量,桥,tarjan)

题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业想把一个热带天堂岛变成旅游胜地,岛上有N个旅游景点,保证任意2个旅游景点之间有路径连通的(可间接连通).而为了给游客提供更方便的服务,该企业要求道路部门在某些道路增加一些设施. 道路部门每次只会选择一条道路施工,在该条道路施工完毕前,其他道路依然可以通行.然而有道路部门正在施工的道路,在施工完毕前是

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

POJ 3352 Road Construction(边—双连通分量)

http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相等,说明属于同一个双连通分量. 接下来把连通分量缩点,然后把这些点连边. 对于一棵无向树,我们要使得其变成边双连通图,需要添加的边数 == (树中度数为1的点的个数+1)/2. 1 #include<iostream> 2 #include<algorithm> 3 #include&