24h購物| | PChome| 登入
2012-03-25 20:52:07| 人氣1,760| 回應0 | 上一篇 | 下一篇

[UVA][CutEdge] 796 - Critical Links

推薦 0 收藏 0 轉貼0 訂閱站台


  Critical Links 

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$

$server_0 (no_of_direct_connections) connected_server dots connected_server$

...

$server_{no_of_servers} (no_of_direct_connections) connected_server dots 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 le k le 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


做法 : 一次 DFS 把樹形畫出來, 求深度

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define oo 1000
int link[100][100], n;
int depth[100], low[100];
int used[100];
int cutBridge[100][100];
int DFS(int node, int d, int parent) {
int i, back = oo, tmp;
depth[node] = d;

for(i = 0; i < n; i++) {
if(link[node][i] == 1) {
if(used[i] == 0) {
used[i] = 1;
tmp = DFS(i, d+1, node);
if(tmp > d) {
cutBridge[node][i] = 1;
cutBridge[i][node] = 1;
}
back = back < tmp ? back : tmp;
} else {
if(i != parent)
back = back < depth[i] ? back : depth[i];
}
}
}
low[node] = back;
return low[node];
}
int main() {
int x, y, m, i ,j, Case = 0;
while(scanf("%d", &n) == 1) {
memset(link, 0, sizeof(link));
memset(depth, 0, sizeof(depth));
memset(low, 0, sizeof(low));
memset(used, 0, sizeof(used));
memset(cutBridge, 0, sizeof(cutBridge));
for(i = 0; i < n; i++) {
scanf("%d ", &x);
scanf("(%d)", &m);
while(m--) {
scanf("%d", &y);
link[x][y] = 1;
link[y][x] = 1;
}
}
for(i = 0; i < n; i++) {
if(used[i] == 0) {
used[i] = 1;
DFS(i, 1, -1);
}
}
int ans = 0;
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
if(cutBridge[i][j] != 0)
ans++;
}
}
printf("%d critical links\n", ans);
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++)
if(cutBridge[i][j] != 0)
printf("%d - %d\n", i, j);
}
puts("");
}
return 0;
}

台長: Morris
人氣(1,760) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10227 - Forests
此分類上一篇:[UVA][CutPoint] 315 - Network

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文