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

    题意:有两个帮派,每个人只属于一个帮派,m次操作,一种操作告诉你两个人不是一个帮派的,另一种操作问两个人是不是在一个帮派。

    解法:并查集+向量偏移。偏移量表示和根节点是不是同一帮派,是为0,不是为1。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    
    using namespace std;
    
    int father[100005], delta[100005];
    void init()
    {
        memset(delta, 0, sizeof delta);
        for(int i = 0; i < 100005; i++)
            father[i] = i;
    }
    int Find(int a)
    {
        if(father[a] != a)
        {
            int tmp = Find(father[a]);
            delta[a] = (delta[a] + delta[father[a]]) % 2;
            father[a] = tmp;
        }
        return father[a];
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            init();
            int n, m;
            scanf("%d%d", &n, &m);
            while(m--)
            {
                char o[2];
                int x, y;
                scanf("%s%d%d", o, &x, &y);
                int c = Find(x), d = Find(y);
                if(o[0] == 'A')
                {
                    if(c != d) puts("Not sure yet.");
                    else
                    {
                        if(delta[x] == delta[y]) puts("In the same gang.");
                        else puts("In different gangs.");
                    }
                }
                else
                {
                    if(c == d) continue;
                    father[c] = d;
                    delta[c] = (delta[y] - delta[x] + 1) % 2;
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    SQL语句导入导出大全
    数据库连接大全
    数据库关键字过滤问题
    HttpHandler
    asp代码的一个do。。。wile循环代码
    vs .net 2005 的Global.asax 在哪添加
    asp.net项目ckeditor+ckfinder实现图片上传
    CKEditor+CKFinder的图片上传配置
    asp实现统计访问量的代码
    asp.net绘统计图
  • 原文地址:https://www.cnblogs.com/Apro/p/4786915.html
Copyright © 2011-2022 走看看