POJ2492 A Bug's Life

本文介绍了一道名为POJ 2492的问题解决方案,使用并查集算法来判断是否存在同性恋关系的虫子,通过记录每一对虫子之间的关系类型进行分析。

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

http://poj.org/problem?id=2492


搞基题啊我操,给的就是n个虫子,m对虫子发生过关系 然后问有没有虫子是同性恋。。。


并查集,需要用t数组保存x对f[x]的关系,0为同性,1为异性。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-8;
const double INF=1e50;
//const double pi=acos(-1);

#define N 2005
#define M 1000005

int f[N],n,m,t[N];

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

    int fx=f[x];
    f[x]=find(f[x]);
    t[x]=(t[x]+t[fx])%2;//这里不可t[x]=(t[x]+t[f[x]])%2

    return f[x];
}

int main()
{
    freopen("a","r",stdin);

    int i,k,T,a,b;
    scanf("%d",&T);
    for (int kk=1;kk<=T;kk++)
    {
        if (kk>1) printf("\n");
        printf("Scenario #%d:\n",kk);

        scanf("%d%d",&n,&m);
        for (i=1;i<=n;i++)
        {
            f[i]=i;
            t[i]=0;
        }
        bool flag=true;

        for (k=1;k<=m;k++)
        {
            scanf("%d%d",&a,&b);
            int fa=find(a),fb=find(b);

            if (fa==fb)
            {
                if (t[a]==t[b]) flag=false;
            }
            else
            {
                f[fb]=fa;
                t[fb]=(t[b]+t[a]+1)%2;
            }
        }

        if (flag==true) printf("No suspicious bugs found!\n");
        else printf("Suspicious bugs found!\n");
    }

    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值