UVA796- Critical Links(无向图中的桥梁)
题意: 给出一个无向图,按顺序输出桥
思路:求出全部的桥,然后按顺序输出就可以
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm> using namespace std; const int MAXN = 10005; struct Edge{
int to, next;
bool cut;
}edge[MAXN * 10];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN];
int Index, top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[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) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if (v == pre) continue;
if (!DFN[v]) {
son++;
Tarjan(v, u);
if (Low[u] > Low[v]) Low[u] = Low[v];
if (Low[v] > DFN[u]) {
bridge++;
edge[i].cut = true;
edge[i^1].cut = true;
}
if (u != pre && Low[v] >= DFN[u]) {
cut[u] = true;
add_block[u]++;
}
}
else if (Low[u] > DFN[v])
Low[u] = DFN[v];
}
if (u == pre && son > 1) cut[u] = true;
if (u == pre) add_block[u] = son - 1;
Instack[u] = false;
top--;
} void init() {
memset(head, -1, sizeof(head));
memset(DFN, 0, sizeof(DFN));
memset(Instack, false, sizeof(Instack));
memset(add_block, 0, sizeof(add_block));
memset(cut, false, sizeof(cut));
tot = 0;
Index = top = 0;
bridge = 0;
} void solve(int N) {
for (int i = 1; i <= N; i++)
if (!DFN[i])
Tarjan(i, i); printf("%d critical links\n",bridge);
vector<pair<int, int> > ans;
for (int u = 1; u <= N; u++)
for (int i = head[u]; i != -1; i = edge[i].next)
if (edge[i].cut && edge[i].to > u) {
ans.push_back(make_pair(u, edge[i].to));
} sort(ans.begin(), ans.end());
for(int i = 0;i < ans.size();i++)
printf("%d - %d\n",ans[i].first-1,ans[i].second-1);
printf("\n");
} int main() {
int n;
while (scanf("%d", &n) == 1) {
init();
int u, k, v;
for (int i = 1; i <= n; i++) {
scanf("%d (%d)", &u, &k);
u++;
while (k--) {
scanf("%d", &v);
v++;
addedge(u, v);
addedge(v, u);
}
}
solve(n);
}
return 0;
}
UVA796- Critical Links(无向图中的桥梁)的更多相关文章
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- uva-796.critical links(连通图的桥)
本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...
- UVA796 - Critical Links(Tarjan求桥)
In a computer network a link L, which interconnects two servers, is considered critical if there are ...
- UVA796 Critical Links —— 割边(桥)
题目链接:https://vjudge.net/problem/UVA-796 In a computer network a link L, which interconnects two serv ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- [UVA796]Critical Links(割边, 桥)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva796 Critical Links
用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...
- UVA796 Critical Links(求桥) 题解
题意:求桥 思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u] 所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边).我们在进行dfs的时候,要加入一个fa表示当前进行搜 ...
- UVA796:Critical Links(输出桥)
Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, ...
随机推荐
- Get started - UIkit documentation
Get started - UIkit documentation Get started Get familiar with the basic setup and structure of UIk ...
- C#超级有用的一种类型—匿名类型
顾名思义 匿名类型就是没有名字的类型.当一个新的匿名对象定义与前面已经存在的类型定义的内部变量类型同样时,编译器就会仅仅生成一个类定义,而不是各一个. 匿名类型对象中仍然能够再包括匿名对象. 在C#3 ...
- jstl标签经典
1. <c:out> 库 :Core(核心库) URI : http://java.sun.com/jsp/jstl/core 前缀 : c 描述 :<c:out> 标签是一个 ...
- 如何制定AxureRP设计体系
经常有朋友问我,如何在从来没有用过AxureRP的公司或者团队里面开始使用AxureRP做原型设计?这个问题对个体来说不存在,因为个人学习使用AxureRP时非常快速的,基本试着做几个原型实例就能把整 ...
- GDI+: Curved Shapes
原文 http://www.functionx.com/vcsharp2003/gdi/curves.htm Curves Introduction to Curves A curve is ...
- Silverlight技术调查(3)——国际化
原文 Silverlight技术调查(3)——国际化 网上有很多关于Silverlight国际化的说明,包括MSDN的示例,都没有强调一点,下面以红色标示,基础国际化知识请先参考MSDN相关章节,关键 ...
- 能上QQ无法上网
不少网友碰到过或亲身经历过电脑QQ还在登录,并且还能正常聊天,但是却打不开网站网页的情况,而当电脑出现能上qq但是打不开网页是由什么原因引起,我们又该如何解决类似的电脑故障呢. 适用范围及演示工具 适 ...
- javascript——ajax应用
概念 AJAX 指异步JavaScript及XML(Asynchronous JavaScript And XML).Ajax的核心是JavaScript对象XmlHttpRequest.XmlHtt ...
- DRP分销系统总结
上个月看完的分销系统的视频,用了漫长的时间看这个项目视频,能安慰自己的是不光是看视频了,还做了很多自己想做的事情,比如驾照拿下来了,比如参加了一些考试,比如讲了一些课程等等.把这个系统的总结总算是补上 ...
- Windows下用Mingw编译Boost.Regex库
下载Boost库,解压. 定位到regex库文件夹下. GCC所对应的MAKEFILE为gcc.mak 进入命令提示符下,输入make -f gcc.mak 这是如果直接按回车执行的话,会出现错误: ...