# UVALive Problem 7456 Least Crucial Node

Accept: 0    Submit: 0
Time Limit: 3.000 seconds

## Problem Description

A wireless sensor network is a self-organizing network without specific infrastructure. It is a multi-hop network in which sensor nodes can be randomly deployed and the data transmission between nodes usually involves other intermediate nodes. Sensor nodes are often deployed in outdoor or hazardous environments. Power outage of sensors can lead to node failure and cause many serious problem. For example, node failure can disconnect the whole network, causing data from one part of the network to fail to transmit to another part of the network. It is difficult to replace failed sensor nodes in hazardous conditions or far-reaching areas such as rainforests or high mountains.

Wireless sensor networks usually connect to the outside world through the so called sinks (may be regarded as gateways). All data collected by sensor nodes are sent to the sink, and then the sink relays such data to remote users or servers through the Internet, satellite, or any viable medium.

A wireless sensor network can be modeled by a graph with each vertex (edge) representing a sensor (a two-way communication link). In the following figure, nodes 4, 10, and 14 are more crucial to the connectivity of the network since a power shortage in any one of them can lead to a disconnected network.

Suppose that node 1 is the sink node. The failure of node 4 disconnects nodes in the set {5, 6, 7, 8, 9, 10, 11, 12, 13, 14} from the sink. The failure of node 10 disconnects the nodes in the set {11, 12, 13, 14} from the sink. Lastly, the failure of node 14 disconnects the nodes in {12, 13} from the sink. This means node 4 is more crucial than nodes 10 and 14 in network connectivity; thus we call node 4 a crucial node of the network. Notice that crucial nodes do not include the sink. Specifically, the failure of a crucial node in a given sensor network disconnects the largest number of nodes from the sink. Moreover, the least crucial node is a crucial node with the least number label among all the crucial nodes.

Please write a program to find the least crucial node for a given sensor network with a specific sink.

## Input

There are several input lines to a test case. The first line of each test case contains an integer n (2 ≤ n ≤ 100), where n is the number of nodes (vertex set of G) labeled with {1, 2, . . . , n} in the network. The second line contains an integer, which is the label of the unique sink of the network. The third line contains an integer m, indicating the number of communication links in the network. The next m lines each contains a pair of integers, say i and j (separated by a space), denoting there is a communication link (edge in E) between node i and node j. There are at most 10 test cases and n = 0 denotes end of the test cases.

## Output

The output for each instance should contain an integer denoting the least crucial node in the given network.

4
4
3
1 2
2 3
3 4
6
3
8
1 2
2 3
2 4
2 5
3 4
3 5
4 5
5 6
0

3
2

【题意】
n个结点,m条双向边

【类型】

【分析】

【时间复杂度&&优化】
O(nmlogn)

## Source Code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 105;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int s[N],u[M],v[M];
int fun(int x)
{
if(s[x]!=x)
s[x]=fun(s[x]);
return s[x];
}
int main()
{
int n,k,m,i,j,ans,c,t,Min;
while(scanf("%d",&n)&&n)
{
Min=inf;
scanf("%d",&k);
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d%d",&u[i],&v[i]);
for(i=1;i<=n;i++)
{
if(i==k)
continue;
for(j=1;j<=n;j++)
s[j]=j;
for(j=0;j<m;j++)
if(u[j]!=i&&v[j]!=i)
s[fun(u[j])]=fun(v[j]);
c=fun(k);t=0;
for(j=1;j<=n;j++)
if(fun(j)==c)
t++;
if(t<Min)
Min=t,ans=i;
}
printf("%d\n",ans);
}
return 0;
}

08-23 89

08-17 158

08-31 285

10-28 67

08-23 77

09-26 868

01-05 175

09-26 1973

08-29 820

08-29 571