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;
    }
    

  • 相关阅读:
    spring boot Mybatis 拦截器,实现拼接sql和修改
    spring security 5.x去除默认前缀
    Umengday08-hive数据仓库建设
    Umengday07-flume、kafka与hdfs日志流转
    Umengday01-nginx模块(环境平台搭建)
    Umeng项目day04
    Java代码~~汽车租赁系统
    java基础知识(一)
    自动化测试知识点杂记,后续再进行整理
    博客第一天
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348267.html
Copyright © 2011-2022 走看看