题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829

题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋

这是一道简单的种类并查集,而且也比较简单只要比较给出的两个点是否有相同祖宗如果有那么他们距离祖宗结点的距离是否为偶数

如果是偶数那么他(她)们必然是同性恋。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e3 + 10;
int n , m , x , y , f[M] , root[M];
void init() {
for(int i = 1 ; i <= n ; i++) {
f[i] = i;
root[i] = 0;
}
}
int find(int x) {
if(x == f[x])
return x;
int t = find(f[x]);
root[x] = (root[f[x]] + root[x]) & 1;
f[x] = t;
return t;
}
bool Union(int x , int y) {
int a = find(x) , b = find(y);
if(a == b) {
if(root[x] == root[y])
return false;
}
else {
f[a] = b;
root[a] = (root[x] + root[y] + 1) & 1;
}
return true;
}
int main() {
int t , ans = 0;
scanf("%d" , &t);
while(t--) {
bool flag = true;
scanf("%d%d" , &n , &m);
init();
for(int i = 1 ; i <= m ; i++) {
scanf("%d%d" , &x , &y);
if(!flag)
continue;
flag = Union(x , y);
}
printf("Scenario #%d:\n" , ++ans);
if(!flag) {
printf("Suspicious bugs found!\n");
}
else {
printf("No suspicious bugs found!\n");
}
printf("\n");
}
return 0;
}

hdu 1182 A Bug's Life(简单种类并查集)的更多相关文章

  1. 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/Oth ...

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

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

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

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

  4. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  5. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  6. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 3038 How Many Answers Are Wrong(种类并查集)

    题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...

  8. hdu1829&&poj2492 A Bug's Life 基础种类并查集

    把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...

  9. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

随机推荐

  1. How to check all timestamps of a file

    A friend of mine she asked me how to check all timestamps of a file on an NTFS volume. She did not h ...

  2. 如何在docker下安装elasticsearch(上)

    一 环境 VMware® Workstation 15 Pro centos7 (1810) docker19.03.1 二 进入centos7启动dcoker systemctl start doc ...

  3. 有趣的Flex布局

    对于刚接触前端的小白,在还原页面样式的时候,往往会遇到页面布局(layout)上的问题,用着生硬的padding来固定盒子的位置,不仅代码看的沉重,还得适应各种浏览器页面,始终没有办法做到统一.接下来 ...

  4. Tomcat源码分析 (一)----- 手写一个web服务器

    作为后端开发人员,在实际的工作中我们会非常高频地使用到web服务器.而tomcat作为web服务器领域中举足轻重的一个web框架,又是不能不学习和了解的. tomcat其实是一个web框架,那么其内部 ...

  5. sharding demo 读写分离 U (分库分表 & 不分库只分表)

    application-sharding.yml sharding: jdbc: datasource: names: ds0,ds1,dsx,dsy ds0: type: com.zaxxer.hi ...

  6. 前端登录jq图形验证码

    <!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...

  7. hdu1241 油田计数

    具体思路:求联通块,在"@“的周围进行dfs,使用8个方向向量来代表搜索的方向 贴一下我的主要代码段: int dir[8][2]={{1,1},{-1,-1},{1,-1},{-1,1}, ...

  8. koa2图片上传成功后返回服务器地址,实时显示服务器图片

    版本:node(8.5.0); koa(2.4.1); koa-router(7.3.0); koa-body(2.5.0); koa-static(4.0.2); 代码实现 const fs = r ...

  9. Nunit与Xunit介绍

    Nunit安装 首先说下,nunit2.X与3.X版本需要安装不同的vs扩展. nunit2.x安装 安装如上3个,辅助创建nunit测试项目与在vs中运行单元测试用例 . 1.Nunit2 Test ...

  10. linux之压缩和解压

    归档:也称为打包,指的是一个文件或目录的集合,而这个集合被存储在一个文件中.归档文件没有经过压缩,因此,它占用的空间是其中所有文件和目录的总和.压缩:压缩文件也是一个文件和目录的集合,且这个集合也被存 ...