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. 一种稀 ...
随机推荐
- Tensorflow学习(练习)—CPU训练模型
Mask R-CNN - Train on Shapes Dataset This notebook shows how to train Mask R-CNN on your own dataset ...
- 面试题:jsp九大内置对象
我们常说的JSP有九大内置对象分别为:request.response.session.out.pagecontext.page.exception.application.config. 我们知道, ...
- Win10 DHCP和Static IP 切换
创建两个.bat文件,分别命名为static.bat和dhcp.bat static.bat文件写入 netsh interface ip set address "Wi-Fi" ...
- Win10 linux子系统Ubuntu下显示图形界面
转载 https://jingyan.baidu.com/article/ed2a5d1f98577809f6be17a3.html 打开终端界面,在这个窗口测试一下ls命令,无误. # 更新 sud ...
- Luogu 4198 楼房重建
BZOJ 2957 挺妙的题. 先把题目中的要求转化为斜率,一个点$(x, y)$可以看成$\frac{y}{x}$,这样子我们要求的就变成了一个区间内一定包含第一个值的最长上升序列. 然后把这个序列 ...
- osm2pgsql windows “illegal option -W” error
新版本不支持 解决: 修改pg_hba.conf的METHOD为trust 参考:http://stackoverflow.com/questions/15510428/osm2pgsql-windo ...
- Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications
January 2014 Containing twenty-four design patterns and ten related guidance topics, this guide arti ...
- URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)
题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...
- WordPaster2-正式包布署说明
1.1. 多平台布署说明 提供信息如下: 1.多平台控件包(根据购买版本提供) 2.控件信息 3.配置方法 WordPaster(x86)Clsid信息 ClsidParser F4B7C0FD- ...
- Android Studio2.3更换默认的ConstraintLayout布局
1.在as安装目录\plugins\Android\lib\templates\activities\common\root\res\layout下,找到simple.xml.ftl文件 2.用以下布 ...