UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了
这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e6+7;
struct edge
{
int to;
int pre;
};
edge E[N<<1];
int head[N],tot;
int low[N],dfn[N],ins[N],ts,top,st[N];
vector<pii>ans; void init()
{
CLR(head,-1);
tot=0;
CLR(low,0);
CLR(dfn,0);
CLR(ins,0);
ts=top=0;
ans.clear();
}
inline void add(int s,int t)
{
E[tot].to=t;
E[tot].pre=head[s];
head[s]=tot++;
}
void Tarjan(int u,int pre)
{
low[u]=dfn[u]=++ts;
ins[u]=1;
st[top++]=u;
int v;
for (int i=head[u]; ~i; i=E[i].pre)
{
v=E[i].to;
if(v==pre)
continue;
if(!dfn[v])
{
Tarjan(v,u);
low[u]=min<int>(low[u],low[v]);
if(low[v]>dfn[u])
{
pii temp=pii(u,v);
if(temp.first>temp.second)
swap(temp.first,temp.second);
ans.push_back(temp);
}
}
else if(ins[v])
low[u]=min<int>(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
do
{
v=st[--top];
ins[v]=0;
}while (u!=v);
}
}
int main(void)
{
int n,u,v,k,i;
while (~scanf("%d",&n))
{
init();
for (i=0; i<n; ++i)
{
scanf("%d (%d)",&u,&k);
while (k--)
{
scanf("%d",&v);
add(u,v);
add(v,u);
}
}
for (i=0; i<n; ++i)
{
if(!dfn[i])
Tarjan(i,-1);
}
sort(ans.begin(),ans.end());
int SZ=(int)ans.size();
printf("%d critical links\n",SZ);
for (i=0; i<SZ; ++i)
printf("%d - %d\n",ans[i].first,ans[i].second);
putchar('\n');
}
return 0;
}
UVA 796 Critical Links(Tarjan求桥)的更多相关文章
- UVA 796 - Critical Links (求桥)
Critical Links In a computer network a link L, which interconnects two servers, is considered criti ...
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- Uva 796 Critical Links 找桥
这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...
随机推荐
- codeforces 459C Pashmak and Buses 解题报告
题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...
- jsp之间传参中文乱码问题
jsp页面之间传参,传中文会出现乱码问题. 如下: $('.yzjjfa_row').eq(0).append('<a class="yzjjfa_contItem jjfa_acti ...
- google maps js v3 api教程(2) -- 在地图上添加标记
原文链接 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 我们在创建地图 ...
- 信与信封问题(codevs 1222)
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...
- LINUX_source
Be careful! ./ and source are not quite the same. ./script runs the script as an executable file, la ...
- hadoop中常见的问题
一.在root下进行格式化 这样很糟糕 这样的话,若是第一次装的话,我的建议是将生成的文件都删掉,恢复到最开始的状态, 1. 首先你需要删除 vi conf/hdfs-site.xml 配置文件的 ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- 【转】解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
这里写的很清楚了,已验证可用 http://blog.csdn.net/linghao00/article/details/7926458
- OpenStack Swift集群部署流程与简单使用
之前介绍了<OpenStack Swift All In One安装部署流程与简单使用>,那么接下来就说一说Swift集群部署吧. 1. 简介 本文档详细描述了使用两台PC部署一个小型Sw ...
- Android中动画
两种动画 view动画 属性动画 (也可以使用xml描述动画) view 4动画 补间动画 渐变 AlphaAnimation 缩放 ScaleAnimation 平移 TranslateAnima ...