题目链接:

  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. bzoj 2653 middle (可持久化线段树)

    middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1981  Solved: 1097[Submit][Status][Discuss] D ...

  2. codevs2597 团伙

    题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友 ...

  3. VIM使用技巧15

    在vim的插入模式下,有时需要插入寄存器中的文本: 1.使用<C-r>{register} 2.使用<C-r><C-p>{register} 3.使用<C-r ...

  4. java核心技术卷一

    java核心技术卷一 java基础类型 整型 数据类型 字节数 取值范围 int 4 +_2^4*8-1 short 2 +_2^2*8-1 long 8 +_2^8*8-1 byte 1 -128- ...

  5. restful接口就是url嘛,通过http请求发起访问。那接口进行监控,就可以监控这个restful url嘛

    EasyAPI接口管理系统 专注API接口监控,让您的API接口更稳定,与APP更紧密 + 购买监控服务 接口性能分析 分析App对应的API接口请求性能,包含HTTP响应时间.吞吐率.HTTP错误率 ...

  6. Android自己主动提示文本框(AutoCompleteTextView)

    自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...

  7. 怎样把引用的jar包和本项目一起导出成jar文件

    之所以要导出Runnable JAR.是由于我们希望将引用到的Jar包与本项目一起进行导出,所以不要选Jar file 选File/Export...然后Java/Runnable JAR file, ...

  8. MVC 用户权限HttpContext.User.IsInRole()

    这几天在用MVC做一个项目,用到了HttpContext.User.IsInRole() 这个方法,但是每次当我用的时候,HttpContext.User.IsInRole(“Admin”) 返回的永 ...

  9. c# Custom Controls

    http://www.cnblogs.com/light169/archive/2008/06/11/1217139.html

  10. busybox的使用

    1 将busybox设置为静态链接,放在文件系统中使用 make menuconfig的时候,Busybox Settings --> Build Options --> Build Bu ...