zoukankan      html  css  js  c++  java
  • 2017ecjtu-summer training # 11 POJ 2492

    A Bug's Life
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 38280   Accepted: 12452

    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!

    题目意思就是 给定n只虫子 不同性别的可以在一起 相同性别的不能在一起
    给你m对虫子 判断中间有没有同性别在一起的;
    我们把同性的放到一个集合里 如果一个集合里出现了异性 则说明存在同性恋在一起
    假设 x 为一种性别 x+n为与其相反的性别  
    若a,b为同性 的  我们则可以把判断 (a,b+n)  (b,a+n)为异性反之亦然;
    AC代码
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <queue>
     8 #include <vector>
     9 #include <algorithm>
    10 #define maxn 2010
    11 #define inf  0x3f3f3f3f
    12 using namespace std;
    13 typedef long long ll;
    14 int par[maxn*2];
    15 void init(int n)
    16 {
    17     for(int i=1;i<=n;i++)
    18     {
    19         par[i]=i;
    20     }
    21 }
    22 int find(int x)
    23 {
    24     if(par[x]==x)
    25         return x;
    26     else
    27         return par[x]=find(par[x]);
    28 }
    29 void uion(int x,int y)
    30 {
    31     int x1=find(x);
    32     int y1=find(y);
    33     if(x1!=y1)
    34         par[x1]=y1;
    35     return;
    36 }
    37 int main(int argc, char const *argv[])
    38 {
    39     int t;
    40     cin>>t;
    41     for(int i=1;i<=t;i++)
    42     {
    43         int n,m;
    44         cin>>n>>m;
    45         init(n*2);
    46         int x,y;
    47         int flag=0;
    48         for(int j=0;j<m;j++)
    49         {
    50             scanf("%d %d",&x,&y);
    51             if(find(x)==find(y)||find(x+n)==find(y+n))
    52             {
    53                 flag=1;
    54                 printf("Scenario #%d:
    Suspicious bugs found!
    
    ",i);
    55                 break;
    56             }
    57             else
    58             {
    59                 uion(x,y+n);
    60                 uion(x+n,y);
    61             }
    62         }
    63         if(flag==0)
    64              printf("Scenario #%d:
    No suspicious bugs found!
    
    ",i);
    65     }
    66     return 0;
    67 }

    看到某博客另一种写法很不错 

     http://www.cnblogs.com/dongsheng/archive/2012/08/08/2627917.html

     1 /* 功能Function Description:     POJ-2492 并查集应用的扩展
     2    开发环境Environment:          DEV C++ 4.9.9.1
     3    技术特点Technique:
     4    版本Version:
     5    作者Author:                   可笑痴狂
     6    日期Date:                      20120808
     7    备注Notes:
     8 关于并查集,注意两个概念:按秩合并、路径压缩。
     9 1、按秩合并
    10 由于并查集一般是用比较高效的树形结构来表示的,按秩合并的目的就是防止产生退化的树(也就是类似链表的树),
    11 用一个数组记录各个元素的高度(也有的记录各个元素的孩子的数目,具体看哪种能给解题带来方便),
    12 然后在合并的时候把高度小的嫁接到高度大的上面,从而防止产生退化的树。
    13 2、路径压缩
    14 而另一个数组记录各个元素的祖先,这样就防止一步步地递归查找父亲从而损失的时间。因为并查集只要搞清楚各个元素所在的集合,
    15 而区分不同的集合我们用的是代表元素(也就是树根),所以对于每个元素我们只需保存其祖先,从而区分不同的集合。
    16 而我们这道题并没有使用纯正的并查集算法,而是对其进行了扩展,
    17 我们并没有使用“1、按秩合并”(当然你可以用,那样就需要再开一个数组)
    18 我们从“1、按秩合并”得到启示,保存“秩”的数组保存的是    元素相对于父节点的关系  ,我们岂不可以利用这种关系
    19 (即相对于父节点的不同秩值来区分不同的集合),从而可以把两个集合合并成一个集合。
    20 (注:此代码 relation=0 代表 和父节点同一性别)
    21 */
    22 
    23 
    24 #include<stdio.h>
    25 int father[2005];
    26 int relation[2005];
    27 
    28 int find_father(int i)
    29 {
    30     int t;
    31     if(father[i]==i)
    32         return i;
    33 
    34     //计算相对于新的父节点(即根)的秩,relation[t]是老的父节点相对于新的父节点(即根)的秩,relation[i]是i元素相对于老的父节点的秩,
    35     //类似于物理里的相对运动,得到的r[i]就是相对于新的父节点(即根)的秩。而且这个递归调用不会超过两层
    36     t=father[i];    
    37     father[i]=find_father(father[i]);
    38     relation[i]=(relation[i]+relation[t]+1)%2;   //注意递归中把这棵树relation中的的值都更新一遍,这句的顺序 不能 和上一句 调换位置
    39     // relation[a]的改变是伴随着father[a]的改变而更新的(有father改变就有relation改变),要是father改变了,而relation未改变,此时的relation就记录了一个错误的值,
    40     //father未改变(即使实际的father已不是现在的值,但只要father未改变,relation的值就是“正确”的,认识到这点很重要。)
    41     return father[i];
    42 }
    43 
    44 void merge(int a,int b)
    45 {
    46     int x,y;
    47     x=find_father(a);
    48     y=find_father(b);
    49     father[x]=y;
    50     relation[x]=(relation[b]-relation[a])%2;//relation[a]+relation[x]与relation[b]相对于新的父节点必须相差1个等级,因为他们不是gay
    51 }                                            //x下边的节点不用改,因为查找的时候会自动更新
    52 
    53 int main()
    54 {
    55     int T,m,n,i,j,a,b,flag;
    56     scanf("%d",&T);
    57     for(i=1;i<=T;++i)
    58     {
    59         flag=0;
    60         scanf("%d%d",&n,&m);
    61         for(j=1;j<=n;++j)       //初始化
    62         {
    63             father[j]=j;
    64             relation[j]=1;
    65         }
    66         for(j=1;j<=m;++j)
    67         {
    68             scanf("%d%d",&a,&b);
    69             if(find_father(a)==find_father(b))
    70             {
    71             //    if(relation[a]!=(relation[b]+1)%2)
    72                 if(relation[a]==relation[b])            //说明是同性
    73                     flag=1;
    74             }
    75             else
    76                 merge(a,b);
    77         }
    78         if(flag)
    79             printf("Scenario #%d:
    Suspicious bugs found!
    
    ",i);
    80         else
    81             printf("Scenario #%d:
    No suspicious bugs found!
    
    ",i);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    Postman初探
    web页面和本地数据对比问题
    Katalon Recorder初探
    Flask入门
    自我实现预言
    gulp 安装 依赖
    maven环境
    加解密 生成 X.509格式,DER编码,后缀名.cer。加密公钥证书
    我的魔法 公式找回中
    gulp 自动ftp至服务器时,处理开发 测试服务器地址问题
  • 原文地址:https://www.cnblogs.com/stranger-/p/7274931.html
Copyright © 2011-2022 走看看