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
题解:
题目要求:按字典序输出桥。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; struct Edge
{
int to, next;
bool cut;
}edge[MAXN*MAXN*];
int tot, head[MAXN]; int Index, DFN[MAXN], Low[MAXN];
int bridge; void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = false;
head[u] = tot++;
} void Tarjan(int u, int pre)
{
DFN[u] = Low[u] = ++Index;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(v==pre) continue;
if(!DFN[v])
{
Tarjan(v, u);
Low[u] = min(Low[u], Low[v]);
if( Low[v]>DFN[u])
{
edge[i].cut = edge[i^].cut = true;
bridge++;
}
}
else
Low[u] = min(Low[u], DFN[v]);
}
} void init()
{
bridge = tot = ;
memset(head, -, sizeof(head)); Index = ;
memset(DFN, , sizeof(DFN));
memset(Low, , sizeof(Low));
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
init();
int u, m, v;
for(int i = ; i<=n; i++)
{
scanf("%d (%d)", &u, &m);
for(int j = ; j<=m; j++)
{
scanf("%d", &v);
addedge(u, v);
addedge(v, u);
}
} for(int i = ; i<n; i++)
if(!DFN[i])
Tarjan(i, i); vector<pair<int, int> >a;
for(int u = ; u<n; u++)
for(int i = head[u]; i!=-; i = edge[i].next)
{
if(edge[i].cut && u<edge[i].to)
a.push_back(make_pair(u, edge[i].to));
} sort(a.begin(), a.end());
printf("%d critical links\n", bridge);
for(int i = ; i<a.size(); i++)
printf("%d - %d\n", a[i].first, a[i].second);
printf("\n");
}
}
UVA796 Critical Links —— 割边(桥)的更多相关文章
- [UVA796]Critical Links(割边, 桥)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva-796.critical links(连通图的桥)
本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...
- UVA796 Critical Links(求桥) 题解
题意:求桥 思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u] 所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边).我们在进行dfs的时候,要加入一个fa表示当前进行搜 ...
- UVA796 - Critical Links(Tarjan求桥)
In a computer network a link L, which interconnects two servers, is considered critical if there are ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- Uva 796 Critical Links 找桥
这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...
- Uva796 Critical Links
用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...
- UVA796:Critical Links(输出桥)
Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, ...
- Light OJ 1026 - Critical Links (图论-双向图tarjan求割边,桥)
题目大意:双向联通图, 现在求减少任意一边使图的联通性改变,按照起点从小到大列出所有这样的边 解题思路:双向边模版题 tarjan算法 代码如下: #include<bits/stdc++.h& ...
随机推荐
- POJ 1080 Human Gene Functions 【dp】
题目大意:每次给出两个碱基序列(包含ATGC的两个字符串),其中每一个碱基与另一串中碱基如果配对或者与空串对应会有一个分数(可能为负),找出一种方式使得两个序列配对的分数最大 思路:字符串动态规划的经 ...
- 【HDOJ6333】Harvest of Apples(莫队)
题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...
- castle problem——(深度优先搜索,递归实现和stack实现)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 使用imageMagick 制作圆角矩形和图片加水印
制作圆角矩形好图片水印都是图片合成的操作 composite -gravity southeast mask175.png src.jpg dest.jpg -gravity southeast ...
- Tomcat绑定具体IP
https://blog.csdn.net/paomadeng/article/details/1826880
- (二)Commonjs规范与模块化
在之前的学习中我们使用require()来引入我们需要的包,这其实就是模块化,各模块相互独立,可以通过某种方式引入别的模块.而这些引入方式都是遵循一定的规范的,这就是CommonJS规范. 一.Com ...
- codevs——3064 求和
3064 求和 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 输入一个数x(x <= ...
- MongoDB学习day08--Mongoose索引、Mongoose内置方法、扩展Mongoose Model的静态方法和实例方法
一.Mongoose索引 索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快. MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化 ...
- 系统安全攻防战:DLL注入技术详解
DLL注入是一种允许攻击者在另一个进程的地址空间的上下文中运行任意代码的技术.攻击者使用DLL注入的过程中如果被赋予过多的运行特权,那么攻击者就很有可能会在DLL文件中嵌入自己的恶意攻击代码以获取更高 ...
- Linux系统启动流程分析
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...