<题目链接>

题目大意:

无向连通图求桥,并将桥按顺序输出。

解题分析;

无向图求桥的模板题,下面用了kuangbin的模板。

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 1e4+;
const int M = 1e5+;
struct Edge{
int to,next;
bool cut;//是否为桥的标记
}edge[M];
int head[N],tot,n;
int low[N],dfn[N],Stack[N];
int Index,top;
bool Instack[N],cut[N];
int add_block[N];//删除一个点后增加的连通块
int bridge;
void init(){
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(Instack,false,sizeof(Instack));
memset(add_block,,sizeof(add_block));
memset(cut,false,sizeof(cut));
Index=top=bridge=tot = ;
}
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){
low[u] = dfn[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = ;
for(int i = head[u];i != -;i = edge[i].next){
int 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]){ //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<low(v)
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; //若u为根,且分支数>1,则u割点
if(u == pre)add_block[u] = son - ;
Instack[u] = false;
top--;
}
void solve(){
for(int i = ;i <= n;i++)
if( !dfn[i] )
Tarjan(i,i);
printf("%d critical links\n",bridge); vector<pair<int,int> >ans; /*-- 将桥按顺序输出 --*/
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");
}
//处理重边
/*map<int,int>mapit;
inline bool isHash(int u,int v)
{
if(mapit[u*N+v])return true;
if(mapit[v*N+u])return true;
mapit[u*N+v] = mapit[v*N+u] = 1;
return false;
}*/
int main(){
while(scanf("%d",&n) == ){
init();
int u,k,v;
//mapit.clear();
for(int i = ;i <= n;i++){
scanf("%d (%d)",&u,&k);
u++;
//这样加边,要保证正边和反边是相邻的,建无向图
while(k--){
scanf("%d",&v);
v++;
if(v <= u)continue;
//if(isHash(u,v))continue;
addedge(u,v);
addedge(v,u);
}
}
solve();
}
return ;
}

2018-10-18

UVA 796 Critical Links(模板题)(无向图求桥)的更多相关文章

  1. (连通图 模板题 无向图求桥)Critical Links -- UVA -- 796

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

  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(无向图求桥)

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

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

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

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

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

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

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

  8. UVA 796 Critical Links(Tarjan求桥)

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

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

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

随机推荐

  1. Confluence 6 启用 HTTP 压缩

    在屏幕的右上角单击 控制台按钮 ,然后选择 基本配置(General Configuration) 链接. 在左侧的面板中选择 通用配置(General Configuration). 启用 HTTP ...

  2. Confluence 6 创建-使用-删除快捷链接

    创建快捷链接 如何创建一个快捷键链接: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧面板中选择 快捷链接(Shortcut Links). 为 ...

  3. Confluence 6 重新获得附件指南

    每一个文件在恢复上传到 Confluence 的时候必须单独重命名,你可以通过下面说明的 3 个方法中选择一个进行操作: 选择 A - 通过文件名恢复附件 如果你知道你需要恢复的每一个文件名,尤其是你 ...

  4. iframe与主框架跨域相互访问方法

    iframe 与主框架相互访问方法  http://blog.csdn.net/fdipzone/article/details/17619673/ 1.同域相互访问 假设A.html 与 b.htm ...

  5. 【Myeclipse】用Myeclipse10.5搭建C/C++开发环境

    一.添加CDT到Myeclipse10.5 我的Myeclipse版本是10.5,刚开始用Myeclipse configuration center添加安装,不管是用远程URL还是用本地Archiv ...

  6. STL 容器区别:vector、list、deque、set、map的底层实现

    https://blog.csdn.net/shawjan/article/details/45424405

  7. nginx安装目录详解(针对centos)

  8. LeetCode(123):买卖股票的最佳时机 III

    Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必 ...

  9. AI-序列化-做五个数据接口

    #url.py url(r'^customer/$', views.CustomerView.as_view()), #查询所有数据.添加数据接口url url(r'^customer/(\d+)', ...

  10. 阿里云人脸识别测试接口出错 返回Body:{ "errno": 3002, "err_msg": "ILLEGAL_PARAMETERS", "request_id": "672cba83-cf93-4ef4-9ce5-d87e51601632" }

    错误信息如下 返回Body:{ "errno": 3002, "err_msg": "ILLEGAL_PARAMETERS", ...... ...