POJ2492 A Bug'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 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

题目大意:找同性恋。。。开始把题意解释给磊哥时,我想着这题可能是判定二分图,然后捉摸着用二分染色,磊哥说是并查集,我觉得并查集也行,但是直到比赛结束我们两种方法都没实现出来。。。-_-!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;

#define maxn 4010
#define maxm 1000010

int pre[maxn];

int ufind(int k) {
    int a = k, b;
    while(pre[k] != -1) k = pre[k];
    while(a != k) {
        b = pre[a];
        pre[a] = k;
        a = b;
    }
    return k;
}

bool same(int a, int b) {
    return ufind(a) == ufind(b);
}

void unite(int a, int b) {
    a = ufind(a);
    b = ufind(b);
    if(a != b) pre[a] = b;
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    int T, cas, i, j, N, M, a, b, ok;
    scanf("%d", &T);
    for(cas = 1; cas <= T; ++cas) {
        printf("Scenario #%d:\n", cas);
        scanf("%d%d", &N, &M);
        memset(pre, -1, sizeof(int) * (2 * N + 1));
        ok = 0;
        while(M--) {
            scanf("%d%d", &a, &b);
            if(ok) continue;
            if(same(a, b))
                ok = 1;
            else {
                unite(a, b + N);
                unite(a + N, b);
            }
        }
        printf(ok ? "Suspicious bugs found!" : "No suspicious bugs found!");
        printf("\n\n");
    }
    return 0;
}

POJ2492 A Bug's Life 【并查集】

时间: 2024-10-25 12:42:06

POJ2492 A Bug's Life 【并查集】的相关文章

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

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

poj2492--A Bug&#39;s Life(并查集变形)

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

hdu 1829 A Bug&#39;s Life 并查集系列

1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 using namespace std; 7 8 #define MAXN 2222 9 int fa[MAXN]; 10 int rnk[MAXN]; //秩 表示某点与根的距离 11 int n,

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 (并查集)

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

hdu 1829 A Bug&#39;s Life 并查集

点击打开链接 http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:给出n个关系啊a,b,表示a和b是异性,问是否存在矛盾,即存在a和b是同性. 思路,并查集,用f[i]记录i的父亲,next[i]表示同一个集合里的下一个元素,根据next数组可以遍历出一个集合里的所有元素,len[i]表示i和集合的根的关系,0表示同性,1表示异性. 每输入一个a和b,找到他们的根,如果根相等,那么就判断他们和根的关系,如果和根都是同性关系或者都是异性关系,则表示a

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

1 /* 2 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 3 要求统计交配过程中是否出现冲突,即是否有两个同性的昆虫发生交配. 4 5 思路:并查集 6 将同性的昆虫放入集合之中,如果输入的昆虫u, v同时出现在了集合中,那么 u,v就是同性的!发生了同性交配的可能! 7 */ 8 9 #include <string> 10 #include <cst

A Bug&#39;s Life____并查集

English preparation: falsify     伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀有昆虫的性行为. the number of scenarios 场景数量 consecutively 连续地 趣语: 这个时代,是个雷人的时代.凤姐的出现,让我们对自己的审美观产生了怀疑:著姐的降临,使我们对自己的性取向做出了更正.而如今,bug们被同性恋了,这直接就是商人们为了扩大HS产业的炒作

poj2492--A Bug&amp;#39;s Life(并查集变形)

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