并查集——A bug s life

                                                     A Bug's Life

                                             Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                 Total Submission(s): 13068    Accepted Submission(s): 4262


Problem 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
    作为一个并查集的新手,加上也不是很努力难过,so,,两天才解决了这个问题。。of course ,也是千辛万苦看了网上的代码才马马虎虎“弄懂”这题大哭。。。。。。。。。
相信也有很多像我一样的新手不懂这题的思路。。那么,就让我来详细地讲一下这题吧。。
    首先,题意是:有一群‘bug’,它们跟人一样,也分雌雄,也能交配,甚至也有同性恋。。。那么,这题就是,通过给你一串数据,让你判断这群‘bug’中有没有同性恋。
比如,输入1 2,代表1和2进行了交配,那么就是说明1和2是异性,这里assume 1是雄,那么2便是雌。紧接着输入 2 3,代表2和3进行了交配,前面已经有了2是雌的,那么3自然是雄的‘最后一对数据是 1 3,第一对中已经有1 是雄的,那么3便是雌的咯,可是第二对数据中已经说明3是雄的,那么与这对数据中3是雌的相矛盾。。so,唯一的解释就是 3 是同性恋尴尬。。好啦,既然是同性恋,那么便输出 suspicious bugs found!
    思路:把每一个’bug‘看作一个节点,开两个数组,f[MAX]用来储存每个节点的父节点,r[MAX]用来储存每个节点的性别(这里,性别用 1和0来表示,就好像人的男和女,动物的雌和雄一样。开始把每一个节点的性别都设为0),那么,如何来判断 是 存在同性恋的呢?看下图。(并查集的基本知识这里就不重复强调了)


我的字确实很烂。。。不过自认为图还是不错滴得意
    那么具体实现就直接看代码啦,,
#include<stdio.h>
int f[10001],r[10001],t,flag;

int find(int x)
{
    if(f[x]==x)
        return x;
    t=find(f[x]);
    r[x]=(r[f[x]]+r[x])&1;
    f[x]=t;
    return f[x];
}

void bin(int x,int y)
{
    int fx,fy;
    fx=find(x);
    fy=find(y);
    if(fx==fy)
    {
        if(r[x]==r[y])
            flag=1;
        return;
    }
    f[fx]=fy;
    r[fx]=(r[x]+r[y]+1)&1;
}

int main()
{
    int N,k=1;
    scanf("%d",&N);
    while(N--)
    {
        int n,s;
        int i;
        int x,y;
        flag=0;
        scanf("%d%d",&n,&s);
        for(i=0;i<=n;i++)
        {
            
            f[i]=i;
            r[i]=0;
        }
        for(i=1;i<=s;i++)
        {
            scanf("%d%d",&x,&y);
            if(flag==1)
                continue;
            bin(x,y);
        }
        printf("Scenario #%d:\n",k++);
        if(flag)printf("Suspicious bugs found!\n");
        else printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值