hdu 1829 a bug's life 5.1.1

来源:互联网 发布:ubuntu磁盘空间不足 编辑:程序博客网 时间:2024/06/05 14:30

计划总不如变化快……java和大雾都没看T0T……先贴个题……晚上熬个夜,明天再来贴代码……

熄灯什么的真是伤不起啊……

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 143 Accepted Submission(s): 60 
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
23 31 22 31 34 21 23 4
 
Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
Hint
Huge input,scanf is recommended.
 
话说并查集是个什么玩意儿啊听着就觉得好高级啊……

其实发现之前做过并查集的题的……不过当时就觉得晕晕乎乎不会用,现在还是……

两个代码,第一个是用sex【i】保存i与根的性别的比较,同性为0,异性为1

#include <iostream>using namespace std;int n,r;//don't use set and sex next time. it is easy to confuse them.int set[2002];//store the root bool sex[2002];//store the relationship with the root. 1 means different sex, 0 mean same sex.int find(int x)//find the root of x{    if(set[x]==x)return x;//root is self    int t;    t=set[x];    set[x]=find(set[x]);//find the real root    sex[x]=(sex[x]+sex[t])%2;//compare the sex with root     //sex[x]is the relation between x and it's former root. sex[t] is the relation between the former root with the real root    //plus and remaind can get the relation between x and the real root    return set[x];}void merge(int x,int y){     int fx,fy;     fx=find(x);     fy=find(y);     set[fx]=fy;//put x into y     sex[fx]=(sex[x]+sex[y]+1)%2;     //x compare to its root, y compare to its root. 1 means x and y are different sex.     //plus and remaind so we get fx compare to fy}    int main(){    int cas;    cin>>cas;    int ci=0;    int x,y;//temp    while(cas--)    {        ci++;        int i;        int flag=0;//0 means no suspicious        scanf("%d%d",&n,&r);//number of bug, relation        for(i=0;i<n;i++)//it is n,not r...        {            set[i]=i;//everybug's root is it self            sex[i]=0;        }                            while(r--)//it is r,not n...        {                        scanf("%d%d",&x,&y);            if(find(x)==find(y))            //have the same root, which means they can be compared            {                if(sex[x]==sex[y])//sex is the same                flag=1;            }            else            merge(x,y);//merge them into one root            // because the data is simple, so we just simply put x into y. no need to worry about the height of x and y        }        printf("Scenario #%d:\n",ci);            if(flag)                printf("Suspicious bugs found!\n");            else                printf("No suspicious bugs found!\n");        printf("\n");    }    system("pause");    return 0;}                                

说真的find和merge时算sex值好麻烦啊……算着算着就晕了……

sex里面存的是一个异性的编号。每次读入的两个数x,y,如果有异性编号sex[x](sex[y]),就把对方y(x]和该异性sex[x](sex[y])合并。两个数都要做一次。时间比上面的略长一点点。

相对上面的比较容易想到,下面的比较容易吧……

原创粉丝点击