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 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1010;
int mp[maxn][maxn];
int DFN[maxn],low[maxn],inq[maxn];
pair p[maxn*maxn];
int ans,tm;
vectorG[maxn];
void init()
{
    for(int i=0;ilow[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;
}


你可能感兴趣的:(Tarjan)