• UVA796:Critical Links(输出桥)


    Critical Links

    题目链接:https://vjudge.net/problem/UVA-796

    Description:

    In a computer network a link L, which interconnects two servers, is considered critical if there are at least two servers A and B such that all network interconnection paths between A and B pass through L. Removing a critical link generates two disjoint sub–networks such that any two servers of a sub–network are interconnected. For example, the network shown in figure 1 has three critical links that are marked bold: 0 -1, 3 - 4 and 6 - 7. Figure 1: Critical links It is known that: 1. the connection links are bi–directional; 2. a server is not directly connected to itself; 3. two servers are interconnected if they are directly connected or if they are interconnected with the same server; 4. the network can have stand–alone sub–networks. Write a program that finds all critical links of a given computer network.

    Input:

    The program reads sets of data from a text file. Each data set specifies the structure of a network and has the format: no of servers server0 (no of direct connections) connected server . . . connected server . . . serverno of servers (no of direct connections) connected server . . . connected server The first line contains a positive integer no of servers(possibly 0) which is the number of network servers. The next no of servers lines, one for each server in the network, are randomly ordered and show the way servers are connected. The line corresponding to serverk, 0 ≤ k ≤ no of servers − 1, specifies the number of direct connections of serverk and the servers which are directly connected to serverk. Servers are represented by integers from 0 to no of servers − 1. Input data are correct. The first data set from sample input below corresponds to the network in figure 1, while the second data set specifies an empty network.

    Output:

    The result of the program is on standard output. For each data set the program prints the number of critical links and the critical links, one link per line, starting from the beginning of the line, as shown in the sample output below. The links are listed in ascending order according to their first element. The output for the data set is followed by an empty line.

    Sample Input:

    8

    0 (1) 1

    1 (3) 2 0 3

    2 (2) 1 3

    3 (3) 1 2 4

    4 (1) 3

    7 (1) 6

    6 (1) 7

    5 (0)

    0

    Sample Output:

    3 critical links

    0 - 1

    3 - 4

    6 - 7

    0 critical links

    题意:

    给出一个无向图,输出桥的个数以及哪些是桥,注意按升序输出。

    题解:

    利用时间戳来求桥,还是比较好理解的。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    const int N = 10005,M = 1000005;
    int n;
    map <int,map<int,int> > mp;
    int head[N];
    struct Edge{
        int u,v,next;
    }e[M<<1];
    int T,tot;
    int dfn[N],low[N],cut[N],bri[M<<1];
    void adde(int u,int v){
        e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
    }
    void init(){
        T=0;tot=0;
        memset(head,-1,sizeof(head));
        memset(cut,0,sizeof(cut));
        memset(dfn,0,sizeof(dfn));
        memset(bri,0,sizeof(bri));
    }
    void Tarjan(int u,int pre){
        dfn[u]=low[u]=++T;
        int son=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(v==pre) continue ;
            if(!dfn[v]){
                son++;//起点有效儿子
                Tarjan(v,u);
                low[u]=min(low[u],low[v]);
                if(low[v]>=dfn[u]&&u!=pre)cut[u]=1;
                if(low[v]>dfn[u]){
                    bri[i]=1;bri[i^1]=1;
                }
            }else{
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(u==pre && son>1) cut[u]=1;
    }
    int main(){
        while(scanf("%d",&n)!=EOF){
            init();
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++) mp[i][j]=0;
            for(int i=1;i<=n;i++){
                int u,v,m;
                scanf("%d (%d)",&u,&m);
                ++u;
                for(int j=1;j<=m;j++){
                    scanf("%d",&v);
                    ++v;
                    mp[u][v]=mp[v][u]=1;
                }
            }
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    if(mp[i][j]) adde(i,j),adde(j,i);
                }
            }
            for(int i=1;i<=n;i++){
                if(!dfn[i]) Tarjan(i,i);
            }
            set <pair<int,int> >S;
            for(int i=0;i<tot;i++){
                if(bri[i]){
                    int u=e[i].u,v=e[i].v;
                    if(u>v)swap(u,v);
                    S.insert(make_pair(u-1,v-1));
                }
            }
            printf("%d critical links
    ",(int)S.size());
            for(auto v:S){
                cout<<v.first<<" - "<<v.second<<endl;
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结
    atitit.php 流行框架 前三甲为:Laravel、Phalcon、Symfony2 attilax 总结
    Atitit.执行cmd 命令行 php
    Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本
    atitit。企业组织与软件工程的策略 战略 趋势 原则 attilax 大总结
    atitit. 管理哲学 大毁灭 如何防止企业的自我毁灭
    Atitit.java的浏览器插件技术 Applet japplet attilax总结
    Atitit.jquery 版本新特性attilax总结
    Atitit. 软件开发中的管理哲学一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向
    (转)获取手机的IMEI号
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10393163.html
Copyright © 2020-2023  润新知