题目链接

题意: 给出一个无向图,按顺序输出桥

思路:求出全部的桥,然后按顺序输出就可以

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm> using namespace std; const int MAXN = 10005; struct Edge{
int to, next;
bool cut;
}edge[MAXN * 10];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN];
int Index, top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];
int bridge; 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) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
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]) {
bridge++;
edge[i].cut = true;
edge[i^1].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 > 1) cut[u] = true;
if (u == pre) add_block[u] = son - 1;
Instack[u] = false;
top--;
} void init() {
memset(head, -1, sizeof(head));
memset(DFN, 0, sizeof(DFN));
memset(Instack, false, sizeof(Instack));
memset(add_block, 0, sizeof(add_block));
memset(cut, false, sizeof(cut));
tot = 0;
Index = top = 0;
bridge = 0;
} void solve(int N) {
for (int i = 1; i <= N; i++)
if (!DFN[i])
Tarjan(i, i); printf("%d critical links\n",bridge);
vector<pair<int, int> > ans;
for (int u = 1; u <= N; u++)
for (int i = head[u]; i != -1; 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 = 0;i < ans.size();i++)
printf("%d - %d\n",ans[i].first-1,ans[i].second-1);
printf("\n");
} int main() {
int n;
while (scanf("%d", &n) == 1) {
init();
int u, k, v;
for (int i = 1; i <= n; i++) {
scanf("%d (%d)", &u, &k);
u++;
while (k--) {
scanf("%d", &v);
v++;
addedge(u, v);
addedge(v, u);
}
}
solve(n);
}
return 0;
}

UVA796- Critical Links(无向图中的桥梁)的更多相关文章

  1. UVA 796 Critical Links(无向图求桥)

    题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号  (与这个点相连的点的个数m)  依次是m个点的   输入到文件结束. 桥输出的时候需要排序   知识汇总: 桥:   无向连通 ...

  2. uva-796.critical links(连通图的桥)

    本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...

  3. UVA796 - Critical Links(Tarjan求桥)

    In a computer network a link L, which interconnects two servers, is considered critical if there are ...

  4. UVA796 Critical Links —— 割边(桥)

    题目链接:https://vjudge.net/problem/UVA-796 In a computer network a link L, which interconnects two serv ...

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

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

  6. [UVA796]Critical Links(割边, 桥)

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

  7. Uva796 Critical Links

    用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...

  8. UVA796 Critical Links(求桥) 题解

    题意:求桥 思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u] 所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边).我们在进行dfs的时候,要加入一个fa表示当前进行搜 ...

  9. UVA796:Critical Links(输出桥)

    Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, ...

随机推荐

  1. sort uniq妙用

    cat a b | sort | uniq > c   # c是a和b的并集 cat a b | sort | uniq -d > c   # c是a和b的交集 cat a b b | s ...

  2. android 按字母搜索

    在看Oplayer的时候看见滑动字母来实现listView的内容搜索,所以就把里面的核心的函数扣除来做了一个demo,分为两部分一个是布局,另一个就是代码了,具体的如下: 布局: <?xml v ...

  3. 【解决方法】System.IO.FileNotFoundException

    错误日志 See the end of this message for details on invoking just-in-time (JIT) debugging instead of thi ...

  4. Swift - iOS应用的国际化与本地化

    在Xcode中我们可以很方便的将APP适配各种本地化语言.苹果的框架已经帮我们把不同语言的数据分离开,包括图片,声音,视频,文档,用户界面文字(甚至代码中编写的用户界面文字),它们会被建立在同一个bu ...

  5. IE Jquery中拒绝訪问的处理方法

    多人合作开发一个站点过程中,为便于开发,将一些公共文件如js,css,images放在外网上,各自链接这类文件以供使用.本地測试时网页的一些JS代码在IE8,IE6中会停止运行,并报某个js文件拒绝訪 ...

  6. Qt4_VS10 程序打包发布

    源地址:http://www.2cto.com/kf/201306/217205.html 目录结构如下: ---------------------------------------------- ...

  7. PHP之验证码代码

    <?php session_start(); $checkcode=""; /*for($i=0;$i<4;$i++) { $checkcode.=dechex(ran ...

  8. TCP/IP笔记 一.综述

    1. TCP/IP分层 TCP/IP 是四层的体系结构:应用层.运输层.网际层和网络接口层,如下图: OSI协议是国际标准的网络协议,但是由于OSI的实用性等问题造成OSI没有流行起来.目前国际上广泛 ...

  9. Freemarker概念简单介绍

    Freemarker概念简单介绍 1.   Freemarker是什么 模板引擎:一种基于模板的,用来生成输出文本的通过工具. 基于java开发包和类库 2.   Freemarker能做什么 MVC ...

  10. Swift - 通过url地址打开web页面

    通过UIApplication.sharedApplication().openURL()方法,可以使用浏览器打开相应的网页. 1 2 3 var urlString = "http://h ...