zoukankan      html  css  js  c++  java
  • Find them, Catch them

    Find them, Catch them
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 36488 Accepted: 11188

    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.

    Source
    POJ Monthly–2004.07.18
    题意:有两个犯罪团伙,D 操作就是表明两个人不是同一伙,A 查询两个人的关系

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define LL long long
    using namespace std;
    
    const int MAX = 110000;
    
    struct node
    {
        int pre;
        int rea;
    }a[MAX];
    
    int n,m;
    
    void init()
    {
        for(int i=0;i<=n;i++)
        {
            a[i].pre=i;
            a[i].rea=0;
        }
    }
    
    int Find(int x)
    {
        if(a[x].pre==x)
        {
            return  x;
        }
        int tmp=a[x].pre;
        a[x].pre=Find(tmp);
        a[x].rea=(a[tmp].rea+a[x].rea)%2;
        return a[x].pre;
    }
    
    void Join(int x,int y,int b,int c)
    {
        if(b!=c)
        {
            a[b].pre=c;
            a[b].rea=(1+a[x].rea+2-a[y].rea)%2;
        }
    }
    int main()
    {
        int T;
        char s[10];
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&m);
            init();
            int u,v;
            for(int i=0;i<m;i++)
            {
                scanf("%s %d %d",s,&u,&v);
                int b=Find(u);
                int c=Find(v);
                if(s[0]=='D')
                {
                    Join(u,v,b,c);
                }
                else
                {
                    if(b!=c)
                    {
                        printf("Not sure yet.
    ");
                    }
                    else
                    {
                        if(a[u].rea==a[v].rea)
                        {
                            printf("In the same gang.
    ");
                        }
                        else
                        {
                            printf("In different gangs.
    ");
                        }
                    }
                }
            }
    
        }
        return 0;
    }
    
  • 相关阅读:
    阿里双11,如何实现每秒几十万的高并发写入
    记住:永远不要在 MySQL 中使用 UTF-8
    史上最烂的项目:苦撑 12 年,600 多万行代码
    除了不要 SELECT * ,使用数据库还应知道的11个技巧!
    厉害了,为了干掉 HTTP ,Spring团队又开源 nohttp 项目!
    请停止学习框架
    基于 MySQL 主从模式搭建上万并发的系统架构
    JS获取节点
    JS函数
    JS
  • 原文地址:https://www.cnblogs.com/juechen/p/5255996.html
Copyright © 2011-2022 走看看