传送门:

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. java设计模式-----7、装饰模式

    首先,什么是装饰者模式呢??? 装饰( Decorator )模式又叫做包装模式.通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案.他是23种设计模式之一,英文叫Decorator ...

  2. JavaScript的进阶之路(五)理解数组2

    数组方法 //定义一个测试数组 var array1 = [1,2,5,null,"a"]; //join()方法是String.split()方法的逆操作,后者是将字符串分割成若 ...

  3. h5 简单拖放

    最新的HTML5标准为所有的html元素规定了一个draggable属性,它表明了元素是否可以拖动,默认情况下,图像,链接,选中的文字是可以拖动的,因为他们的draggable属性被自动设置为true ...

  4. html+css 百度首页练习

    这几天看完了<css权威指南>,写了个百度页面,不带js的纯静态,主要目的就是掌握页面布局,字体颜色之类的没有深究. 写完了觉得很简单,毕竟一开始觉得只要模仿的像就行,但是缩小了浏览器窗口 ...

  5. 基于Vue的WebApp项目开发(五)

    实现图片分享列表 步骤一:新增图片列表文件photolist.vue <template> <div id="tml"> 图片分享页面 </div&g ...

  6. JAVA后台框架优化之日志篇

    1.日志规范 各业务系统日志需要统一,以方便查看.收集日志, 日后统一ELK日志管理,以下为项目的日志配置, 这是兼容当前系统的日志,以后推行微服架构时会有变动,但日志存放方式不会改变,日后会推行sp ...

  7. centos7安装lua语言环境

    Linux 上安装 Lua 安装非常简单,只需要下载源码包并在终端解压编译即可. 官网地址:http://www.lua.org/download.html 我这里安装的是:lua-5.3.0.tar ...

  8. vue学习(一)、Vue.js简介

    Vue.js 五天 汤小洋一. Vue.js简介1. Vue.js是什么Vue.js也称为Vue,读音/vju:/,类似view,错误读音v-u-e 版本:v1.0 v2.0 是一个构建用户界面的框架 ...

  9. 排查 Azure 虚拟机的远程桌面连接问题

    与基于 Windows 的 Azure 虚拟机 (VM) 的远程桌面协议 (RDP) 连接可能会因各种原因而失败,使用户无法访问 VM. 问题可能出在 VM 上的远程桌面服务.网络连接或主计算机上的远 ...

  10. centos yum升级php

    centos yum升级php5.3.3到最5.6.3 不要轻易升级,否则后果很严重! 注意事项: 1 升级后之前的php扩展不会丢失 自动会安装对应最新php的扩展2 升级后需重启下apache 才 ...