UVA 796 Critical Links
输出桥。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = + ;
const int Maxn = * + ;
int low[maxn];
int dfn[maxn];
int U[maxn], V[maxn]; struct Edge
{
int from, to, id, ans;
} edge[Maxn];
vector<int>G[maxn];
int N, M;
int tmpdfn;
int tot;
int Start, End; struct ANSS
{
int first, second;
}ANS[Maxn]; void init()
{
for (int i = ; i<maxn; i++) G[i].clear();
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
low[] = dfn[] = ;
tmpdfn = ;
tot = ;
} void AddEdge(int u, int v)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].id = tot;
edge[tot].ans = ;
G[u].push_back(tot);
tot++; edge[tot].from = v;
edge[tot].to = u;
edge[tot].id = tot;
edge[tot].ans = ;
G[v].push_back(tot);
tot++; } int Tarjan(int u, int id)
{
tmpdfn++;
int lowu = dfn[u] = tmpdfn;
for (int i = ; i<G[u].size(); i++)
{
int B = G[u][i];
if (!dfn[edge[B].to])
{
int lowv = Tarjan(edge[B].to, edge[B].id);
lowu = min(lowu, lowv);
if (lowv >= dfn[u])
{
if (lowv>dfn[u])
edge[B].ans = ;
}
}
else if (dfn[edge[B].to])
{
if (edge[B].id / == id / ) continue;
lowu = min(lowu, dfn[edge[B].to]);
}
} low[u] = lowu;
return lowu;
} bool cmp(const ANSS&a, const ANSS&b)
{
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
} void Display_Cutting_edge()
{
int AA = ;
for (int i = ; i < * M; i++)
{
if (edge[i].ans)
{
if (edge[i].from < edge[i].to)
{
ANS[AA].first = edge[i].from;
ANS[AA].second = edge[i].to;
}
else
{
ANS[AA].first = edge[i].to;
ANS[AA].second = edge[i].from;
}
AA++;
}
}
printf("%d critical links\n", AA);
sort(ANS, ANS + AA, cmp);
for (int i = ; i < AA; i++)
printf("%d - %d\n", ANS[i].first-, ANS[i].second-);
printf("\n");
} int main()
{
while (~scanf("%d", &N))
{
init();
M = ;
for (int i = ; i <= N; i++)
{
int from,TTT;
scanf("%d (%d)", &from, &TTT);
while (TTT--)
{
int too;
scanf("%d", &too);
if (from >= too) continue;
U[M] = from + ; V[M] = too + ;
AddEdge(U[M], V[M]);
M++;
}
}
Start = ; End = N;
Tarjan(, -);
for (int i = ; i <= N; i++) if (!dfn[i]) Tarjan(i, -);
Display_Cutting_edge();
}
return ;
}
UVA 796 Critical Links的更多相关文章
- 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(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(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...
随机推荐
- C++把引用作为返回值
当返回一个引用时,要注意被引用的对象不能超出作用域.所以返回一个对局部变量的引用是不合法的,但是,可以返回一个对静态变量的引用. int& func() { int q; //! return ...
- MyEclipse中用Maven创建Web项目
方法/步骤 new --> other 1.Wizards: mvaen 2.Maven Project 3.Next Use Default Workspace Locatio ...
- Error js内置错误 js处理错误流程 Throw语句
Exceptional Exception Handling in JavaScript MDN资料 Anything that can go wrong, will go wrong. ...
- openstack添加数据库
输入:neutron-db-manage revision -m "表的名称" neutron-db-manage upgrade head 如果遇到版本名找不到的情 ...
- cuda8.0 /usr/bin/ld: cannot find -lGL
/usr/bin/ld: cannot find -lGL collect2: ld returned 1 exit status tennycent@tennycent-desktop:~/$ ...
- 老司机的奇怪noip模拟T3-zhugeliang
3. 诸葛亮(zhugeliang.cpp/c/pas )[问题描述]xpp 每天研究天文学研究哲学,对于人生又有一些我们完全无法理解的思考.在某天无聊学术之后, xpp 打开了 http://web ...
- L3-002. 堆栈
L3-002. 堆栈 题目链接:https://www.patest.cn/contests/gplt/L3-002 线段树 线段树的数据修改和查询都是O(lgn)的,此题只需维护各个区间内的数的个数 ...
- Sass与Compress实战:第一章
1.消除冗余代码的方式: ▶通过变量来复用属性值 例如,一段冗余的CSS代码: h1#brand { color : #1875e7 } #sidebar { background-color : # ...
- Spring Security(06)——AuthenticationProvider
目录 1.1 用户信息从数据库获取 1.1.1 使用jdbc-user-service获取 1.1.2 直接使用JdbcDaoImpl 1.2 PasswordEncode ...
- 编写高质量iOS代码的52个有效方法2-1
一.变量的定义位置(用{}声明示例变量或者用@property属性声明实例变量) 1.用{}声明示例变量: 此方法生命的实例变量,编译器在编译时,会自动计算其偏移量(表示该变量距离存放对象的内存区域的 ...