*HDU1829 并查集

A Bug‘s Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14121    Accepted Submission(s): 4603

Problem Description

Background
Professor
Hopper is researching the sexual behavior of a rare species of bugs. He
assumes that they feature two different genders and that they only
interact with bugs of the opposite gender. In his experiment, individual
bugs and their interactions were easy to identify, because numbers were
printed on their backs.

Problem
Given a list of bug
interactions, decide whether the experiment supports his assumption of
two genders with no homosexual bugs or if it contains some bug
interactions that falsify it.

Input

The
first line of the input contains the number of scenarios. Each scenario
starts with one line giving the number of bugs (at least one, and up to
2000) and the number of interactions (up to 1000000) separated by a
single space. In the following lines, each interaction is given in the
form of two distinct bug numbers separated by a single space. Bugs are
numbered consecutively starting from one.

Output

The
output for every scenario is a line containing "Scenario #i:", where i
is the number of the scenario starting at 1, followed by one line saying
either "No suspicious bugs found!" if the experiment is consistent with
his assumption about the bugs‘ sexual behavior, or "Suspicious bugs
found!" if Professor Hopper‘s assumption is definitely wrong.

Sample Input

2

3 3

1 2

2 3

1 3

4 2

1 2

3 4

Sample Output

Scenario #1:

Suspicious bugs found!

Scenario #2:

No suspicious bugs found!

题意:

有若干bugs,一对一对的给出,判断其中是否有同性恋,如果1与2,2与3,1又与3,就说明1和3是gay.

代码:

 1 //将相同性别的bugs放进同一并查集里,这样只要输入的两个bug有相同的根就是gay.
 2 //路径压缩的while 的find比递归的find稍快一些。
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstring>
 6 using namespace std;
 7 int t,n,m;
 8 int fat[2005],sex[2005];
 9 int find(int x)
10 {
11     int rt=x;
12     while(fat[rt]!=rt)
13     rt=fat[rt];
14     int i=x,j;
15     while(i!=rt)
16     {
17         j=fat[i];
18         fat[i]=rt;
19         i=j;
20     }
21     return rt;
22 }
23 /*int find(int x)
24 {
25     if(fat[x]!=x)
26     fat[x]=find(fat[x]);
27     return fat[x];
28 }*/
29 void connect(int x,int y)
30 {
31     int xx=find(x),yy=find(y);
32     if(xx!=yy)
33     fat[xx]=yy;
34 }
35 int main()
36 {
37     int a,b;
38     scanf("%d",&t);
39     for(int k=1;k<=t;k++)
40     {
41         scanf("%d%d",&n,&m);
42         for(int i=0;i<=n;i++)
43         {
44             fat[i]=i;
45             sex[i]=0;
46         }
47         bool flag=0;
48         for(int i=1;i<=m;i++)
49         {
50             scanf("%d%d",&a,&b);
51             if(flag) continue;
52             if(find(a)==find(b))
53             {
54                 flag=1;
55                 continue;
56             }
57             if(sex[a]==0) sex[a]=b;
58             else connect(sex[a],b);
59             if(sex[b]==0) sex[b]=a;
60             else connect(sex[b],a);
61         }
62         printf("Scenario #%d:\n",k);
63         if(flag) printf("Suspicious bugs found!\n");
64         else printf("No suspicious bugs found!\n");
65         printf("\n");
66     }
67     return 0;
68 }
时间: 2024-08-22 01:05:56

*HDU1829 并查集的相关文章

HDU1829 - A Bug&#39;s Life 分组并查集

HDU1829 - A Bug's Life : http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:首先输入T,接下来是T组测试数据.每组测试数据第一行输入N,M代表共有N个人,接下来有M行输入.每行有两个数a和b,代表编号为a和b的两个人是异性.输入结束后判断是否存在错误的信息.如N = 3,M = 3,接下来是3组输入.1  2,2  3,1  3.此组数据是错的,因为1 和 3都和2是异性,说明1 和 3是同性,但第三组数据又说1和3是异性

hdu1829 A Bug&#39;s Life(并查集)

开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,如果不冲突就进行合并,否则不需要继续合并. #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int MAX=2000; int pre[2*MAX+5]; bool mark; void init(int n){ int i; for(i=1

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&

hdu1829 A Bug&#39;s Life (并查集)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature t

HDU1829(种类并查集)

ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender.

hdu 3038 How Many Answers Are Wrong(种类并查集)

了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了. 现在回过头来看,又看了一遍. 题意—— 输入—— 给出多组测试数据. 每组数据第一行包含两个整数n, m.n表示共有1——n这么多个数,m表示m组提示. 接下来m行,每行包含三个整数a, b, val.表示从a到b这几个数的和为val. 这几组数有可能有冲突,问一共有多少组有冲突的数据. 输出—

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls