本文主要是介绍UVA 796 Critical Links【求桥】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
Description:
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 . . . server
no 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
题目大意:
无向图求桥。多组输入。
解题思路:
求桥模板。
代码实现:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;const int N=1e4+10;struct Edge{int v,flag,next;
}edge[N*20];struct bridge{int u,v;
}ans[N];
bool cmp(bridge a,bridge b){if(a.u!=b.u) return a.u<b.u;else return a.v<b.v;
}int s[N],tot,cnt;
int n,m,dfn[N],low[N],dep;
bool vis[N];void AddEdge(int u,int v){for(int i=s[u]; ~i; i=edge[i].next){if(edge[i].v==v){edge[i].flag++;return ;}}edge[tot].v=v;edge[tot].next=s[u];edge[tot].flag=0;s[u]=tot++;
}void tarjan(int u,int pre){vis[u]=true;dfn[u]=low[u]=++dep;for(int i=s[u]; ~i; i=edge[i].next){int v=edge[i].v;if(v==pre) continue;if(!vis[v]){tarjan(v,u);low[u]=min(low[u],low[v]);}else low[u]=min(low[u],dfn[v]);if((low[v]>dfn[u])&&!edge[i].flag){ans[cnt].u=u;ans[cnt].v=v;if(u>v) swap(ans[cnt].u,ans[cnt].v);cnt++;}}
}int main(){while(~scanf("%d",&n)){memset(s,-1,sizeof s);tot=cnt=0;for(int i=1; i<=n; i++){int u,v,k;scanf("%d (%d)",&u,&k);u++;for(int j=1; j<=k; j++){scanf("%d",&v);v++;AddEdge(u,v);}}memset(vis,0,sizeof(vis));dep=0;for (int i=1;i<=n;i++)if(!vis[i]) tarjan(i,i);sort(ans,ans+cnt,cmp);printf("%d critical links\n",cnt);for(int i=0; i<cnt; i++)printf("%d - %d\n",ans[i].u-1,ans[i].v-1);printf("\n");}return 0;
}
这篇关于UVA 796 Critical Links【求桥】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!