LightOJ 1026 Critical Links【桥】

博客主要介绍了如何找出计算机网络中的关键链接,即桥。文章详细解释了桥的定义,给出了一个网络示例,并提供了具体的问题描述。博主分享了解决此问题的一种算法——tarjan算法,用于检测图中的桥,并提供了算法思路和代码实现。

参看资料:

https://blog.youkuaiyun.com/zyz_3_14159/article/details/52823070


题目:

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 red: 0 - 13 - 4 and 6 - 7 in figure 2.

Figure 1: Original Graph

Figure 2: The 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

Input starts with an integer T (≤ 15), denoting the number of test cases.

Each case starts with a blank line. The next line will contain n (0 ≤ n ≤ 10000) denoting the number of nodes. Each of the next n lines will contain some integers in the following format:

u (k) v1 v2 ... vk

Where u is the node identifier, k is the number of adjacent nodes; v1, v2 ... vk are the adjacent nodes of u. You can assume that there are at most 100000 edges in total in a case. Dataset is huge, so use faster i/o methods.

Output

For each case, print the case number first. Then you should print 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 and then second element. Since the graph is bidirectional, print a link u v if u < v.

Sample Input

3

 

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

 

2

0 (1) 1

1 (1) 0

Sample Output

Case 1:

3 critical links

0 - 1

3 - 4

6 - 7

Case 2:

0 critical links

Case 3:

1 critical links

0 - 1

Note

Dataset is huge, use faster I/O methods.

题目大意:

       给定一个无向图,求该图桥的个数。

解题思路:

        用 tarjan 算法,其中 dfn[ i ] 表示 dfs 时到第 i 个点的时候 的 时间标号(从0开始),low[ i ] 表示第 i 个点能追溯到 dfn 最小的那个点,那么在 tarjan 枚举的时候,只要 low[ v ]>dfs[ u ]就是桥了,因为第 v 个点没有路能够通往 u 上面的图。

实现代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int N=10010;
const int M=100010;
vector<int>vec[N];
pair<int,int>edge[M];
int low[N],dfn[N],cnt,ans;
void tarjan(int u,int father){
	int i,v;
	low[u]=dfn[u]=cnt++;
	for(i=0;i<vec[u].size();i++){
		v=vec[u][i];
		if(dfn[v]==-1){
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
		}
		else if(father!=v)
			low[u]=min(low[u],dfn[v]);
		if(low[v]>dfn[u]){
			if(u>v)
				edge[ans++]=make_pair(v,u);
			else
				edge[ans++]=make_pair(u,v);
		}
	}
}
int main(){
	int cas,c,i,n,k,u,v,j;
	scanf("%d",&cas);
	for(c=1;c<=cas;c++){
		for(i=0;i<N;i++)
			vec[i].clear();
		cnt=0;
		ans=0;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d (%d)",&u,&k);
			for(j=0;j<k;j++){
				scanf("%d",&v);
				vec[u].push_back(v);
			}
		}
		memset(dfn,-1,sizeof(dfn));
		memset(low,-1,sizeof(low));
		for(i=0;i<n;i++)
			if(dfn[i]==-1)tarjan(i,-1);

		sort(edge,edge+ans);
		printf("Case %d:\n", c);
	    printf("%d critical links\n", ans);
	    for(i=0; i<ans; ++ i)
        printf("%d - %d\n", edge[i].first, edge[i].second);
	}
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值