UVA796 Critical Links —— 割边(桥)
题目链接:https://vjudge.net/problem/UVA-796
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
题解:
题目要求:按字典序输出桥。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e3+10; 19 20 struct Edge 21 { 22 int to, next; 23 bool cut; 24 }edge[MAXN*MAXN*2]; 25 int tot, head[MAXN]; 26 27 int Index, DFN[MAXN], Low[MAXN]; 28 int bridge; 29 30 void addedge(int u, int v) 31 { 32 edge[tot].to = v; 33 edge[tot].next = head[u]; 34 edge[tot].cut = false; 35 head[u] = tot++; 36 } 37 38 void Tarjan(int u, int pre) 39 { 40 DFN[u] = Low[u] = ++Index; 41 for(int i = head[u]; i!=-1; i = edge[i].next) 42 { 43 int v = edge[i].to; 44 if(v==pre) continue; 45 if(!DFN[v]) 46 { 47 Tarjan(v, u); 48 Low[u] = min(Low[u], Low[v]); 49 if( Low[v]>DFN[u]) 50 { 51 edge[i].cut = edge[i^1].cut = true; 52 bridge++; 53 } 54 } 55 else 56 Low[u] = min(Low[u], DFN[v]); 57 } 58 } 59 60 void init() 61 { 62 bridge = tot = 0; 63 memset(head, -1, sizeof(head)); 64 65 Index = 0; 66 memset(DFN, 0, sizeof(DFN)); 67 memset(Low, 0, sizeof(Low)); 68 } 69 70 int main() 71 { 72 int n; 73 while(scanf("%d", &n)!=EOF) 74 { 75 init(); 76 int u, m, v; 77 for(int i = 1; i<=n; i++) 78 { 79 scanf("%d (%d)", &u, &m); 80 for(int j = 1; j<=m; j++) 81 { 82 scanf("%d", &v); 83 addedge(u, v); 84 addedge(v, u); 85 } 86 } 87 88 for(int i = 0; i<n; i++) 89 if(!DFN[i]) 90 Tarjan(i, i); 91 92 vector<pair<int, int> >a; 93 for(int u = 0; u<n; u++) 94 for(int i = head[u]; i!=-1; i = edge[i].next) 95 { 96 if(edge[i].cut && u<edge[i].to) 97 a.push_back(make_pair(u, edge[i].to)); 98 } 99 100 sort(a.begin(), a.end()); 101 printf("%d critical links\n", bridge); 102 for(int i = 0; i<a.size(); i++) 103 printf("%d - %d\n", a[i].first, a[i].second); 104 printf("\n"); 105 } 106 }