关闭

UVALive Problem 7456 Least Crucial Node——Regionals 2015 :: Asia - Taipei

标签: acm并查集
413人阅读 评论(0) 收藏 举报
分类:

此文章可以使用目录功能哟↑(点击上方[+])

 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.

 Sample Input

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

 Sample Output

3
2

 Problem Idea

解题思路:

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

问对于点k,剩下的n-1个结点中最至关重要的点是哪个点

对于最至关重要的点的定义:删除该点之后会使得能与点k连通的点的个数最少

若有多个符合条件的点,取标号最小的


【类型】
暴力枚举+并查集

【分析】
因为n的大小范围为2 ≤ n ≤ 100,显然可以暴力枚举每个点

看删除该点之后仍与k连通的结点有多少个,取最小的那个即可

而判断与k是否连通,可以通过并查集实现,与点k在一个集合中说明与点k连通

如样例二







由上可知,删除结点2或结点5时,剩下的点与点3连通的都只有3个,那么点2和点5都是最至关重要的点

此时需选取标号最小的结点,于是点2就是最终结果

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

题目链接→UVALive Problem 7456 Least Crucial Node

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#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;
}

菜鸟成长记

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    博客通知
    个人资料
    • 访问:210541次
    • 积分:5428
    • 等级:
    • 排名:第4902名
    • 原创:318篇
    • 转载:13篇
    • 译文:0篇
    • 评论:389条
    博客专栏
    最新评论