zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    模板语法 DTL(Django Template Language )
    django基础
    day1,基本标签总结
    聚合函数
    day1
    day 3 定时任务
    day 4 tar
    day 6
    day1 mysql
    day 6
  • 原文地址:https://www.cnblogs.com/rainydays/p/2078375.html
Copyright © 2011-2022 走看看