UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的~本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小。
但是我之前说过,在判断割边的时候只需要直接记录就可以了,因为每条边只会访问一次,但其实这是取决于建图方式的,题目中给出了每个点都与之相连的点,所以我们建出的边一定会有重复的,所以需要用map去重一下,可以在建图的时候就判断(因为没有多重边),也可以在收集割边的时候判断。代码如下:
#include<map>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
map<int,int>ma;
#define maxn 10010
struct EDGE
{
int to,nxt;
} edge[*maxn];
int tot,n,dfn[maxn],low[maxn],head[maxn],all,resnum;
void add_edge(int u,int v)
{
edge[tot].to = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void init()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
all = ;
resnum = ;
ma.clear();
}
bool mul(int u,int v)
{
if(ma[u*maxn+v] || ma[v*maxn+u]) return true;
ma[u*maxn+v] = ma[v*maxn+u] = ;
return false;
}
struct Node
{
int x,y;
};
Node out[maxn];
void tarjan(int u,int fa)
{
dfn[u] = low[u] = ++all;
for(int i = head[u]; i != -; i = edge[i].nxt)
{
int v = edge[i].to;
if(!dfn[v])
{
tarjan(v,u);
low[u] = min(low[u],low[v]);
if(low[v] > dfn[u])
{
if(!mul(u,v))
{
int tmpu = u,tmpv = v;
if(tmpu > tmpv)
swap(tmpu,tmpv);
out[resnum].x = tmpu;
out[resnum].y = tmpv;
resnum++;
}
}
}
else if(v != fa) low[u] = min(low[u],dfn[v]);
}
return ;
}
bool cmp(Node a,Node b)
{
if(a.x != b.x) return a.x < b.x;
}
int getnum(char *a)
{
int lena = strlen(a);
int num = ,jw = ;
for(int i = ; i < lena; i++)
{
if(a[i] == ')')
{
for(int j = i-; j > ; j--)
{
num += (a[j]-'')*jw;
jw *= ;
}
// cout<<"num = "<<num<<endl;
return num;
}
}
}
int main()
{
int ans,m,x,y;
while(~scanf("%d",&n))
{
tot = ;
memset(head,-,sizeof(head));
for(int i = ; i < n; i++)
{
scanf("%d",&x);
char op[];
scanf("%s",op);
int m = getnum(op);
for(int j = ; j < m; j++)
{
scanf("%d",&y);
add_edge(x,y);
add_edge(y,x);
}
}
init();
for(int i = ; i < n; i++)
{
if(!dfn[i]) tarjan(i,-);
}
sort(out,out+resnum,cmp);
printf("%d critical links\n",resnum);
for(int i = ; i < resnum; i++)
{
printf("%d - %d\n",out[i].x,out[i].y);
}
puts("");
}
return ;
}
UVA 796 Critical Links (tarjan算法求割边)的更多相关文章
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- ZOJ Problem - 2588 Burning Bridges tarjan算法求割边
题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- 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(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
随机推荐
- SecureCRT - 使用方法和技巧
1. 保活防掉线选项 -> 会话选项 -> 终端勾选 自动重新连接, 发送协议 NO-OP 每60秒 2. 拷贝与粘贴的设置选项 -> Global options -> Te ...
- 抛弃阿里云,中国用户购买海外VPS的五个理由
王掌柜在过去的五年多时间里,折腾过不少vps品牌,最开始玩的是一年一百多块钱的香港虚拟主机,后来业务量大了,开始折腾国内的小鸟云.阿里云.腾讯云.电信云.百度云主机,国外的linode\interse ...
- The server instance Witness rejected configure request; read its error log file for more information. The reason 1427, and state 31, can be of use for
数据库服务器做了镜像之后,发现有错误信息 The server instance Witness rejected configure request; read its error log file ...
- hudson
来源: hudson入门与实战 http://www.360doc.com/content/15/0304/22/12144668_452603921.shtml Hudson安装配置.部署应用及分析 ...
- touchesBegan: withEvent: <--- with UIScrollView / UIImageView
touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...
- Centos 6.5升级到Git2.1.2的步骤
Centos 6.5升级到Git2.1.2的步骤 Centos 6.5升级到Git2.1.2其实是非常的简单,因这款版本控制程序非常的好用,所以小编自己也是使用它了,下面一起来看看Centos 6.5 ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Chapter 1 First Sight——30
The girl sitting there giggled. I'd noticed that his eyes were black — coal black. 那个坐在那里的女孩笑着.我注意到她 ...
- LeetCode OJ 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...