Critical Links
64-bit integer IO format: %lld Java class name: Main
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 first line contains a positive integer (possibly 0) which is the number of network servers. The next lines, one for each server in the network, are randomly ordered and show the way servers are connected. The line corresponding to serverk, , 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 . 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
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
Source
1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 using namespace std; 4 const int maxn = 10010; 5 struct arc{ 6 int u,v,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 u = x; 9 v = y; 10 next = z; 11 } 12 }e[500000]; 13 int head[maxn],dfn[maxn],low[maxn]; 14 int tot,idx,n,m; 15 vector< pii >ans; 16 void add(int u,int v){ 17 e[tot] = arc(u,v,head[u]); 18 head[u] = tot++; 19 } 20 void tarjan(int u,int fa){ 21 dfn[u] = low[u] = ++idx; 22 bool flag = true; 23 for(int i = head[u]; ~i; i = e[i].next){ 24 if(e[i].v == fa && flag){ 25 flag = false; 26 continue; 27 } 28 if(!dfn[e[i].v]){ 29 tarjan(e[i].v,u); 30 low[u] = min(low[u],low[e[i].v]); 31 if(low[e[i].v] > dfn[u]) ans.push_back(make_pair(min(e[i].v,e[i].u),max(e[i].u,e[i].v))); 32 }else low[u] = min(low[u],dfn[e[i].v]); 33 } 34 } 35 int main(){ 36 int u,v; 37 while(~scanf("%d",&n)){ 38 ans.clear(); 39 memset(head,-1,sizeof(head)); 40 memset(low,0,sizeof(low)); 41 memset(dfn,0,sizeof(dfn)); 42 for(int i = tot = 0; i < n; ++i){ 43 scanf("%d (%d)",&u,&m); 44 while(m--){ 45 scanf("%d",&v); 46 add(u,v); 47 } 48 } 49 for(int i = 1; i <= n; ++i) 50 if(!dfn[i]) tarjan(i,-1); 51 printf("%d critical links ",ans.size()); 52 sort(ans.begin(),ans.end()); 53 for(int i = 0; i < ans.size(); ++i) 54 printf("%d - %d ",ans[i].first,ans[i].second); 55 puts(""); 56 } 57 return 0; 58 }