zoukankan      html  css  js  c++  java
  • poj1703 Find them, Catch them 并查集

    poj(1703)

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 26992   Accepted: 8188

    Description

    The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.) 

    Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds: 

    1. D [a] [b] 
    where [a] and [b] are the numbers of two criminals, and they belong to different gangs. 

    2. A [a] [b] 
    where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang. 

    Input

    The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

    Output

    For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

    Sample Input

    1
    5 5
    A 1 2
    D 1 2
    A 1 2
    D 2 4
    A 1 4
    

    Sample Output

    Not sure yet.
    In different gangs.
    In the same gang.

    题目大意:一个城市里有两个帮派,n代表帮派成员编号,有m次操作,D[a][b]代表a和b不是一个帮派,A[a][b]询问a和b是什么关系,即:关系不确定,在一个帮派,不再一个帮派。

    分析:并查集,用一个辅助数组pre[],(pre[a]=b||pre[b]=a)代表a的敌人是b,b的敌人是a,输入D的时候有四种情况构造并查集

    (1)若pre[a]==-1&&pre[b]==-1,则:pre[a]=b;pre[b]=a;

    (2)若pre[a]==-1&&pre[b]!=-1:则:pre[a]=b;make(a,pre[b]):把b的敌人和a并在一棵树里,即为朋友;

    (3)若pre[a]1=-1&&pre[b]==-1;则:同上;

    (4)若pre[a]!!=-1&&pre[b]!=-1:则:make(a,pre[b]),make(pre[a],b);

    对于每次A询问:

    若finde(a)==finde(b)一定是朋友;

    否则有两种情况,要么不确定,要么是敌人;

    当:finde(pre[a])==finde(b)即:a的老大的敌人与b的老大是朋友,则a和b一定是敌人

    否则是不确定的

    程序:

    #include"stdio.h"
    #include"string.h"
    #define M 100004
    int f[M];
    int pre[M];
    int finde(int x)
    {
        if(x!=f[x])
            f[x]=finde(f[x]);
        return f[x];
    }
    void make(int a,int b)
    {
        int x=finde(a);
        int y=finde(b);
        if(x!=y)
            f[x]=y;
    }
    int main()
    {
        int w,n,m,a,b,i;
        char ch[2];
        scanf("%d",&w);
        while(w--)
        {
            scanf("%d%d",&n,&m);
            for(i=0;i<=n;i++)
                f[i]=i;
            memset(pre,-1,sizeof(pre));
            while(m--)
            {
                scanf("%s%d%d",ch,&a,&b);
                if(ch[0]=='D')
                {
                    if(pre[a]==-1&&pre[b]==-1)
                    {
                        pre[a]=b;
                        pre[b]=a;
                    }
                    else if(pre[a]==-1&&pre[b]!=-1)
                    {
                        make(a,pre[b]);
                        pre[a]=b;
                    }
                    else if(pre[a]!=-1&&pre[b]==-1)
                    {
                        make(b,pre[a]);
                        pre[b]=a;
                    }
                    else
                    {
                        make(pre[a],b);
                        make(pre[b],a);
                    }
                }
                else
                {
                    if(finde(a)==finde(b))
                        printf("In the same gang.
    ");
                    else
                    {
                        if(finde(pre[a])==finde(b))
                            printf("In different gangs.
    ");
                        else
                            printf("Not sure yet.
    ");
                    }
                }
            }
        }
        return 0;
    }
    

  • 相关阅读:
    react-native window下创建Hello(解决创建一路的坑)
    vue2.0 监听滚动 锚点定位
    vue-awesome-swiper 轮播图使用
    vue和react区别
    vuex 管理状态
    vue 解决axios 跨域问题
    判断一个对象是否为空? js
    微信小程序中的自定义组件(components)
    深入理解ES6箭头函数中的this
    vue中组件的data为什么是一个函数
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348267.html
Copyright © 2011-2022 走看看