A Bug's Life(hdu1829种类并查集)
题意:有一群虫子,现在给你一些关系,判断这些关心有没有错
思路:向量种类并查集,下面讲一下向量的种类并查集
本题的各个集合的关心有两种0同性,1异性,怎么判断有错,
1.先判断他们是否在一个集合,即父亲是否相同,若父亲相同,用向量算的父亲之间的关系和之前的关心有矛盾没,有矛盾就是有错。
2.如果不在一个集合,就合并他们的父亲,用向量根据根节点的关心求出父亲的关心,然后合并。
这是并查集的重要的操作路径压缩,路径压缩的时候关心就要更新比如 1- 2 是异性 2 - 3 是异性 那么1,2,3在同一集合父亲都是1,压缩过后1和3就是同性
这就路径压缩时的关系更新,关键是怎么更新呢?
假设 x 的父亲节点rootx,rootx的父亲是rootxx
图一 是合并前图二 是合并时
有点像向量,向量加法首尾相接 所以rootxx和x的关系就是 (rootxx --> rootx + rootx-->x) % 2;
同理根据孩子节点推理父亲关系的时候也是
假设x的父亲rootx y的父亲rooty 合并时x的父亲是y,如图
如果想要知道rootx --> rooty的关系,rootx--> x + x --> y + y --> rooty
因为向量是相反为负的所以 y-->rooty = 2 - rooty-->y;
所以 rootx --> rooty = rootx--> x + x --> y + 2 - rooty-->y;
以为 题目x -- > y 为1 所以 rootx --> rooty = rootx--> x +1 + 2 - rooty-->y;
向量一定要注意方向,一定是父亲指向儿子,不能弄反。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; struct bian
{
int parent;
int relation;
}p[10005];
void Make_set(int n)
{
int i;
for(i = 0; i < n; i++)
{
p[i].parent = i;
p[i].relation = 0;
}
} int Find_set(int x)
{
if(x != p[x].parent)
{
int temp = p[x].parent;
p[x].parent = Find_set(p[x].parent);
p[x].relation = (p[temp].relation+p[x].relation) % 2;
}
return p[x].parent;
} int main()
{
int t,cas = 0;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int i,a,b,sum = 0;
Make_set(n);
for(i = 0; i < m; i++)
{
scanf("%d%d",&a,&b);
int x = Find_set(a);
int y = Find_set(b);
if(x == y)
{
if(1 != ( 2 - p[a].relation + p[b].relation ) % 2)
{
sum++;
}
}
else
{
p[x].parent = y;
p[x].relation = (p[a].relation + 3 - p[b].relation) % 2;
}
}
printf("Scenario #%d:\n",++cas);
if(sum)
{
printf("Suspicious bugs found!\n\n");
}
else
{
printf("No suspicious bugs found!\n\n");
}
}
return 0;
}
A Bug's Life(hdu1829种类并查集)的更多相关文章
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- 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 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
- hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...
- hdu1829 A Bug's Life 基础种类并查集
题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
随机推荐
- poj 2406Power Strings
http://poj.org/problem?id=2406 #include<cstdio> #include<cstring> #include<algorithm& ...
- redis 源码分析
参考: http://redisbook.readthedocs.org/en/latest/index.html http://www.databaseskill.com/3421161/ The ...
- armv8(aarch64)linux内核中flush_dcache_all函数详细分析
/* * __flush_dcache_all() * Flush the wholeD-cache. * Corrupted registers: x0-x7, x9-x11 */ ENTRY( ...
- 【HDOJ】4985 Little Pony and Permutation
水题. #include <cstdio> #define MAXN 100005 int buf[MAXN], n; int main() { int i, j, k; while (s ...
- -_-#【Canvas】回弹
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- //string scriptstrs = "<script>alert('欢迎光临!');</script>";
//string scriptstrs = "<script>alert('欢迎光临!');</script>"; //if (!Page.ClientSc ...
- java常用包
java的核心类都放在java这个包以及其子包下,java扩展的许多类都放在javax包以及其子包下.这些实用类也就是平时经常说的API(应用程序接口). 以下几个是java语言中常用包 java.l ...
- Java笔记6之三目运算符
/* 单目运算符:~3 双目运算符:3 + 4 三目运算符: 格式:比较表达式?表达式1:表达式2; 比较表达式:结果是一个boolean ...
- 组合数学第一发 hdu 2451 Simple Addition Expression
hdu 2451 Simple Addition Expression Problem Description A luxury yacht with 100 passengers on board ...
- Genymotion模拟器一滑动页面就跳到搜索003
今天郁闷的要死,好不容易让Appium关联起Genymotion了,但是一滑动屏幕就跳转到搜索003界面,当时还以为是Appium的Bug或者Genymotion本身出问题了. 结果网上搜了一段时间( ...