hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829
并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集。本题就是这样,一种虫子有两种性别,每次m次操作,每次给出(a,b),如果a和b是同性别就出现了错误,也就是说出现了判断它有两种性别的错误。我的策略同样是两个集合,用并查集维护两个集合之间的关系。具体证明请看我的上一篇博客,关于这种做法的正确性的证明。
代码如下:
#include<bits/stdc++.h>
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x)
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define mp(a,b) make_pair((a),(b))
#define P pair<int,int>
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x3f3f3f3f
const int maxn=2e6+;
int n,m,t;
inline int read(){
int ans=,w=;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-')w=-;ch=getchar();}
while(isdigit(ch))ans=(ans<<)+(ans<<)+ch-'',ch=getchar();
return ans*w;
}
int f[maxn],rank[maxn];
bool flag;
void init()
{
f(i,,*n)f[i]=i,rank[i]=;
flag=false;
}
int find(int x)
{
if(x==f[x])return x;
return f[x]=find(f[x]);
}
void Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)return;
if(rank[fx]<rank[fy])f[fx]=fy;
else
{
f[fy]=fx;
if(rank[fx]==rank[fy])rank[fx]++;
}
}
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
std::ios::sync_with_stdio(false);
t=read();
int cnt=;
f(tt,,t)
{
n=read(),m=read();
init();
int a,b;
f(i,,m)
{
a=read(),b=read();
if(same(a,b)||same(a+n,b+n))flag=;
else
{
Union(a,b+n);
Union(a+n,b);//将(a,b)分为不同的集合之中
}
}
pf("Scenario #%d:\n",tt);
if(flag)pf("Suspicious bugs found!\n");
else pf("No suspicious bugs found!\n");
pf("\n");
}
}
hdoj 1829 A bug's life 种类并查集的更多相关文章
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- 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 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- 吴裕雄--天生自然 python数据分析:健康指标聚集分析(健康分析)
# This Python 3 environment comes with many helpful analytics libraries installed # It is defined by ...
- vscode写react有warning
[js] Experimental support for decorators is a feature that is subject to change in a future release. ...
- MySQL5.7彻底取消主从复制
由于手误在master节点执行了stop slave;->change master to XXX;->start slave;的动作,后面虽然使用stop slave停止了主从复制,但是 ...
- APP倒闭:你充值的钱会蒸发吗?
有一句说到吐,但却又不得不说的话:资本大潮退去,才知道谁在裸泳.随着资本寒冬的来临,互联网上众多看起来狂飙突进的项目却呈现迅速萎靡态势.尤其是众多具有互联网元素的油卡.洗衣.保洁等成为重灾区,其中不少 ...
- FBI今年最重要的任务:招募黑客
当FBI(联邦调查局)一次又一次被爆出丑闻的时候,面临着一个又一个的尴尬局面.在这样的情况下,FBI发现了自己的一个巨大问题,那就是以前都依靠隐秘行动和人员的保密性来保证国家的安全,现在必须依靠更 ...
- C++走向远洋——67(项目二、洗牌)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- clientWidth offsetWidth等视窗尺寸
clientWidth和offsetWidth clientWidth 是一个只读属性,返回元素的内部宽度,该属性包括内边距,但不包括垂直滚动条(如果有).边框和外边距. offsetWidth 是一 ...
- 适配iphoneX
tips iphone6设备宽高为375×667,屏幕分辨率为750×1334,故其设备像素比(dpr)为2.iphoneX的设备宽高375*812,屏幕分辨率为1125x2436,故dpr=3 适配 ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 《一步步成为 Hacker_Day 01》
环境搭建 传统运行模式 - 一台机器同时只能运行一个操作系统 |:----------|----------:| | 应用程序 | 应用程序 | |:----------|----------:| | ...