本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥.

本题思路:注意判重就行了,就是一个桥的裸题.

  判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者下半角.

参考代码:

 /*************************************************************************
> File Name: uva-796.critical_links.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月06日 星期五 15时58分54秒
本题思路:注意边的判重.
************************************************************************/ #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int maxn = + , maxm = maxn * maxn + ;
int n;
struct Edge {
int to, next;
bool cut;
} edge[maxm];
int head[maxn], tot;
int low[maxn], dfn[maxn],stack[maxn];
int Index, top, bridge;
bool instack[maxn];
bool cut[maxn];
int add_block[maxn]; void addedge(int u, int v) {
edge[tot].to = v; edge[tot].next = head[u]; edge[tot].cut = false;
head[u] = tot ++;
} void init() {
memset(head, -, sizeof head);
tot = ;
} map<int, int> mp;
vector <pair<int, int> > ans; void tarjan(int u, int pre) {
int v;
low[u] = dfn[u] = ++ Index;
instack[u] = true;
int son = ;
int pre_cnt = ;
for(int i = head[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(v == pre && pre_cnt == ) {
pre_cnt ++;
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 ^ ].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;
if(u == pre) add_block[u] = son - ;
instack[u] = false;
top --;
} void solve() {
memset(dfn, , sizeof dfn);
memset(instack, false, sizeof instack);
memset(add_block, , sizeof add_block);
memset(cut, false, sizeof cut);
Index = top = bridge = ;
ans.clear();
for(int i = ; i < n; i ++)
if(!dfn[i])
tarjan(i, i);
printf("%d critical links\n", bridge);
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");
} inline bool ishash(int u, int v) {
return !(mp[u * maxn + v]++ || mp[v * maxn + u]++);
} int main() {
int u, num, v;
while(scanf("%d", &n) == ) {
mp.clear();
init();
for(int i = ; i < n; i ++) {
scanf("%d (%d)", &u, &num);
for(int i = ; i < num; i ++) {
scanf("%d", &v);
if(ishash(u, v)) continue;
addedge(u, v);
addedge(v, u);
}
}
solve();
}
return ;
}

uva-796.critical links(连通图的桥)的更多相关文章

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

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

  2. UVA 796 - Critical Links (求桥)

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

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

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

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

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

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

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

  6. UVA 796 Critical Links(Tarjan求桥)

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

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

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

  8. Uva 796 Critical Links 找桥

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

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

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

  10. UVA 796 Critical Links

    输出桥. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

随机推荐

  1. 【shell】截取字符串前面文字

    例如:有一个文件test.txt,里面有这些数据. meiguounix232 faguounix ribenunix zhongguounixtaobao hanguounixbaba 现在我想截取 ...

  2. 2019JAVA最新课程-React从入门到实战(新)

    1.准备工作 可以在yunp.top网站看webpack,node/npm,cnpm的相关使用视频教程 react有两种使用方式,一是在现有网站中添加:二是创建一个全新的 官网创建全新一个react ...

  3. C# 自定义类中括号取值 测试

    public class ABC : Hashtable{} static class Program { public static ABC a= new ABC(); static void Ma ...

  4. curl POST如何查看响应的Header(转)

    curl -I 这样其实发送是HEAD请求. 下面这样发送POST请求(-X POST),同时指定Basic认证用户名密码(-u ‘andy:andy’),同时指定数据类型(-H ‘Content-T ...

  5. Mybatis 中在xxx.mapper书写模糊查询

    1.在mybatis中,书写sql,有时候会有一些不细心,如: <!-- 首页商品 关键字搜索--> <select id="getGoodsByLikeTitle&quo ...

  6. nginx下的负载均衡

    负载均衡应用场景: 普通web应用部署到多台应用服务器上,客户端通过访问应用服务器发送请求,最简单的就是n对1模式,n个客户端访问同一个应用服务器,这种情况当并发量大了,就无法应对,而且,如果只有一台 ...

  7. JDK中String类的源码分析(一)

    1.String类是final的,不允许被继承 /** The value is used for character storage. */ private final char value[]; ...

  8. JDK源码--HashMap(之resize)

    1.HashMap源码阅读目标了解具体的数据结构(hash及冲突链表.红黑树)和重要方法的具体实现(hashCode.equals.put.resize...) 2.重要方法 hashCode 与 e ...

  9. C# Socket-TCP异步编程原理详解附源码

    目录 目录异步原理主要方法源码Server源码:Client源码实验效果(广播为例)参考博客 TOC 异步原理 套接字编程原理:延续文件作用思想,打开-读写-关闭的模式. C/S编程模式如下: Ø 服 ...

  10. Spring boot 自定义一个starter pom

    用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...