首页 > 其他 > 详细

POJ 2492 || HDU 1829:A Bug's Life(并查集)

时间:2014-07-15 10:46:34      阅读:346      评论:0      收藏:0      [点我收藏+]

传送门:

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

好吧。。这道题。。思路不是自己的。。Orz。。

带关系的并查集。。

题意:有n个人,给你m对关系,问有没有同性恋的。

本题需要做的是统计输入的数据是否有相同的根节点,有的话就是违法的,结果也就出来了,没有相同根节点的话,得分别处理


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>

using namespace std;

#define f1(i, n) for(int i=0; i<n; i++)
#define f2(i, n) for(int i=1; i<=n; i++)
#define f3(i, n) for(int i=n; i>=1; i--)
#define f4(i, n) for(int i=1; i<n; i++)
#define M 10005

int f[M];
int sex[M];
int flag;

int find (int x)  //并查集的find
{
    return f[x] == x ? x : f[x] = find(f[x]);
}

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

int main()
{
    int t;
    int n, m;
    int a, b;
    scanf("%d", &t);
    f2(cas, t)
    {
        flag = 0;
        memset(sex, 0, sizeof(sex));
        scanf("%d%d", &n, &m);
        f2(i, n)
        f[i] = i;  //初始化, 因为题目数据, 所以是从1开始。
        f1(i, m)
        {
            scanf("%d%d", &a, &b);
            if(flag)
                continue;
            if(find(a) == find(b))
            {
                flag = 1;
                continue;
            }
            if(sex[a]==0)
                sex[a] = b;   //说明sex[i]与i是异性
            else
                make(sex[a], b); //与a是异性,那么与b就是同性。。合并
            if(sex[b]==0)
                sex[b] = a;
            else
                make(sex[b], a);
        }
        printf("Scenario #%d:\n", cas);
        if(flag)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
        printf("\n");
    }

    return 0;
}






POJ 2492 || HDU 1829:A Bug's Life(并查集),布布扣,bubuko.com

POJ 2492 || HDU 1829:A Bug's Life(并查集)

原文:http://blog.csdn.net/u013487051/article/details/37771783

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!