POJ2492(并查集)

A Bug‘s Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28882   Accepted: 9423

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

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.

解题思路:

题目大意抽象为n个点m个操作,每次操作两个对象a和b,代表a和b可以进行交配,那么根据professer的假设,a和b一定是异性。如果是同性的话,那么就会出现同性恋,则professer的假设不成立,输出Suspicious bugs found!;成立输出No suspicious bugs found!。

并查集的模板题,不过与以往做的不同的是,要开一个rea数组记录a和b之间的关系。rea[ i ]存储 i 的异性,具体见代码。

最后注意下每次后面都要有一个空行,描述里没说,但是样例里是那么显示的。

完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int maxn = 1100000;
int n , m;

int f[maxn];
int rea[maxn];

void init()
{
    for(int i = 0 ; i < maxn ; i++)
    {
        f[i] = i;
        rea[i] = 0;
    }

}

int find(int x)
{
    return x == f[x] ? x : find(f[x]);
}

void unin(int a , int b)
{
    int x = find(a);
    int y = find(b);
    if(x != y)
        f[x] = y;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int T;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int a , b;
        int flag = 0;
        for(int i = 0 ; i < m ; i ++)
        {
            scanf("%d%d" , &a , &b);
            int x = find(a);
            int y = find(b);
            if(x == y)
                flag = 1;
            if(rea[x] == 0 && rea[y] == 0) // x和y没有建立性别关系
            {
                rea[x] = y;  // x的异性是y
                rea[y] = x;   //  y的异性是x
            }
            else if(rea[x] == 0)  // y建立起了性别关系,x没建立
            {
                rea[x] = y;   //   x的异性是y
                unin(x , rea[y]);  //   根据假设,那么x一定应该和y的原有异性进行合并,下面同理
            }
            else if(rea[y] == 0)
            {
                rea[y] = x;
                unin(y , rea[x]);
            }
            else
            {
                unin(x , rea[y]);
                unin(y , rea[x]);
            }
        }
        printf("Scenario #%d:\n" , cas++);
        if(flag)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
        printf("\n");
    }
}
时间: 2024-08-26 07:20:03

POJ2492(并查集)的相关文章

hdu-1829 &amp; poj-2492 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1829 #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> #include <set> #include <

POJ2492 A Bug&#39;s Life 【并查集】

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

poj2492 A Bug&#39;s Life【带权并查集】

题目链接:http://poj.org/problem?id=2492 题目描述:找基佬游戏(汗-_-b)有个hentai科学家研究虫子种群,不断地给出二元组xy,表示x和y是异性交往,但是可能会出现矛盾(找到基佬),比如1与2是异性恋,2与3是异性恋,却又告诉你1和3是异性恋.问种群中存不存在基佬败类 思路:与poj1182“食物链”几乎一样,还简单一点,毕竟只有两类物品.par[i]表示父节点,d[i]表示偏移量,0为同性,1为异性.不过要注意的一点是所有合并的过程要对二取模,比如x找到根结

poj2492 A Bug&#39;s Life (并查集拓展)

C - A Bug's Life Crawling in process... Crawling failed Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: CSUST_11 (2013-04-14) System Crawler (2016-05-13) Description B

带权并查集 POJ1988 POJ2492

单纯的并查集很简单,带权并查集还能解决更多的问题,才更好玩,来个题热身.对于下面的知识,现在就当你已经熟练掌握了递归和并查集的路径压缩. POJ1988:题目链接 http://poj.org/problem?id=1988 题目大意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作:  M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上.  C x : 问方块x下面有多少个方块. 操作最多有 P (P<=100,000)次.对每次C操作

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

本文出自:http://blog.csdn.net/svitter 题意: 给出昆虫编号,看昆虫能否交配,如果出现同性交配或者自我交配的情况,则出现BUG. 输入输出分析: 1.输入输出数据: input: 2 3 3 1 2 2 3 1 3 4 2 1 2 3 4 output: Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found! 第一行给出的是测试数据的个数,随后跟着n, m.n是昆虫个数,m是

POJ-2492.A Bug&#39;s Life(带权并查集)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 48043   Accepted: 15483 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(种类并查集)

http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令是否符合逻辑 两种写法: 第一种:带权并查集 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h&g

poj2492(种类并查集)

题目链接: http://poj.org/problem?id=2492 题意: 有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y, x, y表示教授判断x, y为异性, 问教授是否有错误判断,即存在x, y为同性; 这道题和poj1703类似, 不过更简单一点 (poj1703题解) 解法1: 我们可以用rank[x]记录x与其父亲节点的关系, rank[x]=0表同性, rank[x]=1表异性; 假设前面的教授判断都是正确的, 若后面存在与前面判断矛盾的