POJ 2492 A Bug's Life

A Bug‘s Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 35756   Accepted: 11730

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!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany

题目链接:http://poj.org/problem?id=2492

下面给出AC代码:(分析都在代码里,每段代码给出了注释)

 1 /*关于并查集,注意两个概念:按秩合并、路径压缩。
 2 1、按秩合并
 3 由于并查集一般是用比较高效的树形结构来表示的,按秩合并的目的就是防止产生退化的树(也就是类似链表的树),
 4 用一个数组记录各个元素的高度(也有的记录各个元素的孩子的数目,具体看哪种能给解题带来方便),
 5 然后在合并的时候把高度小的嫁接到高度大的上面,从而防止产生退化的树。
 6 2、路径压缩
 7 而另一个数组记录各个元素的祖先,这样就防止一步步地递归查找父亲从而损失的时间。因为并查集只要搞清楚各个元素所在的集合,
 8 而区分不同的集合我们用的是代表元素(也就是树根),所以对于每个元素我们只需保存其祖先,从而区分不同的集合。
 9 而我们这道题并没有使用纯正的并查集算法,而是对其进行了扩展,
10 我们并没有使用“1、按秩合并”(当然你可以用,那样就需要再开一个数组)
11 我们从“1、按秩合并”得到启示,保存“秩”的数组保存的是    元素相对于父节点的关系  ,我们岂不可以利用这种关系
12 (即相对于父节点的不同秩值来区分不同的集合),从而可以把两个集合合并成一个集合。
13 (注:此代码 relation=0 代表 和父节点同一性别)
14 */
15
16
17 #include<stdio.h>
18 int father[2005];
19 int relation[2005];
20
21 int find_father(int i)
22 {
23     int t;
24     if(father[i]==i)
25         return i;
26
27     //计算相对于新的父节点(即根)的秩,relation[t]是老的父节点相对于新的父节点(即根)的秩,relation[i]是i元素相对于老的父节点的秩,
28     //类似于物理里的相对运动,得到的r[i]就是相对于新的父节点(即根)的秩。而且这个递归调用不会超过两层
29     t=father[i];
30     father[i]=find_father(father[i]);
31     relation[i]=(relation[i]+relation[t]+1)%2;   //注意递归中把这棵树relation中的的值都更新一遍,这句的顺序 不能 和上一句 调换位置
32     // relation[a]的改变是伴随着father[a]的改变而更新的(有father改变就有relation改变),要是father改变了,而relation未改变,此时的relation就记录了一个错误的值,
33     //father未改变(即使实际的father已不是现在的值,但只要father未改变,relation的值就是“正确”的,认识到这点很重要。)
34     return father[i];
35 }
36
37 void merge(int a,int b)
38 {
39     int x,y;
40     x=find_father(a);
41     y=find_father(b);
42     father[x]=y;
43     relation[x]=(relation[b]-relation[a])%2;//relation[a]+relation[x]与relation[b]相对于新的父节点必须相差1个等级,因为他们不是gay
44 }                                            //x下边的节点不用改,因为查找的时候会自动更新
45
46 int main()
47 {
48     int T,m,n,i,j,a,b,flag;
49     scanf("%d",&T);
50     for(i=1;i<=T;++i)
51     {
52         flag=0;
53         scanf("%d%d",&n,&m);
54         for(j=1;j<=n;++j)       //初始化
55         {
56             father[j]=j;
57             relation[j]=1;
58         }
59         for(j=1;j<=m;++j)
60         {
61             scanf("%d%d",&a,&b);
62             if(find_father(a)==find_father(b))
63             {
64             //    if(relation[a]!=(relation[b]+1)%2)
65                 if(relation[a]==relation[b])            //说明是同性
66                     flag=1;
67             }
68             else
69                 merge(a,b);
70         }
71         if(flag)
72             printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
73         else
74             printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
75     }
76     return 0;
77 }

POJ 2492 A Bug's Life

时间: 2024-10-20 04:23:05

POJ 2492 A Bug's Life的相关文章

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

【转】POJ 2492 A Bug&#39;s Life:基础并查集进阶

思路参考这里(较详细) 一开始总是WA调了一晚上原来···Init初始化写在scanf上面了···哎╮(╯▽╰)╭anyway!对并查集的理解更深了一步! #include<cstdio> #include<cstring> using namespace std; #define Size 2000 struct node { int Pre; int Relation;// 与父节点的关系 0同性 1异性 }Bug[Size+1]; int N, M; bool found;

(简单) POJ 2492 A Bug&#39;s Life,二分染色。

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

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: 30130   Accepted: 9869 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

POJ 2492 A Bug&#39;s Life (带权并查集 &amp;&amp; 向量偏移)

题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配关系. 分析 : 本题雷同POJ 1182 食物链, 如果会了那一题, 那现在这题便简单多了, 建议先了解食物链的偏移向量做法.这里也是使用向量的思考方式来进行relation的变化, 这里我令 relation = 0为同性, relation = 1为异性, 接下来的步骤就和食物链的雷同了. 优

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

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 in

hdu 1829 &amp;amp;poj 2492 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): 8528    Accepted Submission(s): 2745 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare

poj 2492 A Bug&#39;s Life 【并查集拓展】

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