uva-796.critical links(连通图的桥)
本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥.
本题思路:注意判重就行了,就是一个桥的裸题.
判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者下半角.
参考代码:
/*************************************************************************
> File Name: uva-796.critical_links.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月06日 星期五 15时58分54秒
本题思路:注意边的判重.
************************************************************************/ #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int maxn = + , maxm = maxn * maxn + ;
int n;
struct Edge {
int to, next;
bool cut;
} edge[maxm];
int head[maxn], tot;
int low[maxn], dfn[maxn],stack[maxn];
int Index, top, bridge;
bool instack[maxn];
bool cut[maxn];
int add_block[maxn]; void addedge(int u, int v) {
edge[tot].to = v; edge[tot].next = head[u]; edge[tot].cut = false;
head[u] = tot ++;
} void init() {
memset(head, -, sizeof head);
tot = ;
} map<int, int> mp;
vector <pair<int, int> > ans; void tarjan(int u, int pre) {
int v;
low[u] = dfn[u] = ++ Index;
instack[u] = true;
int son = ;
int pre_cnt = ;
for(int i = head[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(v == pre && pre_cnt == ) {
pre_cnt ++;
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 ^ ].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 > ) cut[u] = true;
if(u == pre) add_block[u] = son - ;
instack[u] = false;
top --;
} void solve() {
memset(dfn, , sizeof dfn);
memset(instack, false, sizeof instack);
memset(add_block, , sizeof add_block);
memset(cut, false, sizeof cut);
Index = top = bridge = ;
ans.clear();
for(int i = ; i < n; i ++)
if(!dfn[i])
tarjan(i, i);
printf("%d critical links\n", bridge);
for(int u = ; u < n; u ++) {
for(int i = head[u]; ~i; 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 = ; i < ans.size(); i ++)
printf("%d - %d\n", ans[i].first, ans[i].second);
printf("\n");
} inline bool ishash(int u, int v) {
return !(mp[u * maxn + v]++ || mp[v * maxn + u]++);
} int main() {
int u, num, v;
while(scanf("%d", &n) == ) {
mp.clear();
init();
for(int i = ; i < n; i ++) {
scanf("%d (%d)", &u, &num);
for(int i = ; i < num; i ++) {
scanf("%d", &v);
if(ishash(u, v)) continue;
addedge(u, v);
addedge(v, u);
}
}
solve();
}
return ;
}
uva-796.critical links(连通图的桥)的更多相关文章
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 - Critical Links (求桥)
Critical Links In a computer network a link L, which interconnects two servers, is considered criti ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- Uva 796 Critical Links 找桥
这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- UVA 796 Critical Links
输出桥. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- 跨平台信息获取小工具第三版本(增加了继承、多线程、异常处理模块、excel表格内容剔除空格)
# coding=utf-8 import threadingimport paramikoimport osimport timeimport xlrdimport xlwtimport openp ...
- 【NOIP2016提高组day1】换教室
题目 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的 课程. 在可以选择的课程中,有 2n 节课程安排在 n 个时间段上. 在第 i ( 1 ≤ i ≤ n )个 时间段上,两 ...
- CSS中的 vh/vw
vh 相对于当前窗口的大小,我用electron-vue来开发一个桌面应该,就用到这个,很方便,百分比需要外面有一个固定的高度,依赖父元素
- c# B/S下 如何优化文件上传速度和实现断点续传问题
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...
- Linux下cs简单通讯(socket)
Server: #include<iostream> #include<sys/types.h> #include<sys/socket.h> #include&l ...
- Netty 介绍和应用场景(一)
1.为什么选择Netty 需要了解了Socket通信(IO/NIO/AIO)编程,对于通信模型已经有了一个基本的认识.,果想把这些真正的用于实际工作中,那么还需要不断的完善.扩展和优化.比如经典的TC ...
- yield return的使用。。。
因为要取两个集合不同的元素,所以写了个拓展方法,用到了yield这个关键字,然后就学习了一波.先上代码 public static IEnumerable<T> NoRetainAll&l ...
- Bootstrap Table 的X-editable插件怎么用
在准备使用X-editable来做Bootstrap Table 的行内编辑的时候,根据http://www.cnblogs.com/landea... 的文章,我不能将全部效果重复实现,网上也搜索了 ...
- c++结构体、共用体和枚举
结构体类型 c++中的结构体成员既可以是数据,也可以是函数 c语言中定义结构体变量必须加struct(这也是很多时候和typedef),但是在c++里面,可以不加 结构体和类的不同在于,结构体中的变量 ...
- springboot 基于@Scheduled注解 实现定时任务
前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...