zoukankan      html  css  js  c++  java
  • nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    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.
     
    输入
    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 10000) 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.
    输出
    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.
    样例输入
    2
    3 3
    1 2
    2 3
    1 3
    4 2
    1 2
    3 4
    样例输出
    Scenario #1:
    Suspicious bugs found!
    
    Scenario #2:
    No suspicious bugs found!

     1 /**
     2     题目大意:
     3         判断输入的两个bug(臭虫)是否是同性恋,教授认为他判断的臭虫都是异性的,你的任务就是看教授
     4         的判断是否真确。
     5         ①、如果有两个臭虫是同性的,那么输出 Suspicious bugs found! (找到可疑(同性)臭虫)
     6         ②、如果 *bug(所有的bug) 都不是同性的,最后输出 no Suspicious bugs found! (没找到可疑(同性)臭虫)
     7 
     8     解题算法:并查集
     9 
    10     idea:
    11         1、将同性的所有臭虫放在一起。
    12             1.1、如果输入的数据在同一个集合中说明这对臭虫是可疑的
    13             1.2、如果输入的数据不在同一个集合说明它们到现在都还是异性关系
    14                 1.2.1、同时,我们假设数据a和数据a+n是异性关系
    15                 1.2.1、那么如果a 与 b也是异性关系,就有a+n 与 b是同性(放在同一个集合中)
    16 
    17     PS:其实,这是一个模板题,只要是这种关系:
    18         <数据只有两种情况、后一个数据的状态会被前面的数据影响、判断新插入的数据是否合法>
    19 **/

    核心模板:

     1 void my_init(int n) // 数据的初始化
     2 {
     3     for(int i = 0; i <= n; ++ i)
     4         pre[i] = i;
     5 }
     6 int my_find(int x) // 找到它的根节点
     7 {
     8     int n = x;
     9     while (n != pre[n])
    10         n = pre[n];
    11     int i = x, j;
    12     while (pre[i] != n) //路径压缩
    13     {
    14         j = pre[i];
    15         pre[i] = n;
    16         i = j
    17     }
    18     return n;
    19 }
    20 void my_join(int a, int b) // 不是同一个根节点的数据加入
    21 {
    22     int n1 = my_find(a), n2 = my_find(b);
    23     if (n1 != n2)
    24         pre[n1] = n2;
    25 }
    26 bool my_judge(int a, int b) // 判断是否在同一个集合中
    27 {
    28     int n1 = my_find(a), n2 = my_find(b);
    29     if (n1 != n2)   return true;
    30     return false;
    31 }

    C/C++代码实现(AC):

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 
     6 using namespace std;
     7 
     8 int T, n, m, pre[20005], flag, cnt;
     9 
    10 void my_init()
    11 {
    12     int nn = n + n;
    13     for (int i = 0; i <= nn; ++ i)
    14         pre[i] = i;
    15     return ;
    16 }
    17 
    18 int my_find(int x)
    19 {
    20     int n = x;
    21     while (n != pre[n])
    22         n = pre[n];
    23     int i = x, j;
    24     while (pre[i] != n)
    25     {
    26         j = pre[i];
    27         pre[i] = n;
    28         i = j;
    29     }
    30     return n;
    31 }
    32 
    33 bool judge(int a, int b)
    34 {
    35     int n1 = my_find(a), n2 = my_find(b);
    36     if (n1 != n2) return true;
    37     return false;
    38 }
    39 
    40 void my_join(int a, int b)
    41 {
    42     int n1 = my_find(a), n2 = my_find(b);
    43     if (n1 != n2) pre[n1] = n2;
    44     return ;
    45 }
    46 
    47 int main()
    48 {
    49     cnt = 1;
    50     scanf("%d", &T);
    51     for(int i = 0; i < T; ++ i)
    52     {
    53         flag = 0;
    54         scanf("%d%d", &n, &m);
    55         my_init();
    56         while (m --)
    57         {
    58             int a, b;
    59             scanf("%d%d", &a, &b);
    60             if (flag) continue;
    61 
    62             if (judge(a, b) || judge(a + n, b + n))
    63             {
    64                 my_join(a, b + n);
    65                 my_join(a + n, b);
    66             }
    67             else
    68                 flag = 1;
    69         }
    70 
    71         if (i != 0) puts("");
    72         if (flag)
    73             printf("Scenario #%d:
    Suspicious bugs found!
    ", cnt ++);
    74         else
    75             printf("Scenario #%d:
    No suspicious bugs found!
    ", cnt ++);
    76 
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    Windows网络编程经验小结
    异步Socket服务器与客户端
    用C#实现C/S模式下软件自动在线升级
    Linux 安装字体
    word 生成目录
    Linux sar使用
    yum 使用说明
    HASH JOIN算法
    row cache lock
    cursor: pin S
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/8994091.html
Copyright © 2011-2022 走看看