题目链接 :http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122091#problem/C

题目:

In a computer network a link L, which interconnects two servers, is considered critical if there are at
least two servers A and B such that all network interconnection paths between A and B pass through L.
Removing a critical link generates two disjoint sub–networks such that any two servers of a sub–network
are interconnected. For example, the network shown in figure 1 has three critical links that are marked
bold: 0 -1, 3 - 4 and 6 - 7.
Figure 1: Critical links
It is known that:
1. the connection links are bi–directional;
2. a server is not directly connected to itself;
3. two servers are interconnected if they are directly connected or if they are interconnected with
the same server;
4. the network can have stand–alone sub–networks.
Write a program that finds all critical links of a given computer network.
Input
The program reads sets of data from a text file. Each data set specifies the structure of a network and
has the format:
no of servers
server0 (no of direct connections) connected server . . . connected server
. . .
serverno of servers (no of direct connections) connected server . . . connected server
The first line contains a positive integer no of servers(possibly 0) which is the number of network
servers. The next no of servers lines, one for each server in the network, are randomly ordered and
show the way servers are connected. The line corresponding to serverk, 0 ≤ k ≤ no of servers − 1,
specifies the number of direct connections of serverk and the servers which are directly connected to
serverk. Servers are represented by integers from 0 to no of servers − 1. Input data are correct. The
first data set from sample input below corresponds to the network in figure 1, while the second data
set specifies an empty network.
Output
The result of the program is on standard output. For each data set the program prints the number of
critical links and the critical links, one link per line, starting from the beginning of the line, as shown
in the sample output below. The links are listed in ascending order according to their first element.
The output for the data set is followed by an empty line.
Sample Input
8
0 (1) 1
1 (3) 2 0 3
2 (2) 1 3
3 (3) 1 2 4
4 (1) 3
7 (1) 6
6 (1) 7
5 (0)
0
Sample Output
3 critical links
0 - 1
3 - 4
6 - 7
0 critical links

题意: 在电脑网络中,有的网络是两个服务器彼此互联的。现给你网络服务器互联的图,问你主要的线路有几条。(主要线路指若断了这条线路,那么整个局域网则不能连接在一块,也就是桥)

vector 代码

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 11005vector< vector <int> >G;
int dfn[maxn], low[maxn], father[maxn];
int n, times, cnt;struct node
{int x, y;
}brige[maxn];bool cmp(node a, node b)
{if(a.x != b.x)return a.x<b.x;return a.y<b.y;
}
void  Init()
{G.clear();G.resize(n+5);memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(father, 0, sizeof(father));times = 1;
}void Tarjan(int u, int fa)
{dfn[u] = low[u] = times++;father[u] = fa;int len = G[u].size(), v;for(int i=0; i<len; i++){v = G[u][i];if(!dfn[v]){Tarjan(v, u);low[u] = min(low[u], low[v]);}else if(fa != v)low[u] = min(low[u], dfn[v]);}
}void solve()
{int v;cnt = 0;for(int i=0; i<n; i++){if(!low[i])Tarjan(i, -1);}for(int i=0; i<n; i++){v = father[i];if(v!=-1 && dfn[v] < low[i]){brige[cnt].x = i;brige[cnt].y = v;if(brige[cnt].x > brige[cnt].y)swap(brige[cnt].x, brige[cnt].y);cnt++;}}sort(brige, brige+cnt, cmp);printf("%d critical links\n", cnt);for(int i=0; i<cnt; i++){printf("%d - %d\n", brige[i].x, brige[i].y);}printf("\n");
}
int main()
{while(scanf("%d",&n) != EOF){Init();for(int i=0; i<n; i++){int a, b, m;scanf("%d (%d)",&a,&m);while(m--){scanf("%d", &b);G[a].push_back(b);G[b].push_back(a);}}solve();}return 0;
}

View Code

邻接表代码

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 1005
int head[maxn], low[maxn], dfn[maxn], father[maxn];
int cnt, times, n;struct node
{int v, next;
}maps[maxn*maxn];struct Brige
{int x,y;
}brige[maxn];bool cmp(Brige a, Brige b)
{if(a.x != b.x)return a.x<b.x;return a.y<b.y;
}void Add(int u, int v)
{maps[cnt].v = v;maps[cnt].next = head[u];head[u] = cnt ++;
}void  Init()
{memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(father, 0, sizeof(father));memset(maps, 0, sizeof(maps));memset(head, -1, sizeof(head));times = 1;
}void Tarjan(int u, int fa)
{dfn[u] = low[u] = times++;father[u] = fa;int  v;for(int i=head[u]; i!=-1; i=maps[i].next){v = maps[i].v;if(!dfn[v]){Tarjan(v, u);low[u] = min(low[u], low[v]);}else if(fa != v)low[u] = min(low[u], dfn[v]);}
}void solve()
{int v;cnt = 0;for(int i=0; i<n; i++){if(!low[i])Tarjan(i, -1);}for(int i=0; i<n; i++){v = father[i];if(v!=-1 && dfn[v] < low[i]){brige[cnt].x = i;brige[cnt].y = v;if(brige[cnt].x > brige[cnt].y)swap(brige[cnt].x, brige[cnt].y);cnt++;}}sort(brige, brige+cnt, cmp);printf("%d critical links\n", cnt);for(int i=0; i<cnt; i++){printf("%d - %d\n", brige[i].x, brige[i].y);}printf("\n");
}
int main()
{while(scanf("%d",&n) != EOF){Init();cnt = 0;for(int i=0; i<n; i++){int a, b, m;scanf("%d (%d)",&a,&m);while(m--){scanf("%d", &b);Add(a, b);Add(b, a);}}solve();}return 0;
}

View Code

转载于:https://www.cnblogs.com/daydayupacm/p/5672878.html

无向图求桥 UVA 796相关推荐

  1. (连通图 模板题 无向图求桥)Critical Links -- UVA -- 796

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

  2. lightoj 1026 无向图 求桥

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1026 #include<cstdio> #include<cstri ...

  3. zoj 2588 buring bridges (无向图求桥,含重边)

    注意输出格式即可 #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  4. 牛客小白月赛12 I 华华和月月逛公园 (tarjian 求桥)

    链接:https://ac.nowcoder.com/acm/contest/392/I 来源:牛客网 华华和月月逛公园 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K, ...

  5. tarjan求桥、割顶

    若low[v]>dfn[u],则(u,v)为割边.但是实际处理时我们并不这样判断,因为有的图上可能有重边,这样不好处理.我们记录每条边的标号(一条无向边拆成的两条有向边标号相同),记录每个点的父 ...

  6. 算法笔记--无向图的桥、割点、边双连通分量和点双连通分量

    概念: 桥:无向图中删去一条边使得图不再联通,则这条边称为桥 割点:无向图中删去一个点使得图不再联通,则这个点称为割点 算法: 运用到tarjan算法 关于tarjan算法: https://www. ...

  7. 无向图求起点到终点所有路径

    无向图求起点到终点所有路径 public class Node {public String name = null;public ArrayList<Node> relationNode ...

  8. 无向图的桥+搜索优化--UESTC1956-北极的猴子

    北极的猴子 Time Limit: 1000 MS     Memory Limit: 256 MB Submit Status 也许你不知道,在北极也有猴子,我们叫它们北极猴.北极猴们在北极一共有n ...

  9. Tyvj(无向图的桥)

    题目链接 分析: 无向图的桥 tip 这么裸的题我为什么没有一A呢 因为我又把n和m搞混啦!!! 这里写代码片 #include<cstdio> #include<cstring&g ...

最新文章

  1. 各种函数调用约定及浮点数传参
  2. 多看看把,条件太多了--leetcode 93. 复原 IP 地址
  3. Android下载apk异常java.net.SocketTimeoutException: timeout解决办法
  4. cmd测试cuda安装_安装:anaconda+cuda+pytorch+pycharm
  5. 为什么你的年薪只是别人的月薪?你需要技术专家帮你「充电」
  6. python词云乱码_python词云库wordCloud使用方法详解(解决中文乱码)
  7. 2008安装完了找不到_【专业性】关于铸铝热水锅炉安装使用的思考
  8. 查看tensor的形状,行列大小
  9. 网工年薪100w+,你在哪个阶段?
  10. 不同级别的Java开发人员的应聘要求
  11. 05_流与文件——课后作业
  12. Pravega Flink connector 的过去、现在和未来
  13. 数控g71编程实例带图_数控编程代码g71 数控g71编程实例有图
  14. 阿里云、腾讯云、Testin云测共获“中国云计算创新企业50强”
  15. Google Guava学习(10)-Guava字符串工具CharMatcher
  16. 期权的定义与BSM定价
  17. playwright-python 截图、录制视频、录制接口(二)
  18. pcie gen3 bios设置_唯一还在认真做BIOS的大陆板卡厂商!七彩虹iGame Z490 Vulcan X V20评测...
  19. 瑞盟高精度模数转换器,MS1242,MS1243,
  20. 二维离散余弦变换(DCT)与二维离散反余弦变换(IDCT)C语言实现

热门文章

  1. Oracle 根据字符串的长度排序
  2. stm32 定时器初步
  3. SpringMVC拦截器HandlerInterceptor原理及使用
  4. ArrayList为何线程不安全,如何解决
  5. volatile关键字及JMM模型
  6. JZOJ 5662. 【GDOI2018Day1模拟4.17】尺树寸泓
  7. JZOJ 3600. 【CQOI2014】通配符匹配
  8. BZOJ 2157: 旅游
  9. #地形剖面图_高考地理笔记:经纬网、等值线、地形剖面图知识汇总
  10. 生成对抗网络gan原理_生成对抗网络(GAN)的半监督学习