HDU 1829 A Bug's Life (种类并查集)

传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1829

A Bug‘s Life

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

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.

Source

TUD Programming Contest 2005, Darmstadt, Germany

Recommend

linle

题目意思:

Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b。只需要判断有没有,按题目要求输出

分析:

两个种类(两性)

种类并查集

判断存在同性恋的标准:

根节点相同的且性别相同的则是同性恋。

code:

#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define max_v 2005
int pa[max_v];//前驱
int re[max_v];//性别
int flag;
void make_set(int x)
{
    pa[x]=x;
    re[x]=0;
}
int find_set(int x)
{
    if(x==pa[x])
        return pa[x];
    int t=find_set(pa[x]);
    re[x]=(re[pa[x]]+re[x])%2;
    pa[x]=t;
    return pa[x];
}
void union_set(int x,int y)
{
    int a=find_set(x);
    int b=find_set(y);
    if(a==b)
    {
        if(re[x]==re[y])
            flag=1;//根节点相同的且性别相同的则是同性恋。
        return ;
    }
    pa[a]=b;
    re[a]=(re[x]-re[y]+3)%2;//保证 0 1
}
int main()
{
    int t,j=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        flag=0;
        for(int i=1;i<=n;i++)
            make_set(i);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(flag==1)
                continue;
            union_set(a,b);
        }
        printf("Scenario #%d:\n",j++);
        if(flag==1)
             printf("Suspicious bugs found!\n");
        else
           printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}

HDU 1829 A Bug's Life (种类并查集)

原文地址:https://www.cnblogs.com/yinbiao/p/9432338.html

时间: 2024-08-08 03:05:32

HDU 1829 A Bug's Life (种类并查集)的相关文章

hdoj 1829 A bug&#39;s life 种类并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有两种性别,每次m次操作,每次给出(a,b),如果a和b是同性别就出现了错误,也就是说出现了判断它有两种性别的错误.我的策略同样是两个集合,用并查集维护两个集合之间的关系.具体证明请看我的上一篇博客,关于这种做法的正确性的证明. 代码如下: 1 #include<bits/stdc++.h> 2 t

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

HDU 1829 A Bug&#39;s Life (并查集)

题目链接:请戳这里. 题目大意及思路:给定n个Bugs(shen me gui)和m对Bugs之间的关系,假定关系是两个Bugs的男女关系,那么问存不存在同性恋的情况. 那么若a与b是男女关系,b与c是男女关系,那么a和c的性别我们就可以认为是相同的.我们用可以建立两个并查集,一类放男男关系,一类放女女关系. 那么若男男关系中出现了环的情况(即有共同的根),那么同性恋关系就出现了. #include<iostream> #include<cstdio> #include<cs

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

【POJ】2492 A bug&#39;s life ——种类并查集

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

hdu1829A Bug&#39;s Life(种类并查集)

传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 1 #include<bits/stdc++.h> 2 using namespace std; 3 int f[2005],n,m,rankk[2005]; 4 bool flag; 5 inline void init() 6 { 7 flag=false; 8 for(int i=0; i<=n; ++i)

hdu 5285 BestCoder Round #48 ($) 1002 种类并查集

// M == 0 有trick... N < 2 也有trick...... 1 #include"iostream" 2 #include"cstdio" 3 #include"cstring" 4 #include"map" 5 using namespace std; 6 int N, M; 7 int rt[100010], w[100010]; 8 int Size[100010], Friend[10001

hdu 1829 A Bug&#39;s Life (基础种类并查集)

先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所

带权并查集(含种类并查集)【经典模板】 例题:①POJ 1182 食物链(经典)②HDU - 1829 A bug&#39;s life(简单) ③hihoCoder 1515 : 分数调查

带权并查集: 增加一个 value 值,并且每次合并和查找的时候需要去维护这个 value 例题一 :POJ 1182 食物链(经典) 题目链接:https://vjudge.net/contest/339425#problem/E 带权并查集的解法 定义两个数组fa[ ]和rela[ ],fa用来判断集合关系,rela用来描述其与根节点的关系.因为关系满足传递性,所以可以推导出给出条件下的当前关系,在判断与之前已有关系是否矛盾. 本题的解法巧妙地利用了模运算,rela数组用0表示同类,1表示当