POJ 2492 A Bug's Life

http://poj.org/problem?id=2492

J - A Bug's Life
Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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.
 
题目大意:m只虫子,两两结合,根据所给的n组结合情况,判断是否有同性恋的情况
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 2010

int f[N], r[N];

int Find(int x)
{
    if(x != f[x])
        f[x] = Find(f[x]);
    return f[x];
}

void Join(int x, int y)
{
    int a = Find(x), b = Find(y);
    if(a != b)
        f[a] = b;
}//合并

int main()
{
    int t, i, m, n, x, y, rx, ry, flag, p = 0;
    scanf("%d", &t);
    while(t--)
    {
        flag = 0;
        p++;
        scanf("%d%d", &m, &n);
        for(i = 1 ; i<= m ; i++)
        {
            f[i] = i;
            r[i] = 0;//0为同性1为异性
        }
        while(n--)
        {
            scanf("%d%d", &x, &y);
            rx = Find(x);
            ry = Find(y);
            if(rx == ry)//x和y在同一个集合里,即x,y为同性
                flag = 1;
            if(!r[x] && !r[y])//x,y之前没有结合过
            {
                r[x] = y;//x与y结合
                r[y] = x;//y与x结合
            }//即x,y互为异性
            else if(!r[x] && r[y])//x之前没有结合过, y之前结合过
            {
                r[x] = y;//x与y结合
                Join(r[y], x);//x与y的结合者(即父节点)互为同性,则将y的父节点与x合并
            }
            else if(r[x] && !r[y])//y之前没有结合过, x之前结合过
            {
                r[y] = x;//y与x结合
                Join(r[x], y);//y与x的结合者(即父节点)互为同性,则将x的父节点与y合并
            }
            else//x,y之前都结合过
            {
                Join(r[x], y);//y与x的结合者(即父节点)互为同性,则将x的父节点与y合并
                Join(r[y], x);//x与y的结合者(即父节点)互为同性,则将y的父节点与x合并
            }
        }
        printf("Scenario #%d:\n", p);
        if(flag)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}

 另一种方法,物种种类,与poj1182食物链相似,利用向量加法

 

    r[x] + 1 - r[y]
rx-------------------》ry
                                 
 ↑ r[x]                       ↑r[y]
               1
 x-------------------》y
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 2010

int f[N], r[N];

int Find(int x)
{
    int k = f[x];
    if(x != f[x])
    {
        f[x] = Find(f[x]);
        r[x] = (r[x] + r[k]) % 2;
    }
    return f[x];
}

int main()
{
    int t, i, m, n, x, y, rx, ry, flag, p = 0;
    scanf("%d", &t);
    while(t--)
    {
        flag = 0;
        p++;
        scanf("%d%d", &m, &n);
        for(i = 1 ; i<= m ; i++)
        {
            f[i] = i;
            r[i] = 0;//0为同性1为异性
        }
        while(n--)
        {
            scanf("%d%d", &x, &y);
            rx = Find(x);
            ry = Find(y);
            if(rx != ry)
            {
                f[rx] = ry;
                r[rx] = (1 + r[y] - r[x]) % 2;
            }
            else
            {
                if(r[x] == r[y])
                    flag = 1;
            }
        }
        printf("Scenario #%d:\n", p);
        if(flag)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}

 

posted @ 2015-07-25 08:25  午夜阳光~  阅读(129)  评论(0编辑  收藏  举报