zoukankan      html  css  js  c++  java
  • POJ3352 Road Construction(边双连通分量)

                                                                                                                                                 Road Construction
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11210   Accepted: 5572

    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

    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
    
    
    3 3
    1 2
    2 3
    1 3

    Sample Output

    2
    0
    【分析】本题就是问最少加多少边使得它成为边双连通图(边连通度>1)。
    先用Tarjan模板将各双连通分量缩点,然后ans=(度数为一的缩点数+1)/2。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <queue>
    #include <vector>
    #define inf 0x7fffffff
    #define met(a,b) memset(a,b,sizeof a)
    typedef long long ll;
    using namespace std;
    const int N = 1005;
    const int M = 25005;
    int s,t,n,m,cnt,tim,top,cut,k;
    int head[M],dfn[M],low[M],stack1[M];
    int num[M],in[M],out[M],vis[M],w[N][N];
    bool flag=false;
    struct man {
        int to,nxt;
    } edg[M];
    void addedg(int u,int v) {
        edg[cnt].to=v;
        edg[cnt].nxt=head[u];
        head[u]=cnt++;
        edg[cnt].to=u;
        edg[cnt].nxt=head[v];
        head[v]=cnt++;
    }
    void init() {
        cnt=0;
        tim=0;
        top=cut=k=0;
        memset(head,-1,sizeof head);
        memset(dfn,0,sizeof dfn);
        memset(low,0,sizeof low);
        memset(stack1,0,sizeof stack1);
        memset(num,0,sizeof num);
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        memset(vis,0,sizeof vis);
        memset(edg,0,sizeof edg);
        memset(w,0,sizeof w);
    }
    void Tarjan(int u,int fa) {
        int v;
        low[u] = dfn[u] = ++tim;
        vis[u] = 1;
        for(int e = head[u]; e != -1; e = edg[e].nxt) {
            v = edg[e].to;
            if(!dfn[v]) {
                Tarjan(v,u);
                low[u] = min(low[u], low[v]);
            } else if(vis[v]&&v!=fa) {
                low[u] = min(low[u], dfn[v]);
            }
        }
    
    }
    int main() {
        int T,u,v;
        while(~scanf("%d%d",&n,&m)) {
            init();
            for(int i=0; i<m; i++) {
                scanf("%d%d",&u,&v);
                addedg(u,v);
            }
            Tarjan(1,1);
            for(int i=1; i<=n; i++) {
                for(int j=head[i]; j!=-1; j=edg[j].nxt) {
                    int v=edg[j].to;
                    if(low[i]!=low[v])out[low[i]]++,out[low[v]]++;
                    cut=max(cut,low[i]);
                    //printf("!!!%d %d
    ",i,low[i]);
                }
            }
            int son=0;
            for(int i=1; i<=cut; i++) {
                if(out[i]==2)son++;
            }//printf("here
    ");
           printf("%d
    ",(son+1)/2);
        }
        return 0;
    }
  • 相关阅读:
    [置顶] Android自定义控件大全
    与机房收费系统图的初步情结
    队列用链表实现(建立,插入新元素,删除元素,读取元素,全部删除,全部读出,判断是否为空,清空)
    数据结构 练习 16-动态规划
    windows和linux在建筑python集成开发环境IDE
    圆角盒演习(1)
    tortoise svn无法识别subversion check向下代码来解决
    CSS+DIV+JQuery实际的视频汇总
    【Android开发经验】Android举UI设计经验
    程序猿什么样的角色代表了这个号码?你想过这个问题?
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5959513.html
Copyright © 2011-2022 走看看