POJ_2492 A Bug's Life 【并查集】
一、题面
二、分析
并查集判断类别的题目感觉套路都差不多。
还是先判断在不在一个集合里,在一个集合里才能判断是否同类。
若不在一个集合里则需要将这两个点联系起来。
关于联系起来后关系的变化,画几个图后发现还是异或。
为什么用0表示同类,1表示异类?画几个图发现这样表示关系变换最简单。
三、AC代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <fstream> using namespace std; const int MAXN = 2e3+;
int Par[MAXN];
bool Rank[MAXN]; void Init()
{
memset(Par, -, sizeof(Par));
memset(Rank, , sizeof(Rank));
} int Find(int x)
{
if(Par[x] == -)
return x;
int t = Par[x];
Par[x] = Find(Par[x]);
Rank[x] = Rank[t]^Rank[x];
return Par[x];
} int main()
{
//freopen("input.txt", "r", stdin);
int T, N, M;
scanf("%d", &T);
for(int Cnt = ; Cnt <= T; Cnt++)
{
if(Cnt!=)
printf("\n");
Init();
int x, y, fx, fy;
bool ans = true;
scanf("%d %d", &N, &M);
for(int i = ; i < M; i++)
{
scanf("%d %d", &x, &y);
if(ans)
{
fx = Find(x);
fy = Find(y);
if(fx == fy && Rank[x] == Rank[y])
{
ans = false;
}
else if(fx != fy)
{
Par[fx] = fy;
Rank[fx] = Rank[x]^(Rank[y]^);
}
}
}
if(ans)
{
printf("Scenario #%d:\nNo suspicious bugs found!\n", Cnt);
}
else
{
printf("Scenario #%d:\nSuspicious bugs found!\n", Cnt);
}
}
return ;
}
POJ_2492 A Bug's Life 【并查集】的更多相关文章
- hdu 1829 A Bug's Life(并查集)
A Bu ...
- J - A Bug's Life 并查集
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Background Professor Hopper is researching th ...
- poj 2492A Bug's Life(并查集)
/* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 2492 A Bug's Life 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- poj2492_A Bug's Life_并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34947 Accepted: 11459 D ...
- [poj2492]A Bug's Life(并查集+补集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34678 Accepted: 11339 D ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- A Bug's Life____并查集
English preparation: falsify 伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀 ...
随机推荐
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- 661. Image Smoother色阶中和器
[抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...
- NanoPi2
https://item.taobao.com/item.htm?spm=a230r.1.14.9.Ijhc8S&id=526981593477&ns=1&abbucket=1 ...
- 41、OrthoMCL和mcl软件进行基因家族分析
转载:http://www.realbio.cn/news/124.html https://blog.csdn.net/seallama/article/details/43820763 http: ...
- nmap 笔记
本文由阿德马翻译自国外网站,请尊重劳动成果,转载请注明出处,谢谢 1.初级用法: 教程 Nmap使用不同的技术来执行扫描,包括:TCP的connect()扫描,TCP反向的ident扫描,FTP反 ...
- java IO Nio 文件拷贝工具类Files
public static void main(String[] args) throws Exception { Files.copy(Paths.get("file/text.txt&q ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- MongoDB整理笔记の索引
MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引是凌驾于数 ...
- MongoDB整理笔记の走进MongoDB世界
本人学习mongodb时间不长,但是鉴于工作的需要以及未来发展的趋势,本人想更深层的认识mongodb底层的原理以及更灵活的应用mongodb,边学边工作实践. mongodb属于nosql中算是最 ...
- ComicEnhancerPro 系列教程十七:二值化图像去毛刺
作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十七:二值化图像去毛 ...