HDU 1829 A Bug's Life 并查集

本文介绍了一种用于检测bug性别的算法,通过分析一系列虫子互动数据来判断是否存在同性恋行为,确保实验结果符合特定假设。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Bug's Life

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

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!
/*
题目意思就是判断是否为同性
给定一系列数对,例如a和b,表示a和b不是同一种性别
检查一堆数据中是否有同性恋,
找出主要矛盾是如果1喜欢2,2喜欢3,而1又喜欢3,则1 3 同性矛盾。
找出出现类的的矛盾 

rank[]用作记录某点跟根节点的距离,
rank表示当前点到父节点的距离,如果距离是奇数,那么这两个就是异性,否则是同性,
rank不是1就是0 也可以一认为与根节点关系 
这里用它来记录某点跟根节点的关系,所以得到距离模2即可。

*/ 

#include<iostream>
#include<stdio.h>
using namespace std;

int pre[2001],rank[2001];

int find(int x)
{
    int t;
    if(x==pre[x])
        return x;
        
    t=find(pre[x]);// 找到根节点 
    
    rank[x]=(rank[x]+rank[pre[x]])%2;//更新该节点与要更新的节点关系 
    pre[x]=t;
    return t;
} 

int join(int x,int y)
{
    int a=find(x);
    int b=find(y);//二者根
    
    if(a==b)
    {
        if(rank[x]==rank[y])//根相同 而且与根节点的性别的区别也一样,必然同性 
            return 1;
        return 0;
    }
    pre[a]=b;//二者根不同 更新使根a,b有相同的根b 
    //则b不用变 更新a的rank 判断与根b的关系 
    rank[a]=(rank[x]+rank[y]+1)%2;//x与y异性 所以+1 
    //rank[x]表示x与a的关系 rank[y]表示y与b的关系 
    //例如:1+1 x与a异性 y与b异性 x与y异性 a与b异性 rank[a]应该为异性1 
    //例如:1+0 x与a异性 y与b同性 x与y异性 a与b同性 rank[a]应该为同性0  
    return 0; 
}
int main(){
    
    int t,n,m,c=1,i,flag,a,b; 
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        
        memset(rank,0,sizeof(rank));
        for(i=1;i<=n;i++)
            pre[i]=i;
        flag=0;
        
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            if(join(a,b))
                flag=1;
        }
        printf("Scenario #%d:\n",c++);
          if(flag)
            printf("Suspicious bugs found!\n");
         else
            printf("No suspicious bugs found!\n");
       
        printf("\n");
        
    } 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值