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 }
  • 相关阅读:
    MySQL架构优化:定时计划任务与表分区
    关于mysql 删除数据后物理空间未释放(转载)
    Mysql删除数据后磁盘空间未释放的解决办法【转】
    Mysql 自动备份脚本
    迄今最安全的MySQL?细数5.7那些惊艳与鸡肋的新特性(上)【转载】
    Redis学习笔记(二)-key相关命令【转载】
    干货分享:MySQL之化险为夷的【钻石】抢购风暴【转载】
    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https【转载】
    业务零影响!如何在Online环境中巧用MySQL传统复制技术【转】
    VNC轻松连接远程Linux桌面
  • 原文地址:https://www.cnblogs.com/awsent/p/4266973.html
Copyright © 2011-2022 走看看