题意:有一群虫子,现在给你一些关系,判断这些关心有没有错

思路:向量种类并查集,下面讲一下向量的种类并查集

本题的各个集合的关心有两种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种类并查集)的更多相关文章

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

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

  2. 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 ...

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

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

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

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

  5. POJ-2492 A Bug's Life(种类并查集)

    http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 转:Mysql在大型网站的应用架构演变

    原文来自于:http://www.cnblogs.com/Creator/p/3776110.html 原创文章,转载请注明: 转载自http://www.cnblogs.com/Creator/本文 ...

  2. codemirror 插件

    做在线词典编辑的时候.里面有些自定义标签.类似html标签一样. 为了让编辑编辑.改成了  <动词></动词> 所以引用了 codemirror插件 此插件绝对牛逼 它主要功能 ...

  3. 定义一个栈的数据结构,要求实现一个min函数,每次能够得到栈的最小值,并且要求Min的时间复杂度为O(1)

    具体实现代码如下: stack.h内容如下: #ifndef _STACK_H_ #define _STACK_H_ #define NUM 256 typedef struct _tagStack ...

  4. 解读sample5

    说明 被测试代码文件 sample1.h.sample1.cc和sample3-inl.h 测试代码文件 sample5_unittest.cc 官网上如是描述sample5: Sample #5 i ...

  5. spring中@value注解需要注意

    首先,@value需要参数,这里参数可以是两种形式:@Value("#{configProperties['t1.msgname']}")或者@Value("${t1.m ...

  6. Heritrix的安装与配置 (最新版 已测试通过)

    本教程,结合本人亲身实践,不仅适合于最新版本Heritrix 1.14.4,更适合其他任何版本.Heritrix具体下载地址如下:      http://sourceforge.net/projec ...

  7. Android Framework 记录之二

    接着上次的记录,续写. 23.services文件夹 文件 描写叙述 class AlarmManagerService extends IAlarmManager.Stub { //定时管理服务 p ...

  8. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. Laravel 4 Quick Tip: Custom Error Pages

    App::error(function($exception, $code) { switch ($code) { case 403: return Response::view('errors.40 ...

  10. jpeg和gif已经影响互联网发展进程了,他们应该被历史淘汰了!!!

    本人发现.传统的图片格式已经不适应互联网时代了!!!,故本人发起定义一种新的图片格式.后缀名为 .gnet 互联网上的图片大多有这几种来源.微博上传,视频截图,网络编辑人上传等,以眼下的技术.这些图片 ...