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 ...
随机推荐
- OpenGL模板缓冲区与模板测试
原文地址:http://www.blogjava.net/qileilove/archive/2014/01/23/409269.html 帧缓冲区有许多缓冲区构成,这些缓冲区大致分为: 颜色缓冲区: ...
- glsl 多重纹理
#include"glsl.h" void SHADER::drawBox() { glBegin(GL_QUADS); // Front Face glNormal3f( 0.0 ...
- Tomcat 配置 项目 到tomcat目录外面 和 域名绑定访问(api接口、前端网站、后台管理网站)
先停止tomcat服务 1.进入apache-tomcat-7.0.68/conf/Catalina/localhost(如果之前还都没有启动过tomcat,是不会有此目录的,先启动一次再关闭,会自动 ...
- php 中的 short_open_tag 的作用
在php的配置文件(php.ini)中有一个short_open_tag的值,开启以后可以使用PHP的段标签:(<? ?>). 同时,只有开启这个才可以使用 <?= 以代替 < ...
- html5引擎开发 -- 引擎消息中心和有限状态机 - 初步整理 一
一 什么是有限状态机 FSM (finite-state machine),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.他对于逻辑以及 ...
- C#操作MSMQ(消息队列)
using System; using System.Collections.Generic; using System.Text; using System.Messaging; using Sys ...
- Cocostudio学习笔记(2) Button + CheckBox
这篇记录了两个控件的使用流程:Button 和 CheckBox. ------------------------------------------------------------------ ...
- Mybatis中的foreach
<delete id="deleteByParam"> DELETE FROM YZ_SECURITIES_CURRENCY WHERE ID IN <forea ...
- JavaIO再回顾
File类 JavaIO访问文件名和文件检测相关操作 分隔符最好是使用File类提供的File.separator,使程序更加的健壮. File类提供的方法基本上是见名知意,例如getName()就是 ...
- javascript:;禁用a标签默认功能的缺点。
在使用a标签做切换tab或者其他功能时,经常使用javascript:;来作为a标签的href来使用. 缺点: 1.在js尚未加载的情况下,点击该a标签会弹出新窗口. 2.会使gif动画失效(没经历过 ...