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 ...
随机推荐
- 转:使用XHProf优化PHP程序
原文来自于:http://blog.sina.com.cn/s/blog_665fc8980100l8dq.html XHProf 是 FaceBook 开发的一个函数级别的 PHP 分层分析器. 数 ...
- 实现一个基于tcc/tlink的简单的编译链接工具
一.基础研究 在这里我们需要提供一套新的c语言开发工具cc,它支持的c程序不是从main开始运行而是从CMain开始运行. 书上已经对该工具程序进行了需求分析:(1)要在屏幕中间显示彩色的字符串:(2 ...
- jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件
写词典在线编辑器用到的一个功能 能获取选中的文字.当前的光标的位置 等位置,而且支持多个文本框一起操作 非常方便 git地址:https://github.com/localhost/jquery-f ...
- 机器学习公开课~~~~mooc
https://class.coursera.org/ntumlone-001/class/index
- gnu cc扩展和ABI
gnc cc扩展标准c的语法,非常强大!!!详情请见: http://uw714doc.sco.com/cgi-bin/info2html?%28gcc.info%29C%2520Extensions ...
- Common Subsequence
#include<cstdio> #include<iostream> #include<cmath> #include<string> #includ ...
- Radar Installation 贪心
Language: Default Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42 ...
- 【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1033 http://poj.org/problem?id=1141 ZOJ目前挂了. ...
- 转:ORM框架
转自 程序员成长之路:http://blog.csdn.net/zxc22436/article/details/6875220 对象关系映射(ORM)提供了概念性的.易于理解的模型化数据的方法.OR ...
- Python自动化之Django的CSRF
什么CSRF? CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到你的网站上了,那么当这个 ...