UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接>
题目大意:
无向连通图求桥,并将桥按顺序输出。
解题分析;
无向图求桥的模板题,下面用了kuangbin的模板。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 1e4+;
const int M = 1e5+;
struct Edge{
int to,next;
bool cut;//是否为桥的标记
}edge[M];
int head[N],tot,n;
int low[N],dfn[N],Stack[N];
int Index,top;
bool Instack[N],cut[N];
int add_block[N];//删除一个点后增加的连通块
int bridge;
void init(){
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(Instack,false,sizeof(Instack));
memset(add_block,,sizeof(add_block));
memset(cut,false,sizeof(cut));
Index=top=bridge=tot = ;
}
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){
low[u] = dfn[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = ;
for(int i = head[u];i != -;i = edge[i].next){
int 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]){ //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<low(v)
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; //若u为根,且分支数>1,则u割点
if(u == pre)add_block[u] = son - ;
Instack[u] = false;
top--;
}
void solve(){
for(int i = ;i <= n;i++)
if( !dfn[i] )
Tarjan(i,i);
printf("%d critical links\n",bridge); vector<pair<int,int> >ans; /*-- 将桥按顺序输出 --*/
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");
}
//处理重边
/*map<int,int>mapit;
inline bool isHash(int u,int v)
{
if(mapit[u*N+v])return true;
if(mapit[v*N+u])return true;
mapit[u*N+v] = mapit[v*N+u] = 1;
return false;
}*/
int main(){
while(scanf("%d",&n) == ){
init();
int u,k,v;
//mapit.clear();
for(int i = ;i <= n;i++){
scanf("%d (%d)",&u,&k);
u++;
//这样加边,要保证正边和反边是相邻的,建无向图
while(k--){
scanf("%d",&v);
v++;
if(v <= u)continue;
//if(isHash(u,v))continue;
addedge(u,v);
addedge(v,u);
}
}
solve();
}
return ;
}
2018-10-18
UVA 796 Critical Links(模板题)(无向图求桥)的更多相关文章
- (连通图 模板题 无向图求桥)Critical Links -- UVA -- 796
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
随机推荐
- Jenkins二 安装gitlab及其使用
git --version 如果没有安装git直接源码安装即可,如果安装了先删除原来的git. yum -y remove git先安装编译git需要的包. yum install zlib-deve ...
- 剑指offer 二叉搜索树和双向链表
剑指offer 牛客网 二叉搜索树和双向链表 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 18:58:36 2019 ...
- CF 494B 【Obsessive String】
很有趣的一道题 这道题提议很难懂,其实就是让你求合法的集合数目.合法的集合定义为: 1.集合中的所有串都是s的子串,且互不重叠 2.集合中的所有串都含有子串t. 看到网上很多题解说要用kmp,但我就不 ...
- 2017-2018-2 20165314 实验三《 敏捷开发与XP实践》实验报告
知识点: 1.XP团队使用现场客户.特殊计划方法和持续测试来提供快速的反馈和全面的交流: -XP是以开发符合客户需要的软件为目标而产生的一种方法论 -XP是一种以实践为基础的软件工程过程和思想 -XP ...
- 饮冰三年-人工智能-Python-10之C#与Python的对比
1:注释 C# 中 单行注释:// 多行注释:/**/ python 中 单行注释:# 多行注释:“““内容””” 2:字符串 C#中 "" 用双引号如("我是字符串&q ...
- Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615"
问题:Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615" 原因 ...
- GO语言之urfave/cli命令行解析
练习URL: https://blog.csdn.net/sd653159/article/details/83381786 相信只要部署过线上服务,都知道启动参数一定是必不可少的,当你在不同的网络. ...
- Angularjs 学习笔记-2017-02-06-双向数据绑定
NG: ng-bind: 标签属性 ng-bind=" obj.xxx " ,不会出现 用于区别{{ }} 标签,当页面未加载完毕时可以看到{{}}标签,非常不雅观,ng-bi ...
- javaScript事件(七)事件类型之键盘与文本事件
键盘事件如下: keydown:当用户按下键盘上的任意键时触发,而且如果按住不放的话,会重复触发此事件. keypress:当用户按下键盘上的字符键时触发,而且如果按住不放的话,会重复触发此事件. k ...
- CND网站加速
CDN是什么 1-CDN俗称网站加速2-公司一般是购买其他cdn服务商提供的服务3-CDN一般是用来缓存网站的静态资源文件的(css,js,图片,html,htm),浏览器获取某个静态资源是按照就近原 ...