现在的位置: 首页 > 综合 > 正文

HDOJ 1829 A Bug’s Life (并查集)

2018年01月12日 ⁄ 综合 ⁄ 共 2369字 ⁄ 字号 评论关闭

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5567    Accepted Submission(s): 1828


Problem 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!
Hint
Huge input,scanf is recommended.
 


题意:

首先给定n只虫子,然后给出m个情况。每个情况给出两只虫子交配,当出现两只虫子为同性恋时,便判错。


解题思路:

首先肯定是并查集,将同一类的虫子并到同一个集合。

我刚开始的时候想把所以有关系的虫子并到一个集合,然后通过其到根节点的步数来判断。结果wa了。。

后来学长指点,将虫子分为两个集合,一个为共,一个为母。

如果两字虫子的父节点为同一个,则为同性恋。

如果两只虫子都没有标记过,则将两只虫子分别归为到不同的性别。


算法

并查集。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[2005],sex[2005],r[2005];
int find(int x)
{
    if (x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}
void uni(int x,int y)
{
    x=find(x);
    y=find(y);
    if (x==y) return;
    else if (r[x]>r[y])
        f[y]=x;
    else if (r[x]==r[y])
    {
        r[x]++;
        f[y]=x;
    }
    else
        f[x]=y;
}
int main ()
{
    int t,n,m,i,a,b,p=1;
    cin>>t;
    while(t--)
    {
        bool ass=true;
        scanf("%d%d",&n,&m);
        memset(f,0,sizeof(f));
        memset(sex,0,sizeof(sex));
        memset(r,0,sizeof(r));
        for (i=1; i<=n; i++)
            f[i]=i;
        for (i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            if (ass)
            {
                if (find(a)==find(b))
                    ass=false;
                else
                {
                    if (!sex[a])
                        sex[a]=b;   //如果a没有被标记过,则使a的异性为b
                    else
                        uni(sex[a],b);  //否则,将a的父节点和b连接
                    if (!sex[b])
                        sex[b]=a;
                    else
                        uni(sex[b],a);
                }
            }
        }
        if (!ass)
            printf("Scenario #%d:\nSuspicious bugs found!\n",p);
        else
            printf("Scenario #%d:\nNo suspicious bugs found!\n",p);
        p++;
        cout<<endl;
    }
    return 0;
}

抱歉!评论已关闭.