题目链接:

  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 (割边+排序)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. UVA 796 Critical Links(Tarjan求桥)

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

  7. UVA 796 - Critical Links (求桥)

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

  8. Uva 796 Critical Links 找桥

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

  9. UVA 796 Critical Links

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

随机推荐

  1. iOS 自己主动释放手动释放混编

    当项目为手动释放时,Build Settings中,Objective-c Automatic Reference Conting 为YES 时,想要手动管理一些文件,在CompileSources中 ...

  2. [Maid] Write Tasks in Markdown with Maid

    Maid enables you to write your tasks in Markdown. Create a maidfile.md or a README.mdthen add Header ...

  3. 【POJ 3122】 Pie (二分+贪心)

    id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...

  4. cocos2dx塔防游戏逻辑

    cocos2dx 塔防游戏逻辑 1.欢迎界面 2.tield制作游戏地图,空块设置cantouch属性为1 3.设置地图锚点,把锚点增加一个锚点容器,给怪物的行走函数传入 该锚点容器參数,让怪物依照锚 ...

  5. [Canvas画图] 藏图阁(16) 人体穴位

    本节目标: 趁着今天是愚人节.阿伟决定来重温一下学医的那段日子. 有那么一段时间, 阿伟对武侠小说和医学同一时候产生了浓厚的兴趣,当时最想学的就是葵花点穴手, 一阳指之类的点穴功夫.轻轻一点.就能把别 ...

  6. mciSendString详解(转)

    做个mp3播放器,用realplay和WMP做出来的程序内存占用太大.如果你仅仅是播放MP3,建议使用API函数mciSendString,我把该函数的详细资料罗列如下供你参考.Option Expl ...

  7. iOS开发——常见BUG——window决定程序的状态栏管理问题

    Xcode7升级之后遇到的问题   问题一: 老项目在Xcode6上运行没有任何问题,但在Xcode7上运行直接崩了! 经过一波分析: 发现是因为我顶部状态栏处添加了topWindow,用于处理Tab ...

  8. 【codeforces379F】 New Year Tree

    距离一个点最远的点一定是直径的一个端点.考虑运用这个原理,每次维护一下直径端点即可. #include<algorithm> #include<iostream> #inclu ...

  9. 使用 dva + antd 快速开发react应用

    使用 dva + antd 快速开发react应用 版本说明: 注意:dva的版本是0.9.2 $ node -v v10.2.1 $ npm -v $ dva -v dva-cli version ...

  10. CANopen——总线基本知识

    1. 总线标准 2. 获取索引和子索引 2fh,2bh,23h,40h等,是不是对应cs的不同值: 主站1280h的对象字典?1280h-sub2,得到client的COB-ID值: 根据收到的m-& ...