UVALive 5292 Critical Links

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

Regionals 1997, Europe - Southeastern

解题:求割边

 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 }

时间: 2024-10-07 13:42:35

UVALive 5292 Critical Links的相关文章

[UVA796]Critical Links(割边, 桥)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求桥的数量,也就是割边的数量.输入有点小坑,左右括号外必须得有个空格才行,起初以为是转义的问题. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8

UVA796 - 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 disjoin

UVA 796 - Critical Links【求桥】

link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 题意: 求桥的数目及边,要求输出边的点的次序由小到大 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include

UVA 796 Critical Links

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include &l

Light OJ 1026 - Critical Links (图论-求割边, 桥)

题目大意:双向联通图, 现在求减少任意一边使图的联通性改变,按照起点从小到大列出所有这样的边 解题思路:双向边模版题 tarjan算法 代码如下: #include<bits/stdc++.h> using namespace std; const int N = 100003; vector<int>vec[N]; pair<int, int>edge[N]; int dfn[N], low[N]; int res, ans; void tarjan(int u, i

UVA 796 Critical Links —— (求割边(桥))

和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混了.. 另外从这题发现,代码还是写成模块化比较好,比如solve一个函数,init一个函数等等,这样可以避免很多东西忘记写,比方说dfn或者G的清空等等.. 代码如下: 1 #include <stdio.h> 2 #include <stack> 3 #include <alg

UVA 796 Critical Links(无向图求桥)

题目来源: UVa Online Judgehttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求一个连通图中必不可少的路径: #include<stdio.h> #include<algorithm> #include<vector> #include<string.h> #define

Light OJ 1026 Critical Links 求桥

题目 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3]

UVA 796 - Critical Links (求桥按序输出)

tanjar求图中的桥,然后排序输出. 代码: #include<iostream> #include<cstdio> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<cstring> #include<algorithm> #define rep(i,a,b) for(i