zoukankan      html  css  js  c++  java
  • 并查集(2)

    Description

    Background
    Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 
    Problem
    Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

    Input

    The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

    Output

    The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

    Sample Input

    2
    3 3
    1 2
    2 3
    1 3
    4 2
    1 2
    3 4

    Sample Output

    Scenario #1:
    Suspicious bugs found!
    
    Scenario #2:
    No suspicious bugs found!


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int f[2005],y[2005];

    void init(int n)
    {
    for(int i=0;i<n;i++)
    f[i]=i;
    }

    int ff(int x)
    {
    if(f[x]!=x)
    f[x]=ff(f[x]);
    return f[x];
    }

    void join(int a,int b)
    {
    f[ff(a)]=f[ff(b)];
    }

    int main()
    {
    int T,n,m,cas=1;
    scanf("%d",&T);
    while(T--)
    {
    int flag=0;
    cin>>n>>m;
    init(n);
    memset(y,0,sizeof(y));
    while(m--)
    {
    int a,b;
    scanf("%d%d",&a,&b);
    if(!(y[a]+y[b]))
    {
    y[a]=b;
    y[b]=a;
    }
    else if(y[a]&&!y[b])
    {
    y[b]=a;
    join(y[a],b);
    }
    else if(!y[a]&&y[b])
    {
    y[a]=b;
    join(y[b],a);
    }
    else
    {
    join(y[a],b);
    join(y[b],a);
    }
    if(ff(a)==ff(b))
    flag=1;
    }
    printf("Scenario #%d: ",cas++);
    if(flag)
    printf("Suspicious bugs found! ");
    else
    printf("No suspicious bugs found! ");
    if(T)
    printf(" ");
    }
    return 0;
    }

    方法二:

    #include <cstdio>
    #include <iostream>
    #include <cstring>

    using namespace std;

    const int MAX = 2005;

    bool flag;
    int fa, fb, n;
    int father[MAX], rela[MAX];

    int find(int x)
    {
    if(father[x] == -1)
    return x;
    else
    {
    int tmp = father[x];
    father[x] = find(father[x]);

    rela[x] = (rela[x]+rela[tmp])%2;
    return father[x];
    }
    }

    int main()
    {
    int T, m, a, b;
    scanf("%d", &T);
    while(T--)
    {
    flag = true;
    scanf("%d%d", &n, &m);

    memset(father, -1, (n+1)*sizeof(int));
    memset(rela, 0, (n+1)*sizeof(int));
    while(m--)
    {
    scanf("%d%d", &a, &b);
    if(!flag)
    continue;
    fa = find(a);
    fb = find(b);

    if(fa != fb)
    {
    father[fa] = fb;
    rela[fa] = (rela[a] + rela[b] + 1)%2;
    }
    else
    if(rela[a] == rela[b])
    flag = false;
    }
    printf("Scenario #%d: ", k);
    if(flag)
    printf("No suspicious bugs found! ");
    else
    printf("Suspicious bugs found! ");

    }
    return 0;
    }

  • 相关阅读:
    C#规范整理·异常与自定义异常
    C#规范整理·资源管理和序列化
    C#规范整理·泛型委托事件
    C#规范整理·集合和Linq
    <抽象工厂>比<工厂方法>多了啥(区别)
    <工厂方法>比<简单工厂>多了啥(区别)
    Unable to start Ocelot because either a ReRoute or GlobalConfiguration
    MySQL服务安装
    mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”)的处理方法
    使用博客系统发生_STORAGE_WRITE_ERROR_错误
  • 原文地址:https://www.cnblogs.com/chen9510/p/4717335.html
Copyright © 2011-2022 走看看