题目大意:给你一个网络要求这里面的桥。
输入数据:
n 个点
点的编号  (与这个点相连的点的个数m)  依次是m个点的
 
输入到文件结束。
桥输出的时候需要排序
 
知识汇总:
桥:   无向连通图中,如果删除某条边后,图变成不连通了,则该边为桥。
求桥:
在求割点的基础上吗,假如一个边没有重边(重边 1-2, 1->2 有两次,那么 1->2 就是有两条边了,那么 1->2就不算是桥了)。
当且仅当 (u,v) 为父子边,且满足 dfn[u] < low[v]
这里对重边处理的时候用了两种方法。写了两个代码,也挺简单的。
#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(无向图求桥)的更多相关文章

  1. UVA 796 - Critical Links (求桥)

    Critical Links  In a computer network a link L, which interconnects two servers, is considered criti ...

  2. UVA 796 - Critical Links 无向图字典序输出桥

    题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...

  3. UVA 796 Critical Links —— (求割边(桥))

    和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...

  4. Uva 796 Critical Links (割边+排序)

    题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...

  5. UVA 796 Critical Links(模板题)(无向图求桥)

    <题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...

  6. uva 796 Critical Links(无向图求桥)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVA 796 Critical Links(Tarjan求桥)

    题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...

  8. Uva 796 Critical Links 找桥

    这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...

  9. UVA 796 Critical Links (tarjan算法求割边)

    这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...

随机推荐

  1. codevs 1817 灾后重建

    /* 暴力暴力 离线每次添边 堆优化dij 70 SPFA 80..... */ #include<iostream> #include<cstdio> #include< ...

  2. C# 内存管理优化畅想(一)---- 大对象堆(LOH)的压缩

    我们都知道,.net的GC是不会压缩大对象堆的,因为其时间开销不可接受,但这是以大对象堆产生大块碎片为代价的,如果以后要分配的大对象比最大的碎片还大,那么即使它比所有碎片的总大小要小,也是无法在不扩展 ...

  3. (转)system()函数

    [C/C++]Linux下system()函数引发的错误   今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...

  4. 分享:带波形的语音播放工具(wavesurfer-js)

    项目名称:wavesurfer-js github地址:https://github.com/katspaugh/wavesurfer.js 官网地址:http://wavesurfer-js.org ...

  5. ToString格式.

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  6. Deep Learning 学习随记(八)CNN(Convolutional neural network)理解

    前面Andrew Ng的讲义基本看完了.Andrew讲的真是通俗易懂,只是不过瘾啊,讲的太少了.趁着看完那章convolution and pooling, 自己又去翻了翻CNN的相关东西. 当时看讲 ...

  7. CSS居中的方法总结

    [水平居中] 行内:text-align:center; 定宽块状:1.left:0 right:0然后用margin: auto外边距填充,水平方向不会发生外边距叠加;  2.绝对定位(父元素定位不 ...

  8. Mac OS X 系统下快速显示隐藏文件的方法(使用Automator创建workflow)

    有的时候需要显系统中的隐藏文件,在 Mac 中不像windows系统那么方便(勾选选项就能够操作),需要在 Terminal 中执行: localhost:~ mx$ defaults write c ...

  9. .net软件工程师面试题(参考答案)

    一.填空题(每空1分,共12分) 1面向对象的语言具有__封装______性.__继承_______性.__多态______性. 2能用foreach遍历访问的对象需要实现 ____Ienumerab ...

  10. php实现返回上一页的功能的3种有效方法

    php实现返回上一页的功能的3种有效方法 header(location:你的上一页的路径);   //   注意这个函数前不能有输出 header(location:.getenv("HT ...