• 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).
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #include #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[maxnmaxn], y[maxnmaxn];

    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
    ", p_ans); //a
    }
    
    return 0;
    

    }

  • 相关阅读:
    初识JSP知识
    Session每次访问都回新建对象问题的处理
    C++ 重载中括号
    C++ 重载输出符号
    C++ 单目运算 and 双目运算符重载complex
    C++运算符重载范例
    c++ 运算符重载之成员函数重载
    c++ 友元函数
    c++ static 类
    c++ const 类
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5781411.html
Copyright © 2020-2023  润新知