zoukankan      html  css  js  c++  java
  • 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): 9223    Accepted Submission(s): 2965


    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!
     
    Hint
    Huge input,scanf is recommended.
    Source
     1 #include<stdio.h>
     2 #include<string.h>
     3 int f[3000];
     4 int dis[3000];
     5 
     6 int find(int x)
     7 {
     8     int t;
     9     if(f[x]==x)
    10     return x;
    11 
    12     t=find(f[x]);
    13     dis[x]=(dis[x]+dis[f[x]])%2;
    14     f[x]=t;
    15     return t;
    16 
    17 }
    18 
    19 int make(int x,int y)
    20 {
    21     int a=find(x);
    22     int b=find(y);
    23     if(a==b)
    24     {
    25         if(dis[x]==dis[y])
    26             return 1;
    27         return 0;
    28     }
    29 
    30     f[a]=b;
    31     dis[a]=(dis[x]+dis[y]+1)%2;
    32     return 0;
    33 
    34 }
    35 
    36 int main()
    37 {
    38     //freopen("in.txt","r",stdin);
    39     int t,count=1;
    40     int n,m;
    41     int i,a,b,flag;
    42     scanf("%d",&t);
    43     while(t--)
    44     {
    45         scanf("%d%d",&n,&m);
    46         memset(dis,0,sizeof(dis));
    47 
    48         for(i=1;i<=n;i++)
    49         f[i]=i;
    50 
    51         flag=0;
    52         for(i=0;i<m;i++)
    53         {
    54             scanf("%d%d",&a,&b);
    55             if(make(a,b))
    56             {
    57                 flag=1;
    58             }
    59         }
    60 
    61         printf("Scenario #%d:
    ",count++);
    62         if(flag)
    63         printf("Suspicious bugs found!
    ");
    64         else
    65         printf("No suspicious bugs found!
    ");
    66         printf("
    ");
    67     }
    68     return 0;
    69 }
    View Code
     
  • 相关阅读:
    Arduino Nano与SIM800C 通信
    Laravel 查询数据按照时间分组
    Laravel 查询或写入Enum字段出错的bug解决办法
    【示例代码及地区json】js省市区三级联动 升级版
    【示例代码及地区json】js省市区三级联动
    Laravel使用whereHas进行过滤不符合条件的预加载with数据
    Laravel where条件拼接,数组拼接where条件
    postman传数组参数,二维数组,多维数组
    中国元器件电商
    蓝牙核心技术了解(蓝牙协议、架构、硬件和软件笔记)
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4105839.html
Copyright © 2011-2022 走看看