UVALive 7456Least Crucial Node (并查集)

编程入门 行业动态 更新时间:2024-10-17 23:33:29

Least Crucial Node

题目链接:

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

Description


http://7xjob41.z0.glb.clouddn/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://wwwblogs/Sunshine-tcf/p/5781411.html

更多推荐

UVALive 7456Least Crucial Node (并查集)

本文发布于:2023-06-11 01:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365799.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UVALive   Node   Crucial

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!