UVALive 7456 Least Crucial Node (并查集)

Least Crucial Node

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127401#problem/C

Description


http://7xjob4.com1.z0.glb.clouddn.com/e15d7d11650607e5795d28e1120d7109

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


题意:


求标号最小的最大割点.
(删除该点后,指定点#sink能到达的点数减少最多).


题解:


由于数据规模巨水,直接对枚举各点跑一遍并查集(或dfs)计算删除后的减少量.
(不要对源点#sink跑并查集,因为割点不能是#sink).


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int fa[maxn];
int _rank[maxn];

void init() {
    for(int i=0; i<maxn; i++) {
        fa[i] = i;
        _rank[i] = 0;
    }
}

int find_set(int x) {
    return x==fa[x]? x:find_set(fa[x]);
}

void unit_set(int x, int y) {
    x = find_set(x);
    y = find_set(y);
    if(_rank[x] < _rank[y]) swap(x,y);
    fa[y] = x;
    if(_rank[x] == _rank[y]) _rank[x]++;
}

int x[maxn*maxn], y[maxn*maxn];

int main(int argc, char const *argv[])
{
    //IN;

    int n;
    while(scanf("%d", &n) != EOF && n)
    {
        int sink, m;
        scanf("%d %d", &sink, &m);
        int ans = 0;

        int init_ans = 0;
        init();
        for(int i=1; i<=m; i++) {
            scanf("%d %d", &x[i], &y[i]);
            unit_set(x[i], y[i]);
        }

        for(int i=1; i<=n; i++) {
            if(find_set(i) == find_set(sink)) init_ans++;
        }

        int p_ans = 0;
        for(int i=1; i<=n; i++) if(i!=sink){
            init();
            for(int j=1; j<=m; j++) {
                if(x[j]==i || y[j]==i) continue;
                unit_set(x[j], y[j]);
            }
            int cnt = 0;
            for(int j=1; j<=n; j++) {
                if(find_set(j) == find_set(sink)) cnt++;
            }

            if(init_ans-cnt > ans) {
                ans = init_ans - cnt;
                p_ans = i;
            }
        }

        printf("%d\n", p_ans); //a
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5781411.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值