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

POJ 3352 Road Construction

时间:2017-11-25 15:21:29      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:eve   sla   ons   clu   ges   down   limit   cstring   att   

Road Construction
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12646   Accepted: 6384

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

某同学告诉我他出了道题,我做了,他说没数据
他给我在poj交了,GG
据题意可知,每两个点都必须在一个简单环中,有割边的话一定会走两次
由于不知道双联通分量,我求出所有割边,删掉割边后缩点,再把连上割边,这时变成一棵树,
要让树上点都位于简单环中,就是把叶子节点连起来,叶子几点就是度为1的点
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    while(c<0||c>9) {
        if(c==-)f=-1;
        c=getchar();
    }
    while(c<=9&&c>=0) {
        x=x*10+c-0;
        c=getchar();
    }
    return x*f;
}
int n,m;
const int maxn = 500007;
struct node{
    int u,v,next;
}edge[maxn];
int head[maxn];
int num=1,N=0;
void add_edge(int a,int b) {
    edge[++num].u=a,edge[num].v=b;edge[num].next=head[a],head[a]=num;
}
int DFN[maxn],LOW[maxn],cnt=1;
int father[maxn];
bool tag[maxn];
int whic[maxn],k=0;
void tarjan(int x,int fa) {
    father[x]=fa;
    DFN[x]=LOW[x]=cnt++;
    for(int i=head[x];i;i=edge[i].next) {
        int v=edge[i].v;
        if(v==fa)continue;//重边,不是桥
        if(DFN[v]==-1) {
            tarjan(v,x);
            whic[++k]=i;
            LOW[x]=min(LOW[x],LOW[v]);
        }
        else LOW[x]=min(LOW[x],DFN[v]);
    }
}
int popo[maxn],popo_cnt=0;
void get_bridge() {
    tarjan(1,-1);
    for(int i=1;i<=k;++i) {
        int k=whic[i];
        int u=edge[k].u;
        int v=edge[k].v;
        //printf("%d %d\n",u,v);
        if(u>0&&LOW[v]>DFN[u]) {
            //if(edge[k-1].v==edge[k].u&&edge[k-1].u==edge[k].v)tag[k-1]=1;
            //else tag[k+1]=1;
            tag[k^1]=tag[k]=1;
            popo[++popo_cnt]=k;
            //printf("%d %d\n",u,v);
        }
    }
}
int belong[maxn],color_cnt=0;
void dfs(int x,int fa) {
    belong[x]=color_cnt;
    for(int i=head[x];i;i=edge[i].next) {
        if(tag[i]||edge[i].v==fa) continue;
        int v=edge[i].v;
        if(!belong[v]) {
            dfs(v,x);
        }
    }
}
int rd[maxn];
void work() {
    for(int i=1;i<=n;++i) {
        if(!belong[i]) {
            ++color_cnt;
            belong[i]=color_cnt;
            dfs(i,-1);
        }
    }
    int ans=color_cnt;
//    for(int i=1;i<=n;++i)printf("%d ",belong[i]);
    if(popo_cnt==0)ans--;
    for(int i=1;i<=popo_cnt;++i) {
        int po=popo[i];
        int u=edge[po].u;
        int v=edge[po].v;
        rd[belong[u]]++,rd[belong[v]]++;
        if(rd[belong[u]]==2)ans--;
        if(rd[belong[v]]==2)ans--;
    }
    if(ans&1)ans++;
    ans/=2;
    printf("%d\n",ans);
}
int main() {
    n=read(),m=read();
    int a,b;
    for(int i=1;i<=m;++i) {
        a=read(),b=read();
        add_edge(a,b);
        add_edge(b,a);
    }
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    get_bridge();
    work();
    return 0;
}

 

POJ 3352 Road Construction

标签:eve   sla   ons   clu   ges   down   limit   cstring   att   

原文地址:http://www.cnblogs.com/sssy/p/7895226.html

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