poj 2492

A Bug‘s Life

POJ - 2492

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!
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=2000+100;
17 int flag[N];
18 int parent[N];
19 int m,n;
20 void init(){
21     for(int i=0;i<=n;i++)parent[i]=i;
22     memset(flag,0,sizeof(flag));
23 }
24 int find(int x){
25     if(parent[x]!=x){
26         int root=parent[x];
27         parent[x]=find(parent[x]);
28         flag[x]=(flag[x]+flag[root])%2;
29     }
30     return parent[x];
31 }
32 /*
33 int find(int x){
34     int r=x;
35     while(r!=parent[r])r=parent[r];
36     int i=x;
37     int j;
38     while(i!=r){
39         flag[i]=(flag[i]+flag[parent[i]])%2;
40         j=parent[i];
41         parent[i]=r;
42         i=j;
43     }
44     return r;
45 }*/
46 void Union(int x,int y){
47     int xx=find(x);
48     int yy=find(y);
49     parent[xx]=yy;
50     flag[xx]=(flag[y]+flag[x]+1)%2;
51 }
52 int main()
53 {
54     //freopen("text.txt","r",stdin);
55     int T,kase;
56     scanf("%d",&T);
57     for(int kase=1;kase<=T;kase++)
58     {
59         scanf("%d%d",&n,&m);
60         init();
61         int flag1=0;
62         for(int i=0;i<m;i++)
63         {
64             int x,y;
65             scanf("%d%d",&x,&y);
66             if(flag1)
67                 continue;
68             int fx=find(x);
69             int fy=find(y);
70             if(fx==fy)
71             {
72                 if(flag[x]==flag[y])
73                     flag1=1;
74             }
75             else
76                 Union(x,y);
77         }
78         printf("Scenario #%d:\n",kase);
79         if(flag1)
80             printf("Suspicious bugs found!\n");
81         else
82             printf("No suspicious bugs found!\n");
83         if(kase!=T)
84             printf("\n");
85     }
86     return 0;
87 }
时间: 2024-08-05 15:01:57

poj 2492的相关文章

POJ 2492 A Bug&#39;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 gender

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

题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些 sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1 每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2; 还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有 paren

【转】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 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

POJ 2492 并查集扩展(判断同性恋问题)

G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: Description BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. H

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