[kuangbin带你飞]专题九 连通图 Critical Links UVA

来源:互联网 发布:c语言运行快捷键 编辑:程序博客网 时间:2024/04/30 19:51

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<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=20000;int cnt=0,head[maxn],low[maxn],dfn[maxn],stack[maxn],instack[maxn];int top,indexx,belong[maxn],du[maxn],vis[maxn];int bridge=0,block=0;void init(){    top=indexx=0;    memset(belong,0,sizeof(belong));    memset(du,0,sizeof(du));    cnt=0;bridge=block=0;    memset(head,-1,sizeof(head));    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    memset(stack,0,sizeof(stack));    memset(instack,0,sizeof(instack));    memset(belong,0,sizeof(belong));    memset(du,0,sizeof(du));    memset(vis,0,sizeof(vis));}struct node{    int v,next;    int cut;}edge[maxn];struct A{    int u,v;}ab[maxn*2];void add_edge(int u,int v){    edge[cnt].v=v;    edge[cnt].cut=0;    edge[cnt].next=head[u];    head[u]=cnt++;}int flag=0;void tarjan(int u,int pre){    low[u]=dfn[u]=++indexx;    stack[top++]=u;    instack[u]=1;    int v;    for(int i=head[u];i!=-1;i=edge[i].next)    {        v=edge[i].v;        if(v==pre)        {            continue;        }        if(!dfn[v])        {            tarjan(v,u);            low[u]=min(low[u],low[v]);            if(low[v]>dfn[u])            {                bridge++;                edge[i].cut=1;            }        }        else if(instack[v])        {            low[u]=min(low[u],dfn[v]);        }    }}int cmp(A aa,A bb){    if(aa.u==bb.u)        return aa.v<bb.v;    return aa.u<bb.u;}int main (){    int n;    while(~scanf("%d",&n))    {        init();        flag=0;        for(int i=1;i<=n;i++)        {            int xx,yy;            scanf("%d (%d)",&xx,&yy);            while(yy--)            {                int zz;                scanf("%d",&zz);                add_edge(xx,zz);                add_edge(zz,xx);            }        }        for(int i=0;i<n;i++)        {            if(!dfn[i])            {                tarjan(i,i);            }        }        printf("%d critical links\n",bridge);        int tt=0;        for(int i=0;i<n;i++)        {            for(int j=head[i];j!=-1;j=edge[j].next)            {                int v=edge[j].v;                if(edge[j].cut)                {                    if(v>i)                    {                        ab[tt].u=i;                        ab[tt++].v=v;                    }                    else                    {                        ab[tt].u=v;                        ab[tt++].v=i;                    }                    edge[j].cut=edge[i^1].cut=0;                }            }        }        sort(ab,ab+tt,cmp);        for(int i=0;i<tt;i++)        {            printf("%d - %d\n",ab[i].u,ab[i].v);        }        printf("\n");    }}
阅读全文
0 0
原创粉丝点击