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& ...
随机推荐
- 洛谷 P1156 垃圾陷阱 谈论剪枝,非满分
这是一个91分的非dp代码(是我太弱) 剪枝八五个(实际上根本没那么多,主要是上课装逼,没想到他们dp水过去了),不过我的思路与dp不同: 1.层数到达i+1,return 这个必须有 2.当前剩余生 ...
- 大数据学习——sqoop入门
下载地址 https://pan.baidu.com/s/1qWDl29L9I_KVU54c0ioNfQ fvfh 3.1 概述 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传 ...
- 洛谷P1077 摆花
题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过ai盆,摆花时 ...
- 【置换】G. Poker 2.0
https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/G [题意] 题意很简单,就是“鸽尾式”洗扑克,问洗m次各张牌的位置 [思路] 牌 ...
- 【记录】新建Cordova项目出现ios-deploy找不到的问题
按老流程 Cordova create helloApp Cordova platform add ios 之前一般这种操作之后就能有执行的iOS目录了,像这样 然后 Cordova build ...
- 使用Sencha Architect开发Sencha Touch应用的整理
官网:http://www.sencha.com/ 其实官网上的文档都很清楚了,不过整理一下总比较好 第一步,软件准备 注: 以下软件的安装本着这样两条原则 一是不要安装在中文目录下 二是不要安装在带 ...
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling
题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes onl ...
- 【git】git分支的合并
原文: http://gitbook.liuhui998.com/3_3.html http://gitbook.liuhui998.com/5_3.html 一.如何分支的合并 在git中,可以使用 ...
- 解决Flex4在Module里调用PopUpManager报错问题
项目大了 就需要用到Module, 发现在一个Module里边, 和普通应用里一样popup一个组件的时候, 总是会报错. 这个在Flex3的时候也会出现, 会报样式错误 所以就想到了addPopUp ...
- CSS属性操作一
CSS属性操作 一.CSS text 1.文本颜色:color 颜色属性被用来设置文字的颜色.颜色是通过CSS最经常的指定: • 十六进制值 - 如: #FF0000 • 一个RGB值 - 如: RG ...