UVA 796 - Critical Links (桥 tarjan)

In a computer network a link L, which interconnects two servers, is considered critical if there are atleast 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–networkare interconnected. For example, the network shown in figure 1 has three critical links that are markedbold: 0 -1,3 - 4and6 - 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 withthe 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 andhas the format:

no of servers
server
0 (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 networkservers. The next no of servers lines, one for each server in the network, are randomly ordered andshow 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 toserverk . Servers are represented by integers from 0 to no of servers 1. Input data are correct. Thefirst data set from sample input below corresponds to the network in figure 1, while the second dataset specifies an empty network.

Output

The result of the program is on standard output. For each data set the program prints the number ofcritical links and the critical links, one link per line, starting from the beginning of the line, as shownin 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. 



裸求桥题。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int mp[maxn][maxn];
int DFN[maxn],low[maxn],inq[maxn];
pair<int,int> p[maxn*maxn];
int ans,tm;
vector<int>G[maxn];
void init()
{
    for(int i=0;i<maxn;i++) G[i].clear();
    ans=0;
    tm=0;
    memset(DFN,0,sizeof DFN);
    memset(low,0,sizeof low);
    memset(mp,0,sizeof mp);
}
void add(int u,int v)
{
    G[u].push_back(v);
}
void tarjan(int u,int pre)
{
    DFN[u]=low[u]=++tm;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==pre) continue;
        if(DFN[v]==0)
        {
            tarjan(v,u);
            if(low[u]>low[v])
                low[u]=low[v];
            if(low[v]>DFN[u])
            {
                ans++;
                int vv=v,uu=u;
                if(uu>vv) swap(uu,vv);
                p[ans].first=uu,p[ans].second=vv;
            }
        }
        else if(low[u]>DFN[v])
            low[u]=DFN[v];
    }
}
int main()
{
    int m;
    int v;
    while(~scanf("%d",&m))
    {
        init();
        int n=-1;
        while(m--)
        {
         
            int u,num;
            scanf("%d",&u);
            n=max(n,u);
            char c1,c2;
            cin>>c1>>num>>c2;
            for(int i=1;i<=num;i++)
            {
                scanf("%d",&v);
                n=max(n,v);
                if(mp[u][v]==0)
                {
                    add(u,v);
                    add(v,u);
                    mp[u][v]=mp[v][u]=1;
                }
            }
        }
        for(int i=0;i<=n;i++) if(!DFN[i]) tarjan(i,i);
        printf("%d critical links\n",ans);
        sort(p+1,p+1+ans);
        for(int i=1;i<=ans;i++)
            printf("%d - %d\n",p[i].first,p[i].second);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值