zoukankan      html  css  js  c++  java
  • (专题赛)A Bug's Life

    并查集

    #转题意:Professor Hopper专门研究bug的生活习性,他表示若两只bugs的生活习性差别很大,则说明他们可能为不同的性别,但如果出现三只bugs的习性两两差别很大,则有可能出现同性恋的bug了。现在有n只bugs,和生活习性差别很大的m对bugs的编号,问这些bugs中,有没有可能出现同性恋者。题目中给出的数对比如(1 2 ,2 3 ,1 3)是表示交配关系的,而且交配的都默认为理解为异性,这里如果有矛盾的情况,就表明有同性恋存在。比如1和2是异性,2和3为异性,那就表明1和3是同性,但是给出的数对是1和3异性表明有矛盾存在,则有同性恋存在,则输出Suspicious bugs found。

    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!

      1 #include<stdio.h>
      2 
      3 
      4 
      5 int pre[1000],rank[1000];
      6 
      7 
      8 
      9 int find(int x)
     10 
     11 {
     12 
     13 if(x!=pre[x])
     14 
     15 pre[x]=find(pre[x]);
     16 
     17 return pre[x];
     18 
     19 }
     20 
     21 
     22 
     23 void join(int i,int j)
     24 
     25 {
     26 
     27 int x=find(i);
     28 
     29 int y=find(j);
     30 
     31 if(x==y)
     32 
     33 return;
     34 
     35 else
     36 
     37 pre[y]=x;
     38 
     39 }
     40 
     41 
     42 
     43 int main()
     44 
     45 {
     46 
     47 int t,i,m,n,x,y,ti=1;
     48 
     49 scanf("%d",&t);
     50 
     51 while(t--)
     52 
     53 {
     54 
     55 scanf("%d",&n);
     56 
     57 for(i=1;i<=n;i++)
     58 
     59 {
     60 
     61 pre[i]=i;
     62 
     63 rank[i]=0;
     64 
     65 }
     66 
     67 int flag=0;
     68 
     69 scanf("%d",&m);
     70 
     71 while(m--)
     72 
     73 {
     74 
     75 scanf("%d %d",&x,&y);
     76 
     77 if(flag)
     78 
     79 continue;
     80 
     81 if(find(x)==find(y))
     82 
     83 {//若x,y在同一个集合,则证明同性恋存在
     84 
     85 flag=1;
     86 
     87 }
     88 
     89 if(rank[x]==0&&rank[y]==0)
     90 
     91 {
     92 
     93 rank[x]=y; //表明y是x的异性
     94 
     95 rank[y]=x;
     96 
     97 }
     98 
     99 else if(rank[x]==0)
    100 
    101 {
    102 
    103 rank[x]=y;
    104 
    105 join(x,rank[y]);
    106 
    107 }
    108 
    109 else if(rank[y]==0)
    110 
    111 {
    112 
    113 rank[y]=x;
    114 
    115 join(y,rank[x]);
    116 
    117 }
    118 
    119 else
    120 
    121 {
    122 
    123 join(x,rank[y]);
    124 
    125 join(y,rank[x]);
    126 
    127 }
    128 
    129 }
    130 
    131 printf("Scenario #%d:
    ",ti++);
    132 
    133 if(flag)
    134 
    135 puts("Suspicious bugs found!");
    136 
    137 else
    138 
    139 puts("No suspicious bugs found!");
    140 
    141 printf("
    ");
    142 
    143 }
    144 
    145 return 0;
    146 
    147 }
  • 相关阅读:
    校验XX是否在有效期内
    Thymleaf js直接获取后台传过来的对象或者对象的属性以及map
    H5新特性之data-*
    Thymleaf中th:each标签遍历list如何获取index
    SpringBoot图片上传(三)——调用文件上传项目的方法(同时启动两个项目)
    根据状态码,展示不同的文本,两种方法简单讨论
    列表前台验空的必要性
    thymleaf模板截取日期的年月日,去掉时分秒
    javaWeb锁屏的简单实现
    svn上check下来的项目,用idea打开,菜单栏没有svn工具解决办法
  • 原文地址:https://www.cnblogs.com/awsent/p/4266973.html
Copyright © 2011-2022 走看看