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 }
  • 相关阅读:
    The Princess and the Pea,摘自iOS应用Snow White and more stories
    Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复
    Android笔记之获取debug.keystore和release.keystore的MD5/SHA1值
    Android笔记之WebView加载网页的进度回调
    Android Studio停留在“Indexing paused due to batch update”的解决方案
    【Python初级】StringIO和BytesIO读写操作的小思考
    【Python初级】由判定回文数想到的,关于深浅复制,以及字符串反转的问题
    【Python初级】由生成杨辉三角代码所思考的一些问题
    【面试总结-编程】多行两列数据,实现同key的value求和并输出
    【算法与数据结构实战】模拟竖式加法,自定义位数
  • 原文地址:https://www.cnblogs.com/awsent/p/4266973.html
Copyright © 2011-2022 走看看