hdu1829A Bug's Life(种类并查集)

传送门

关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的)。一旦遇到性别一样的,就说明找到了可疑的

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int f[2005],n,m,rankk[2005];
 4 bool flag;
 5 inline void init()
 6 {
 7     flag=false;
 8     for(int i=0; i<=n; ++i)
 9         f[i]=i, rankk[i]=0;
10 }
11 int getf(int v)
12 {
13     if(v==f[v])return f[v];
14     int t=getf(f[v]);
15     rankk[v] = (rankk[f[v]]+rankk[v])&1;//到根节点的距离
16     f[v]=t;
17     return f[v];
18 }
19 void merge(int v, int u)
20 {
21     int t1=getf(v), t2=getf(u);
22     if(t1==t2)
23     {
24         if(rankk[v]==rankk[u])//到根节点的距离相等,就表示他们的性别相同
25             flag=true;
26         return;
27     }
28     f[t1]=t2;//if(t1!=t2)
29     rankk[t1] = (rankk[v]+rankk[u]+1)&1;
30 }
31 int main()
32 {
33     int t;
34     scanf("%d",&t);
35     int cases=1;
36     while(t--)
37     {
38         scanf("%d%d",&n,&m);
39         init();
40         for(int i=0; i<m; i++)
41         {
42             int a,b;
43             scanf("%d%d",&a,&b);
44             if(flag)continue;
45             merge(a,b);
46         }
47         printf("Scenario #%d:\n",cases++);
48         if(flag)printf("Suspicious bugs found!\n");
49         else printf("No suspicious bugs found!\n");
50         printf("\n");
51     }
52
53     return 0;
54 }

hdu1829A Bug's Life(种类并查集)

原文地址:https://www.cnblogs.com/fqfzs/p/9998197.html

时间: 2024-08-08 03:05:29

hdu1829A Bug's Life(种类并查集)的相关文章

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

【POJ】2492 A bug&#39;s life ——种类并查集

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

hdoj 1829 A bug&#39;s life 种类并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有两种性别,每次m次操作,每次给出(a,b),如果a和b是同性别就出现了错误,也就是说出现了判断它有两种性别的错误.我的策略同样是两个集合,用并查集维护两个集合之间的关系.具体证明请看我的上一篇博客,关于这种做法的正确性的证明. 代码如下: 1 #include<bits/stdc++.h> 2 t

poj 2492 a bug&#39;s life 简单种类并查集

题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. 1 #include<cstdio> 2 using namespace std; 3 const int MAXN=50010; 4 int fa[MAXN]; 5 int rel[MAXN]; // 0代表同类,1代表吃fa[i],2代表被吃 6 void _set(int n) 7 { 8 for(int i=1;i&l

hdoj-1289-A Bug&amp;#39;s Life【种类并查集】

A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11386 Accepted Submission(s): 3709 Problem Description Background Professor Hopper is researching the sexual behavior of a rare specie

hdu1829 A Bug&#39;s Life 基础种类并查集

题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合,异性的异性为同性. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2010; int f[N], r[N], flag; void init() { for(int i=1;i&

hdu 1829 A Bug&#39;s Life (基础种类并查集)

先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所

A Bug&#39;s Life(种类并查集)(也是可以用dfs做)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1829 Description Background Professor Hopper is researching the sexual behavior of a r

hdoj-1289-A Bug&#39;s Life【种类并查集】

A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11386 Accepted Submission(s): 3709 Problem Description Background Professor Hopper is researching the sexual behavior of a rare specie