POJ A Bug's Life POJ 2492(和食物链差不多的一道题)

本文介绍了一个关于虫群交互行为的研究实验,旨在验证一种稀有昆虫是否仅与异性互动的假设。通过构建连通性图并利用并查集算法,文章详细解释了如何检测可能存在的同性互动案例。

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

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.
这道题简直和食物链是相似的,但是我感觉,我比赛的时候没有冷静下来思考才会没做出来,所以比赛时千万不要慌

#include <stdio.h>
//存储的是其父亲的下表
int bugs[2010];
int relation[2010];//1:相同性别 0:不同性别
//初始化
void init(int len)
{
    for(int i = 0;i <= len; i++)
    {
        bugs[i] = i;
        relation[i] = 1;
    }
}
//找到根
int find(int bug)
{
    if(bugs[bug]==bug)return bug;
    int tem = bugs[bug];
    bugs[bug] = find(bugs[bug]);//递归更新域,返回最终的父亲节点,把所有的孩子都更新了
    //注意这里,求当前位置和父亲的关系,记录之前父亲的位置为tem,然后因为是递归,
    //此时的relation[tem]已经在递归中更新过了,也就是孩子和父亲的关系+父亲和爷爷的关系+1然后模2就得到
    //孩子和爷爷的关系,这里用0和1表示,0表示不同性别,1表示相同性别
    relation[bug] = (relation[bug]+relation[tem]+1)%2;
    return bugs[bug];
}

void union_set(int a,int b,int x,int y)
{
    //合并,让前边的集合的根指向后边集合的根,成为一个集合
    bugs[x]=y;
    //更新前边集合根和新的集合根之间的关系,
    //注意这里,relation[a]+relation[x]与relation[b]
    //相对于新的父节点必须相差1个等级,因为他们不是gay
    relation[x] = (relation[b]-relation[a])%2;
}
int main()
{
    int S;
    int n,inter;
    int bug1,bug2,parent1,parent2;
    bool flag;//false:无同性恋,true:有同性恋
    scanf("%d",&S);
    for(int i=1; i<=S;i++)
    {
        scanf("%d%d",&n,&inter);
        flag = false;
        init(n);//初始化,使其父节点为自己
        for(int j = 1; j <= inter; j++)
        {
            scanf("%d%d",&bug1,&bug2);
            if(flag)continue;
            parent1 = find(bug1);
            parent2 = find(bug2);
            if(parent1==parent2)
            {
                if(relation[bug1]==relation[bug2])//同性
                flag = true;
            }
            union_set(bug1,bug2,parent1,parent2);
        }
        if(flag)
        printf("Scenario #%d:\nSuspicious bugs found!\n",i);
        else
        printf("Scenario #%d:\nNo suspicious bugs found!\n",i);
        printf("\n");
    }
    return 0;
}

另一种做法:

我终于把这个题弄懂了,这题就是将不同结点,形成一个单链树
到最后,所有的点,都只有一个根结点,然后判断根结点会不会有分支,以及u,v到根结点距离的奇偶性来判断会不会存在同性链

#include "stdio.h"
#include "string.h"

int f[3000];
int rank[3000];

int find(int x)
{
    int t;
    if(x==f[x])
        return x;

    t=find(f[x]);
    rank[x]=(rank[x]+rank[f[x]])%2;//在更新父节点之前要记录该点与要更新的那个点之间的关系
    f[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;
    }
    f[a]=b;
    rank[a]=(rank[x]+rank[y]+1)%2;//X与Y是异性,那么a与b是什么关系呢,将X与Y连接起来距离加1,所以将X与a的距离加Y与b的距离再加1^_^
    return 0;
}

int main()
{
    int cas,c=1;
    int n,m;
    int i,flag,a,b;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        memset(rank,0,sizeof(rank));

        for(i=1;i<=n;i++)
        {
            f[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\n");
        else
            printf("No suspicious bugs found!\n\n");
          }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值