传送门:

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
 
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]=;
}
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])%;
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=;//根节点相同的且性别相同的则是同性恋。
return ;
}
pa[a]=b;
re[a]=(re[x]-re[y]+)%;//保证 0 1
}
int main()
{
int t,j=;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
flag=;
for(int i=;i<=n;i++)
make_set(i);
for(int i=;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(flag==)
continue;
union_set(a,b);
}
printf("Scenario #%d:\n",j++);
if(flag==)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
 

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

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

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  2. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

  3. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  4. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  5. hdu1829A Bug's Life(种类并查集)

    传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...

  6. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  7. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  8. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

  9. POJ2492:A Bug's Life(种类并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 45757   Accepted: 14757 题 ...

随机推荐

  1. 数组的filter()方法

    filter()也是一个用的不多的方法,但有时候还是比较有用的: 首先,Array.filter()是数组的方法,它作为数组方法被调用,传入一个callback,返回Array中符合callback条 ...

  2. 移动端HTML5实现文件上传

    PC端上传文件多半用插件,引入flash都没关系,但是移动端要是还用各种冗余的插件估计得被喷死,项目里面需要做图片上传的功能,既然H5已经有相关的接口且兼容性良好,当然优先考虑用H5来实现. 用的技术 ...

  3. 普通平衡树Tyvj1728、luogu P3369 (treap)

    您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询排名为x的 ...

  4. Python 批量修改文件名并移动文件到指定目录

    # -*- coding: utf-8 -*- import os, sys,re,shutil from nt import chdir #读取中文路径 u'' path=u"D:\\zh ...

  5. AngularJS开发人员最常犯的10个错误

    简介AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客户 ...

  6. SVN - Checksum mismatch while updating

    Go to the folder with the file causing problems Execute command svn update --set-depth empty (note: ...

  7. flask代码统计作业

    用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...

  8. 如何安装window7 语言包

    很多下载的语言包都是.exe文件,双击后生成lp.cab文件. 这时马上把文件复制到另外一个文件夹中,直接拖最快. 这是 window7 sp1 的很多语言包下载地址(企业版和旗舰版通用):http: ...

  9. 平均负载(Load average)

    load average 的含义平均负载(load average)是指系统的运行队列的平均利用率,也可以认为是可运行进程的平均数. top命令中load average显示的是最近1分钟.5分钟和1 ...

  10. .net core系列之《新一代的配置系统Configuration在支持多数据源,热更新,层级化方面代码快速实践》

    在我们之前.Net Framework的项目中,配置文件是WebConfig或AppcConfig文件,而当我们想要添加我们自定义的节点时,还需要在这个文件中的section中定义我们自定义的节点,这 ...