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;
    }

  • 相关阅读:
    微软的PDC2009开完了,上去淘Video喽,有不少好东西
    [摘要]Pushing the Limits of Windows: Paged and Nonpaged Pool From Mark Russinovich's blog
    [摘要]Pushing the Limits of Windows: Virtual Memory From Mark Russinovich's blog
    如何知道在Windbg中得到Memory type(也叫做caching type)
    用ImgBurn做了一张可以安装所有版本的Win7安装盘
    WF 工作流(5)
    ORACLE 綁定變量
    oracle inside(1)
    WF 工作流(1)
    WF 工作流(2)
  • 原文地址:https://www.cnblogs.com/chen9510/p/4717335.html
Copyright © 2011-2022 走看看