POJ 2492 A Bug'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 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 <string>
  4 #include <queue>
  5 #include <vector>
  6 #include <map>
  7 #include <algorithm>
  8 #include <cstring>
  9 #include <cctype>
 10 #include <cstdlib>
 11 #include <cmath>
 12 #include <ctime>
 13 using    namespace    std;
 14
 15 const    int    SIZE = 2005;
 16 int    FATHER[SIZE],MARK[SIZE],RANK[SIZE];
 17
 18 void    ini(int);
 19 int    get_father(int);
 20 void    unite(int,int);
 21 bool    same(int,int);
 22
 23 int    main(void)
 24 {
 25     int    t,n,m,x,y,count = 0;
 26     bool    flag;
 27
 28     scanf("%d",&t);
 29     while(t --)
 30     {
 31         count ++;
 32         scanf("%d%d",&n,&m);
 33         ini(n);
 34         flag = false;
 35         while(m --)
 36         {
 37             scanf("%d%d",&x,&y);
 38             if(flag)
 39                 continue;
 40             if(same(x,y))
 41                 flag = true;
 42             if(!MARK[x] && !MARK[y])
 43             {
 44                 MARK[x] = y;
 45                 MARK[y] = x;
 46             }
 47             else    if(!MARK[x])
 48             {
 49                 MARK[x] = y;
 50                 unite(x,MARK[y]);
 51             }
 52             else    if(!MARK[y])
 53             {
 54                 MARK[y] = x;
 55                 unite(y,MARK[x]);
 56             }
 57             else
 58             {
 59                 unite(x,MARK[y]);
 60                 unite(y,MARK[x]);
 61             }
 62         }
 63         printf("Scenario #%d:\n",count);
 64         if(flag)
 65             puts("Suspicious bugs found!");
 66         else
 67             puts("No suspicious bugs found!");
 68         puts("");
 69     }
 70
 71
 72     return    0;
 73 }
 74
 75 void    ini(int n)
 76 {
 77     for(int i = 1;i <= n;i ++)
 78     {
 79         MARK[i] = RANK[i] = 0;
 80         FATHER[i] = i;
 81     }
 82 }
 83
 84 int    get_father(int n)
 85 {
 86     if(n == FATHER[n])
 87         return    n;
 88     return    FATHER[n] = get_father(FATHER[n]);
 89 }
 90
 91 void    unite(int x,int y)
 92 {
 93     x = get_father(x);
 94     y = get_father(y);
 95
 96     if(x == y)
 97         return    ;
 98     if(RANK[x] < RANK[y])
 99         FATHER[x] = y;
100     else
101     {
102         FATHER[y] = x;
103         if(RANK[x] == RANK[y])
104             RANK[x] ++;
105     }
106 }
107
108 bool    same(int x,int y)
109 {
110     return    get_father(x) == get_father(y);
111 }

POJ 2492 A Bug's Life (并查集)

时间: 2024-12-25 22:25:10

POJ 2492 A Bug's Life (并查集)的相关文章

POJ 2492(权值向量并查集)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 49835   Accepted: 16094 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:基础并查集进阶

思路参考这里(较详细) 一开始总是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;

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 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