pku2492 A Bug's Life

本文介绍了一个基于并查集实现的一夫一妻制检查算法,通过输入夫妻关系来判断是否存在违规现象。使用C++实现,包括并查集的基本操作如查找和合并等。

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

题目意思:一夫一妻制,呵呵。赤裸裸的并查集。

<code>

 

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define maxs 2000
#define maxn 4005
int father[maxn];
void Make_Set()
{
   for(int i=1;i<=maxn;i++)
     father[i]=i;
}
int Find_Set(int x)//²éÕÒÒ»¸öÔªËØËùÔÚ¼¯ºÏ
{
    if(x!=father[x])
    {
       father[x]=Find_Set(father[x]);//µÝ¹é²éÕÒ
    }
    return father[x];
}

void Union(int x,int y)
{
   x=Find_Set(x),y=Find_Set(y);
   if(x<y)
      father[y]=x;
   else if(x>y)
      father[x]=y;
}
int main()
{
    int T,n,m,i,j;
    int x,y;
    int ca=0;
    scanf("%d",&T);
    while(T--)
    {
    
      Make_Set();
      scanf("%d%d",&n,&m);
      for(i=1;i<=m;i++)
      {
         scanf("%d%d",&x,&y);
         if(Find_Set(x)==Find_Set(y))break;
         Union(x,y+maxs);
         Union(x+maxs,y);
      }
      for(j=i+1;j<=m;j++)
        scanf("%d%d",&x,&y);
      printf("Scenario #%d:/n",++ca);
      if(i<=m)
        printf("Suspicious bugs found!/n/n");
      else
        printf("No suspicious bugs found!/n/n");
    }
    system("pause");
    return 0;
}


    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值