zoukankan      html  css  js  c++  java
  • A Bug's Life(加权并查集)

    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!



    题目意思:t组数据,n个虫子,m组相互喜爱的关系,虫子分为雌雄两种,每个虫子只有一个性别,问是否存在同性恋的虫子。

    解题思路:一开始我的思路是希望通过将虫子划分为雌雄两个集合,看看这两个集合中是否出现了环,出现了环则说明存在着同性恋。我看了看网上的题 解,给出了加权并查集的概念,这里我就试着使用这个思想来解题。

    加权并查集: 有的时候,不仅需要像普通并查集一样记录一些元素之间有无关系,还需要记录它们之间有怎样的关系,这时候就需要引入加权并查集。 通常情况下,用一个数组r来记录这些关系,r[i]表示元素i与父结点的关系。至于是什么关系,还要根据具体要求来看。 在find(x)函数中进行路径压缩的同时,许多结点的父结点会改变,这时就需要根据实际情况调整权值以保证其正确性。 在union(x,y)函数中,(不妨设将y集合并入x集合)由于y的父结点的改变,需要调整y对应的权值,但不需要调整y的子结点对应的权值,因为子结点 权值会在find(子结点)时得到调整。

     

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int pre[20010];
     6 int r[20010];///r=0代表与根节点同性
     7 int Find(int x)
     8 {
     9     int t;
    10     if(pre[x]==x)
    11     {
    12         return x;
    13     }
    14     t=pre[x];
    15     pre[x]=Find(pre[x]);///压缩路径
    16     r[x]=(r[x]+r[t]+1)%2;
    17     return pre[x];
    18 }
    19 void Union(int a,int b)
    20 {
    21     int x,y;
    22     x=Find(a);
    23     y=Find(b);
    24     pre[x]=y;
    25     r[x]=(r[b]-r[a])%2;
    26 }
    27 int main()
    28 {
    29     int t,i,j,k,flag;
    30     int n,m,a,b;
    31     scanf("%d",&t);
    32     for(k=1;k<=t;k++)
    33     {
    34         flag=0;
    35         scanf("%d%d",&n,&m);
    36         for(i=1; i<=n; i++)
    37         {
    38             r[i]=1;
    39             pre[i]=i;
    40         }
    41         for(i=1; i<=m; i++)
    42         {
    43             scanf("%d%d",&a,&b);
    44             if(Find(a)==Find(b))
    45             {
    46                 if(r[a]==r[b])///同性
    47                 {
    48                     flag=1;
    49                 }
    50             }
    51             else
    52             {
    53                 Union(a,b);
    54             }
    55         }
    56         if(flag)
    57         {
    58             printf("Scenario #%d:
    Suspicious bugs found!
    
    ",k);
    59         }
    60         else
    61         {
    62             printf("Scenario #%d:
    No suspicious bugs found!
    
    ",k);
    63         }
    64     }
    65     return 0;
    66 }
    
    
  • 相关阅读:
    购买云主机时应该注意哪些事项
    wdcp的安装方法与常见问题
    推荐一些不错的计算机书籍(php c mysql linux等等)
    .htaccess详解及.htaccess参数说明【转】
    常用的7个.htaccess代码组织某个国家的IP访问
    最新ecshop v2.7.3版本去版权完全版
    javascript常用数组算法总结
    10道javascript笔试题
    Magento显示多货币,Magento 多货币设置
    Magento后台手动修改订单状态方法及手动修改方法php
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9810784.html
Copyright © 2011-2022 走看看