hdu 1829 a bug's life 5.1.1

本文深入探讨并查集在解决特定问题时的使用技巧,并通过实例演示其在判断生物性别行为一致性上的应用。文章详细介绍了算法的实现过程,包括如何利用并查集来检测是否存在违反假设的虫子行为模式。

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

计划总不如变化快……java和大雾都没看T0T……先贴个题……晚上熬个夜,明天再来贴代码……

熄灯什么的真是伤不起啊……

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 143 Accepted Submission(s): 60
 
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.
 
话说并查集是个什么玩意儿啊听着就觉得好高级啊……

其实发现之前做过并查集的题的……不过当时就觉得晕晕乎乎不会用,现在还是……

两个代码,第一个是用sex【i】保存i与根的性别的比较,同性为0,异性为1

#include <iostream>
using namespace std;

int n,r;
//don't use set and sex next time. it is easy to confuse them.
int set[2002];//store the root 
bool sex[2002];//store the relationship with the root. 1 means different sex, 0 mean same sex.

int find(int x)//find the root of x
{
    if(set[x]==x)return x;//root is self
    int t;
    t=set[x];
    set[x]=find(set[x]);//find the real root
    sex[x]=(sex[x]+sex[t])%2;//compare the sex with root 
    //sex[x]is the relation between x and it's former root. sex[t] is the relation between the former root with the real root
    //plus and remaind can get the relation between x and the real root
    return set[x];
}

void merge(int x,int y)
{
     int fx,fy;
     fx=find(x);
     fy=find(y);
     set[fx]=fy;//put x into y
     sex[fx]=(sex[x]+sex[y]+1)%2;
     //x compare to its root, y compare to its root. 1 means x and y are different sex.
     //plus and remaind so we get fx compare to fy
}
    

int main()
{
    int cas;
    cin>>cas;
    int ci=0;
    int x,y;//temp
    while(cas--)
    {
        ci++;
        int i;
        int flag=0;//0 means no suspicious
        scanf("%d%d",&n,&r);//number of bug, relation
        for(i=0;i<n;i++)//it is n,not r...
        {
            set[i]=i;//everybug's root is it self
            sex[i]=0;
        }
            
        
        while(r--)//it is r,not n...
        {
            
            scanf("%d%d",&x,&y);
            if(find(x)==find(y))
            //have the same root, which means they can be compared
            {
                if(sex[x]==sex[y])//sex is the same
                flag=1;
            }
            else
            merge(x,y);//merge them into one root
            // because the data is simple, so we just simply put x into y. no need to worry about the height of x and y
        }
        printf("Scenario #%d:\n",ci);
            if(flag)
                printf("Suspicious bugs found!\n");
            else
                printf("No suspicious bugs found!\n");
        printf("\n");
    }
    system("pause");
    return 0;
}
            
            
        

说真的find和merge时算sex值好麻烦啊……算着算着就晕了……

sex里面存的是一个异性的编号。每次读入的两个数x,y,如果有异性编号sex[x](sex[y]),就把对方y(x]和该异性sex[x](sex[y])合并。两个数都要做一次。时间比上面的略长一点点。

相对上面的比较容易想到,下面的比较容易吧……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值