• poj1703


    简单并查集

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 100005

    int n, m;
    int f[maxn], father[maxn];

    int getanc(int a)
    {
    if (a == father[a])
    return a;
    return father[a] = getanc(father[a]);
    }

    void merge(int a, int b)
    {
    father[getanc(a)]
    = getanc(b);
    }

    void differ(int a, int b)
    {
    if (f[a] == -1)
    f[a]
    = b;
    else
    merge(f[a], b);
    if (f[b] == -1)
    f[b]
    = a;
    else
    merge(f[b], a);
    }

    void ask(int a, int b)
    {
    if (f[a] == -1 || f[b] == -1)
    {
    printf(
    "Not sure yet.\n");
    return;
    }
    if (getanc(a) == getanc(b))
    {
    printf(
    "In the same gang.\n");
    return;
    }
    if (getanc(a) == getanc(f[b]))
    {
    printf(
    "In different gangs.\n");
    return;
    }
    printf(
    "Not sure yet.\n");
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    while (t--)
    {
    memset(f,
    -1, sizeof(f));
    scanf(
    "%d%d", &n, &m);
    getchar();
    for (int i = 0; i < n; i++)
    father[i]
    = i;
    for (int i = 0; i < m; i++)
    {
    char ch;
    int a, b;
    scanf(
    "%c%d%d", &ch, &a, &b);
    a
    --;
    b
    --;
    getchar();
    if (ch == 'D')
    differ(a, b);
    else
    ask(a, b);
    }
    }
    return 0;
    }

  • 相关阅读:
    新的学习计划
    Python学习搬家啦
    安装数据库软件
    oracle11g RAC之grid安装
    PG源码编译安装
    vnc图形化远程centos7.6步骤
    postgresql 日期格式
    centos配置yum源
    pg创建多个实例
    PostgreSQL配置
  • 原文地址:https://www.cnblogs.com/rainydays/p/2078375.html
Copyright © 2020-2023  润新知