1. 程式人生 > >並查集——A bug s life

並查集——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;
}

相關推薦

——A bug s life

                                                     A Bug's Life                                              Time Limit: 15000/5000 MS

-A Bug's Life(poj2492)

題意: 給出N條蟲子,讓a和b交配, 給出M對a和b交配後問, 有沒有性別矛盾的蟲子, 即和一隻蟲子和男的交配完之後又和女的交配 題解: 1.壓縮路徑關係轉化,r[x] = (r[x]+r[f[x]])%2,很好理解//r[x]表示x和fx的關係,0同性,1異性 若x和fx為同性(r[x] = 0)

數算實習 A bug's life

背景 Hopper教授正在研究一種稀有物種的性行為。他假設他們有兩種不同的性別,他們只與異性發生性行為。每個個體的編號都印在他們的背上。 問題 給出一個bug之間的性行為列表,確定實驗是否支援他的假設:該物種之間不存在同性戀關係。 輸入 輸入的第一行包含實驗的數

zcmu 1437 A Bug's Life

【題目】 1437: A Bug's Life Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 112  Solved: 49 [Submit][Status][Web Board] Description Prof

【ZCMU1437】A Bug's Life(種類

題目連結 1437: A Bug's Life Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 113  Solved: 50 [Submit][Status][Web Board] Description Pr

A Bug's Life(加權

Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature t

E - a bugs life HDU - 1829 種類

E - a bug‘s life HDU - 1829 /*為每個蟲子開兩個節點 分別表示該蟲子為雄性和該蟲子為雌性 例如1表示蟲子1為雄性2表示蟲子1為雌性,3表示蟲子2是雄性4表示蟲子2是雌性 如果資料給出了1 2交配,則合併1 4和2 3,表示1 4同時為真和2 3同時為真 依

HDU 1829 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): 7507    Accepted

poj 2492 A Bug's Life()分組

A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 29058 Accepted: 9467 Description Background  Professor Hop

A Bug's Life (分類

Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different gend

A Bugs life POJ 2492 解題報告 (種類

   這也算是種類並查集的一道經典例題了吧,題意就不多解釋了,先寫一些我對種類並查集的一些理解。    種類並查集比普通的並查集多一個relation陣列,relation[i] 記錄了 i 和 其直接父親節點的關係,這個關係的表示因題目而異,種類並查集的重點和難點就是

A Bug's Life 種類

就是種類並查集的一種,如果理解食物鏈的思想,這道題應該不是問題,都是用向量的思想理解,程式碼如下: #include<stdio.h> struct node { int f; int r; } s[20010]; int find(int x

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): 12358    Accepted

A Bug's Life

Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

HDU 1829 A Bug's Life

問題描述: Problem Description Background Professor Hopper is researching the sexual behavior of a rar

POJ-2492 A Bug's Life (種類

A Bug’s Life Background Professor Hopper is researching th

A Bug's Life(帶權

hdu1829 這是一道比較簡單 好理解的並查集 #include <iostream> #include <stdio.h> #include <math.h> #include <algorithm> #include &

百練 2492 : A Bug's Life --簡單應用

他說每個測試輸出兩行,其實是三行吧,否則PE. 並查集的簡單應用,類似於”發現它,抓住它”. 描述 Hopper 博士正在研究一種罕見種類的蟲子的性行為。他假定蟲子只表現為兩種性別,並且蟲子只與異

2492  A Bug's Life (帶權

解題思路:d[i]=0表示父節點同性 d[i]==1 表示與父節點異性  #include<iostream> #include<cstdio> #include<vector> #include<algorithm>

A Bug's Life

     Background     Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two differ