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

                                                                                                    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 42031   Accepted: 12923

    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个人,两个帮派,每个人都属于其中的某一个帮派,输入字符D时会给信息,告诉你哪两个人不是一个帮派的,输入字符A时让你判断两个人之间的关系(是否一帮派等等)
    思路:并查集,每个人具体是哪个帮派的人并不知道,所以创建2*N个元素,其中元素x和x+N分别代表一个人x属于帮派1和帮派2。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N_MAX = 100000*2+1;
    int par[N_MAX];
    int Rank[N_MAX];
    
    void init(const int &n) {
        for (int i = 0;i < n;i++) {
            par[i] = i;
            Rank[i] = 0;
        }
    }
    int find(const int&x) {
        if (par[x] == x)
            return x;
        else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (Rank[x] < Rank[y]) {
            par[x] = y;
        }
        else {
            par[y] = x;
            if (Rank[x] == Rank[y])Rank[x]++;
        }
    }
    bool same(const int &x,const int&y) {
        return find(x) == find(y);
    }
    
    
    int main() {
        int T,N,M;
        cin >> T;
        while (T--) {
            scanf("%d%d",&N,&M);
            init(2*N);//元素x和x+N分别代表元素x分别属于两个帮派
            char op;int x, y;
            getchar();//////!!!!!!!!!!!!!!!!!!千万别忘,不然会影响下面的输入
            while(M--) {
                scanf("%c%d%d",&op,&x, &y);
                //scanf("%c%d%d%*c", &op, &x, &y);
                x--;y--;
                if (op == 'A') {//需要判断两个元素是否属于一个帮派
                    if (same(x, y))printf("In the same gang.
    ");//x,y在同一个帮派
                    else if (same(x, y + N))printf("In different gangs.
    ");//x,y在不同帮派
                    else printf("Not sure yet.
    ");
                }
                else if (op == 'D') {//合并元素的操作,x,y不在同一帮派
                    unite(x,y+N);
                    unite(x + N, y);
                }
                cin.ignore();
            }
    
        }
    
        return 0;
    }



  • 相关阅读:
    为啥负利率国债有人抢着买?因为时代变了
    微增长时代
    U盘插入电脑后图标是灰色的,打开提示“请将磁盘插入驱动器”
    计算shell 脚本的执行时间
    win10系统应用商店打开后无法联网 代码: 0x80131500 的解决办法
    Jetbrains家的软件都可用的激活码-pycharm
    postman中x-www-form-urlencoded与form-data的区别
    升级Gogs版本
    上海对售价超1499元的茅台酒即没收并另处罚款
    提高收入的根本途径
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/5906356.html
Copyright © 2011-2022 走看看