Uva 796 Critical Links (割边+排序)
题目链接:
题目描述:
题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题目读了好几遍,但是依旧没有找到数据范围写在哪里,经过无数次runtime error最终把范围定在1W左右。(题目晦涩难懂,wa到死了,嗷~~~~~~)
解题思路:
就是用Tarjan算法dfs出low和dfn数组,然后记录下来起点和终点排好序的桥,然后把桥排序输出就ok了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
struct node
{
int to, next;
} edge[maxn*];
bool cmp (node a, node b)
{
if (a.next == b.next)
return a.to < b.to;
return a.next < b.next;
}
int low[maxn], dfn[maxn], head[maxn];
int Father[maxn], tot, ntime, cnt;
node Q[maxn * ];
void init ()
{
ntime = tot = cnt = ;
memset (low, , sizeof(low));
memset (dfn, , sizeof(dfn));
memset (head, -, sizeof(head));
memset (Father, , sizeof(Father));
}
void Add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
void Tarjan (int u, int father)
{
low[u] = dfn[u] = ++ntime;
Father[u] = father;
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (!dfn[v])
{
Tarjan (v, u);
low[u] = min (low[u], low[v]);
}
else if (father != v)
low[u] = min (low[u], dfn[v]);
}
}
int main ()
{
int n, m;
node q;
while (scanf ("%d", &n) != EOF)
{
init ();
m = n;
while (n --)
{
int u , k;
scanf ("%d (%d)", &u, &k);
while (k --)
{
int v;
scanf ("%d", &v);
Add (u, v);
Add (v, u);
}
}
for (int i=; i<m; i++)
if (!dfn[i])
Tarjan (i, -); for (int i=; i<m; i++)
{
int v = Father[i];
if (v != - && dfn[v] < low[i])
{
q.next = v;
q.to = i;
if (q.next > q.to)
swap(q.next, q.to);
Q[cnt++] = q;
}
}
printf ("%d critical links\n", cnt);
sort (Q, Q+cnt, cmp);
for (int i=; i<cnt; i++)
printf ("%d - %d\n", Q[i].next, Q[i].to);
printf ("\n");
}
return ;
}
Uva 796 Critical Links (割边+排序)的更多相关文章
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
- uva 796 Critical Links(无向图求桥)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 - Critical Links 无向图字典序输出桥
题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...
- UVA 796 Critical Links(Tarjan求桥)
题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...
- UVA 796 - Critical Links (求桥)
Critical Links In a computer network a link L, which interconnects two servers, is considered criti ...
- Uva 796 Critical Links 找桥
这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...
- UVA 796 Critical Links
输出桥. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- __asm
来源:http://msdn.microsoft.com/zh-cn/library/45yd4tzz.aspx Microsoft 专用 __asm 关键字调用一个内联汇编,并且可以显示,每当 c. ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- 苹果装WIN 7
一.准备:1.8G或以上的正版U盘或者移动硬盘,提前备份U盘数据(2013款air要求安装64位系统,市面上比较多U盘不是正版盘,一般做不成功,要换盘) 2.64位纯净版Windows7 ISO的下载 ...
- 【.Net 学习系列】-- Windows服务定时运行,判断当前时间是否在配置时间段内
/// <summary> /// 判断程序是否在设置运行时间内 /// </summary> /// <param name="startTime" ...
- android_handler(二)
这篇记录 android 消息机制中.WorkThread 模拟向网络訪问数据,获得数据后,返回 message 发送给 MainThread ,并改动 TextView 的 text 的这种一个步骤 ...
- NYOJ 题目42 一笔画问题(欧拉图)
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 zyc从小就比較喜欢玩一些小游戏.当中就包含画一笔画.他想请你帮他写一个程序.推断一个图是否可以用一笔画下 ...
- A* Pathfinding Project (Unity A*寻路插件) 使用教程
Unity4.6 兴许版本号都已经内置了寻路AI了.之前的文章有介绍 Unity3d 寻路功能 介绍及项目演示 然而两年来项目中一直使用的是 A* Pathfinding 这个插件的.所以抽时间来写下 ...
- Linux/UNIX之进程环境
进程环境 进程终止 有8种方式使进程终止,当中5中为正常终止,它们是 1) 从main返回 2) 调用exit 3) 调用_exit或_Exit 4) 最后一个 ...
- Nginx + FastCgi + Spawn-fcgi + C 架构的server环境搭建
1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemongers.com/NginxChs 下载 Nginx 0.6.26(开发版)(请下载最新版本号) tar z ...
- 嵌入式开发之davinci--- 8127 和8148的区别
1.主要的差别是8148有sata接口,8127没有 2.经过最近各方查找,应该是8107中把DSP砍掉了,8127如1楼所示 http://www.deyisupport.com/question_ ...