UVA 796 Critical Links(无向图求桥)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 11005
#define min(a,b) (a<b?a:b)
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;
}
}bridge[maxn];
int n, dfn[maxn], low[maxn], Father[maxn], Time;
vector<int> G[maxn]; void init()
{
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(Father, , sizeof(Father));
Time = ;
for(int i=; i<n; i++)
G[i].clear();
} void Tarjan(int u,int fa)
{
Father[u] = fa;
low[u] = dfn[u] = ++Time;
int len = G[u].size(), v, k = ; for(int i=; i<len; i++)
{
v = G[u][i]; if(v == fa && !k)
{
k ++;
continue;
}
if( !low[v] )
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
int ans = ;
for(int i=; i<n; i++)
{
if(!dfn[i])
Tarjan(i,-);
} for(int i=; i<n; i++)
{
int v = Father[i];
if(dfn[v] < low[i] && v != -)
{ bridge[ans].x = i;
bridge[ans].y = v; if(bridge[ans].x > bridge[ans].y)
swap(bridge[ans].x, bridge[ans].y);
ans ++;
}
}
sort(bridge, bridge + ans); printf("%d critical links\n", ans); for(int i=; i<ans; i++)
{
printf("%d - %d\n",bridge[i].x,bridge[i].y);
}
printf("\n");
} int main()
{
while(scanf("%d",&n) != EOF)
{
init();
for(int i=; i<n; i++)
{
int a, b, m;
scanf("%d (%d)",&a,&m); while(m--)
{
scanf("%d", &b);
G[a].push_back(b);
// G[b].push_back(a);
}
}
solve();
}
return ;
} /**
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)
*/ #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
usingnamespace std;
#define INF 0xfffffff
#define maxn 11005
#define min(a,b) (a<b?a:b)
/** 无向图求桥 **/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;
}
}bridge[maxn];
int n, dfn[maxn], low[maxn], Father[maxn], Time;
vector<int> G[maxn]; void init()
{
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(Father, , sizeof(Father));
Time = ;
for(int i=; i<n; i++)
G[i].clear();
} void Tarjan(int u,int fa)
{
Father[u] = fa;
low[u] = dfn[u] = ++Time;
int len = G[u].size(), v; for(int i=; i<len; i++)
{
v = G[u][i]; if( !low[v] )
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
elseif(fa != v)
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
int ans = ;
for(int i=; i<n; i++)
{
if(!low[i])
Tarjan(i, -);
} for(int i=; i<n; i++)
{
int v = Father[i];
if(v != - && dfn[v] < low[i])
{ bridge[ans].x = i;
bridge[ans].y = v; if(bridge[ans].x > bridge[ans].y)
swap(bridge[ans].x, bridge[ans].y);
ans ++;
}
}
sort(bridge, bridge + ans); printf("%d critical links\n", ans); for(int i=; i<ans; i++)
{
printf("%d - %d\n",bridge[i].x,bridge[i].y);
}
printf("\n");
} int main()
{
while(scanf("%d",&n) != EOF)
{
init();
for(int i=; i<n; i++)
{
int a, b, m;
scanf("%d (%d)",&a,&m); while(m--)
{
scanf("%d", &b);
G[a].push_back(b);
G[b].push_back(a);
}
}
solve();
}
return0;
}
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 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- Uva 796 Critical Links 找桥
这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
随机推荐
- CTE在Oracle和Sqlserver中使用的差异
CTE是一个很好用的工具,他可以帮助我们清晰代码结构,减少临时表使用,同时oracle和sqlserver都提供支持.但在oracle和sqlserver中使用CTE也存在一定区别. Oracle使用 ...
- 9.27 noip模拟试题
工资 (money/money.in/money.out) 时限1000ms 内存256MB 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可 ...
- 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".
运行注册表: (运行->输入"regedit").在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\v1.0中,删除"Loc ...
- Spring 中各种通知
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Long型整数,缄默溢出
/** 长整数问题 @author husky */ public class LongDemo { public static void main(String[] args) { /** * 问题 ...
- SGU 157.Patience
简单的搜索,在n>10时,要打表 code: #include<stdio.h> #include<string.h> #include<algorithm> ...
- jQuery-弹窗登录
在jQuery中实现弹窗常要用到的方法有: width() :元素的宽度 outerWidth() 元素的宽度 盒子的padding+border 总的宽度 scrollTop() 鼠标滚轮自上 ...
- WF学习笔记(一)
-流程启动方式1: WorkflowInvoker.Invoke(new Workflow1()); -流程启动方式2: WorkflowApplication instance = new Work ...
- Javascript闭包函数快速上手
闭包函数是什么?在开始学习的闭包的时候,大家很能都比较难理解.就从他的官方解释来说,都是比较概念化的. 不过我们也还是从闭包的含义出发. 闭包是指函数有自由独立的变量.换句话说,定义在闭包中的函数可以 ...
- 汇总前端最最常用的JS代码片段
html5选择器 //参数均接收一个合法的css选择器 element = document.querySelector('.foo,.bar');//返回带有foo或者bar样式类的首个元素 ele ...