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 ...
随机推荐
- centos6下时间同步(ntp)操作
1.时间同步的重要性 时间同步可以保证业务的正常运行,比如数据同步,比如系统计划任务的批量执行.等. 2.查看自己的系统时间. [root@localhost ~]# date 3.系统修改时间 # ...
- 演示-JQuery关系选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- chrome浏览器默认启动时打开2345导航的解决方法
2345并没有改动chrome内部设置.它仅仅是把全部的快捷方式改动了.包含開始菜单旁边的快捷启动图标. 仅仅须要右键chrome快捷方式.在目标一栏中,把"----chrome.exe&q ...
- 关于java之socket输入流输出流可否放在不同的线程里进行处理
2014年2月20日到叫(黑土)(人士)的公司去面试,一家新成立的公司.刚去公司是他们新聘请的猎头A来面试我的,A面试完之后是一个号称X总的年轻人来面试我,初一见此人有点邋遢,穿着西装. X:&quo ...
- python2.0_s12_day12_html介绍
html 就像一个裸体的人css 就像是人穿的衣服js 就像是人做的动作一.网页文件HTML的构成 1.对应规则的选择,就如同我们写python时#!/usr/bin/env python3.5 这么 ...
- shell基础篇(二)-shell变量
1. 定义变量 1).定义变量时,变量名不加美元符号($),如: var="hello world"2).注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同 ...
- docker中文、手册、教程
Docker资源 Docker官方英文资源: docker官网:http://www.docker.com Docker windows入门:https://docs.docker.com/windo ...
- 使用HTML5 的跨域通信机制进行数据同步
离线应用系统的设计目标就是在网络离线情况下依然可以操作我们的应用系统,并在网络畅通的情况下与服务器进行数据交互. 所以离线应用系统最终会做成类似C/S架构的客户端应用程序.这边基于Chrome或者 S ...
- oracle中怎么用normal方式登录怎么自定义用户名和密码
1.首先要创建一个用户.必须使用有最高权限的用户来创建,语句如下: create user shopping identified by 123456;--创建shopping用户,密码123456 ...
- 5个基于Web的建模工具
本文介绍 5 款很棒的直接可以在浏览器使用的建模工具,无需单独安装软件. 1. Creately提供在线图表和协助功能,包含多种建模语言(UML)支持,这里有一个简单的演示:here 2.Diagra ...