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 ...
随机推荐
- Kafka学习之一深度解析
背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能 高吞吐 ...
- 使用MFC WinInet进行FTP中文件的简单上传和下载功能
建立基于对话框的MFC应用程序CMfcFtpWinInetDlg: 1.首先Dlg类中包含头文件 #include "afxinet.h" 2.添加成员变量: C++ Code ...
- failed to push some refs to 'git@github.com:*/learngit.git'
https://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html 出现错误的主要原因是github中的README.md文件不在本地代码目 ...
- SQLServer------如何让标识列重新开始计算
方法: DBCC CHECKIDENT (表名, RESEED, )
- Spring------Spring boot data jpa的使用方法
1.DoMain.java import org.springframework.boot.SpringApplication; import org.springframework.boot.aut ...
- laravel 查询构建器(连贯操作)
注:laravel 查询返回行的都是 php 的 stdClass 对象实例,不是数组!!!! 1)查询多行(get) DB::table('table_name')->get(); 带偏移和限 ...
- php导出excel(xls或xlsx)(解决长数字显示问题)
1)demo $titles = array('订单号','商品结算码','合同号','供应商名称','专柜','商品名称','商品货号','商品单价','商品总价','供应商结算金额','商品数量' ...
- SVN迁移及备份的方法【转】
转自: http://spiritfrog.iteye.com/blog/448578 + http://magnet2008.iteye.com/blog/586578 备份策略 ========= ...
- thinkjs——两表联查
问题来源: 现有一张texture以及一张tradename表,两者的联系是texture表中有一字段名为tid对应tradename表中的id,而tradename表中却有一字段type,要求根据t ...
- DiscuzX的目录权限设置1
经常有朋友遇到Discuz目录权限设置出错的问题,网上千奇百怪的教程非常多,所谓的终极安全的教程更是满天飞,各种所谓的安全加强软件也随处可见,可实际过程中发现,老手用不上,新手则只会因为这些东西徒增麻 ...