zoukankan      html  css  js  c++  java
  • POJ 3352 Road Construction

    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
    ",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
    ",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
    ",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;
    }
    
  • 相关阅读:
    SASS(Syntactically Awesome Stylesheets Sass)绝对新手入门教程 java程序员
    android不同Activity之间的数据共享 java程序员
    响应式的前端框架 Groundwork java程序员
    分享网页加载速度优化的一些技巧? java程序员
    超棒的微软Metro风格Logo设计 java程序员
    删除DataTable中除指定行以外的行
    C#递归计算树形菜单 小小西
    记录我的不足一个周【当做故事看,我经常就是在圆子里找故事看的!】
    报告论文:程控交换技术的研究
    技巧心得:VBS学习心得~~
  • 原文地址:https://www.cnblogs.com/sssy/p/7895226.html
Copyright © 2011-2022 走看看