UVA796 - Critical Links(Tarjan求桥)
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
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737
给你一个图,让你求这个图中哪些是桥,并输出;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 10005
#define met(a, b) memset(a, b, sizeof(a)) int dfn[N], low[N], Time, ans;
int n, f[N];
vector<vector<int> >G; struct node
{
int x, y;
bool friend operator < (node A,node B)
{
if(A.x == B.x)
return A.y < B.y;
return A.x < B.x;
}
}a[N]; void Init()
{
met(dfn, );
met(low, );
met(f, );
met(a, );
G.clear();
G.resize(n+);
Time = ;
} void Tarjan(int u, int fa)
{
low[u] = dfn[u] = ++Time;
f[u] = fa;
int len = G[u].size(), v;
for(int i=; i<len; i++)
{
v = G[u][i];
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]); if(low[v] > dfn[u])///判断是否是桥;
{
a[ans].x = u;
a[ans].y = v;
if(a[ans].x>a[ans].y)swap(a[ans].x, a[ans].y);
ans++;
}
}
else if(fa != v)
low[u] = min(dfn[v], low[u]);
}
} int main()
{
while(scanf("%d", &n) != EOF)
{
Init(); int u, v, m; for(int i=; i<n; i++)
{
scanf("%d (%d)", &u, &m);
for(int j=; j<m; j++)
{
scanf("%d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
}
ans = ; for(int i=; i<n; i++)
if(!dfn[i])
Tarjan(i, -); sort(a, a+ans); printf("%d critical links\n", ans);
for(int i=; i<ans; i++)
printf("%d - %d\n", a[i].x, a[i].y);
printf("\n");
}
return ;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 10005
#define met(a, b) memset(a, b, sizeof(a)) int dfn[N], low[N], Time;
int n, f[N];
vector<vector<int> >G; struct node
{
int x, y;
bool friend operator < (node A,node B)
{
if(A.x == B.x)
return A.y < B.y;
return A.x < B.x;
}
}a[N]; void Init()
{
met(dfn, );
met(low, );
met(f, );
met(a, );
G.clear();
G.resize(n+);
Time = ;
} void Tarjan(int u, int fa)
{
low[u] = dfn[u] = ++Time;
f[u] = fa;
int len = G[u].size(), v;
for(int i=; i<len; i++)
{
v = G[u][i];
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(fa != v)
low[u] = min(dfn[v], low[u]);
}
} int main()
{
while(scanf("%d", &n) != EOF)
{
Init(); int u, v, m; for(int i=; i<n; i++)
{
scanf("%d (%d)", &u, &m);
for(int j=; j<m; j++)
{
scanf("%d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
} for(int i=; i<n; i++)
if(!dfn[i])
Tarjan(i, -); int ans = ; for(int i=; i<n; i++)
{
v = f[i];
if(v!=- && low[i]>dfn[v])
{
a[ans].x = i;
a[ans].y = v;
if(a[ans].x>a[ans].y)swap(a[ans].x, a[ans].y);
ans++;
}
}
sort(a, a+ans); printf("%d critical links\n", ans);
for(int i=; i<ans; i++)
printf("%d - %d\n", a[i].x, a[i].y);
printf("\n");
}
return ;
}
UVA796 - Critical Links(Tarjan求桥)的更多相关文章
- UVA796 Critical Links(求桥) 题解
题意:求桥 思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u] 所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边).我们在进行dfs的时候,要加入一个fa表示当前进行搜 ...
- uva-796.critical links(连通图的桥)
本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...
- UVA 796 - Critical Links (求桥)
Critical Links In a computer network a link L, which interconnects two servers, is considered criti ...
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA796 Critical Links —— 割边(桥)
题目链接:https://vjudge.net/problem/UVA-796 In a computer network a link L, which interconnects two serv ...
- Tarjan 求桥,割,强连通
最近遇到了这种模板题,记录一下 tarjan求桥,求割 #include <bits/stdc++.h> using namespace std; #define MOD 99824435 ...
- Tarjan求桥
传送门(poj3177) 这道题是Tarjan求桥的模板题.大意是要求在原图上加上数量最少的边,使得整张图成为一个边双联通分量. 具体的做法是,先在图中求出所有的桥,之后把边双联通分量缩成点,这样的话 ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- uva 796 C - Critical Links(tarjan求桥)
题目链接:https://vjudge.net/contest/67418#problem/C 题意:求出桥的个数并且按顺序输出 题解:所谓桥就是去掉这条边后连通块增加,套用一下模版就行. #incl ...
随机推荐
- HDU 1556 Color the ball 树状数组 题解
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...
- QWidget:Must construct a QApplication before a QWidget。
异常描述: 用PyQt开发的界面程序,再新增加了几个module后, 在eric6开发环境下启动后什么都没出现,什么错误提示也都没有, 在控制台下:python XXXX.py 后显示 QWidg ...
- asp.net mvc中用angularJs写的增删改查的demo。初学者,求指点。。
直接给个代码下载链接.... http://pan.baidu.com/s/1FfVgq 本人刚刚学习angularJs,感觉双向数据绑定蛮爽的... 之前的代码存在点问题,已修复
- apache Storm学习之三-消息可靠性
4.1 简介 storm可以确保spout发送出来的每个消息都会被完整的处理.本章将会描述storm体系是如何达到这个目标的,并将会详述开发者应该如何使用storm的这些机制来实现数据的可靠处理. 4 ...
- iperf/netperf网络性能测试工具、Wireshark网络包分析工具
iperf http://www.linuxidc.com/Linux/2014-05/101160.htm netperf http://www.linuxidc.com/Linux/2013 ...
- MathType中输入破折号的教程
MathType公式编辑器中的包含的各种数学符号与模板已经足够我们在编辑公式时使用了,但是除此之外,MathType还有一些符号并不是数学专有的符号,但是在数学中也偶尔会用到,比如破折号.MathTy ...
- VR资源浏览网站
https://my.matterport.com 资源 https://my.matterport.com/show/?m=kCeVCzCjQ5s
- c++ 类数据成员的定义、声明
C++为类中提供类成员的初始化列表类对象的构造顺序是这样的:1.分配内存,调用构造函数时,隐式/显示的初始化各数据成员2.进入构造函数后在构造函数中执行一般计算 1.类里面的任何成员变量在定义时是不 ...
- 上千万或上亿数据(有反复),统计当中出现次数最多的N个数据. C++实现
上千万或上亿的数据,如今的机器的内存应该能存下.所以考虑採用hash_map/搜索二叉树/红黑树等来进行统计次数. 然后就是取出前N个出现次数最多的数据了,能够用第2题提到的堆机制完毕. #inclu ...
- ios button标记
在写项目的时候,for循环创建多个button,在需要设置背景图片和,需要标记所选中的button的需求, 在这里提供两种方法: 一: 1:把for循环创建的button全部装到一个新建的数组中,把他 ...