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输出时左边必须比右边小. ...
随机推荐
- jsp带参转链接
1.jsp:forward page <jsp:forward page="show.jsp"> <jsp:param name="data" ...
- ASP.NET 打包下载文件
使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...
- DOM - nodeType 的取值
DOM 中,共有 12 中不同类型的节点,nodeType 的取值以数值表示. 节点类型 描述 子节点 1 Element 表示元素. Element, Text, Comment, Proce ...
- Java 最简单的批处理
批处理Batch && PreparedStatement : import java.sql.*; public class TestBatch { public static vo ...
- retain two decimal digits.
package kju.o; import static kju.print.Printer.*; import java.text.*; class MathDemo { public static ...
- 设置linux服务器定时与时间服务器同步
在一些大公司经常出现这样一个情况:公司或一些机关单位的内部业务系统的应用服务器以及数据都是做的多机集群部署而且基本都是linux系统,而且都是内部网,不与外网通讯的.这样经常就会出现一个情况,我发送任 ...
- DGV属性
1.控件的SelectedCells.Count属性可以判断用户是否已经选择数据,如果大于0说明有选择的数据. 2.SelectedCells[N].Value的属性可以获取某一行数据中某列的数据,其 ...
- 异步IO简介
最近想学习一下libevent,就先翻译一下libevent的官方文档吧. 英文原文链接:http://www.wangafu.net/~nickm/libevent-book/01_intro.ht ...
- 关于unitils联合dbunit的测试
unitils据说测试的能力很强大,可测试dao,service,web层,其实对数据库的测试我更关心,看到有人展示了测试的方法,数据直接写在xls表中,很直观,然后就依照他们的方法进行试验,花费的时 ...
- linux 日常命令(磁盘空间)
df –hl 在windows下可以很方便的查看磁盘空间的.但是到了Linux查看磁盘空间,你可能就有点摸不着头脑了,呵呵.不要急,我这就要给你解决这个问题. Df命令是Linux查看磁盘空间系统以磁 ...