码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 5292 Critical Links

时间:2015-04-05 14:37:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

Critical Links

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVALive. Original ID: 5292
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 program reads sets of data from a text file. Each data set specifies the structure of a network and has the format:

 


技术分享

技术分享

...

技术分享

 


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

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

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\n",ans.size());
52         sort(ans.begin(),ans.end());
53         for(int i = 0; i < ans.size(); ++i)
54             printf("%d - %d\n",ans[i].first,ans[i].second);
55         puts("");
56     }
57     return 0;
58 }
View Code

 

UVALive 5292 Critical Links

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4394060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!