zoukankan      html  css  js  c++  java
  • POJ

    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!

    Hint

    Huge input,scanf is recommended.
     
      1 /*
      2 #include <stdio.h>
      3 #include <iostream>
      4 using namespace std;
      5 //存储的是其父亲的下表
      6 int bugs[2010];
      7 int relation[2010];//1:相同性别 0:不同性别
      8 //初始化
      9 void init(int len)
     10 {
     11     for(int i = 0;i <= len; i++)
     12     {
     13         bugs[i] = i;
     14         relation[i] = 1;
     15     }
     16 }
     17 //找到根
     18 int find(int bug)
     19 {
     20     if(bugs[bug]==bug)return bug;
     21     int tem = bugs[bug];
     22     bugs[bug] = find(bugs[bug]);//递归更新域,返回最终的父亲节点,把所有的孩子都更新了
     23     //注意这里,求当前位置和父亲的关系,记录之前父亲的位置为tem,然后因为是递归,
     24     //此时的relation[tem]已经在递归中更新过了,也就是孩子和父亲的关系+父亲和爷爷的关系+1然后模2就得到
     25     //孩子和爷爷的关系,这里用0和1表示,0表示不同性别,1表示相同性别
     26     relation[bug] = (relation[bug]+relation[tem]+1)%2;
     27     return bugs[bug];
     28 }
     29 
     30 void union_set(int a,int b,int x,int y)
     31 {
     32     //合并,让前边的集合的根指向后边集合的根,成为一个集合
     33     bugs[x]=y;
     34     //更新前边集合根和新的集合根之间的关系,
     35     //注意这里,relation[a]+relation[x]与relation[b]
     36     //相对于新的父节点必须相差1个等级,因为他们不是gay
     37     relation[x] = (relation[b]-relation[a])%2;
     38 }
     39 
     40 int main()
     41 {
     42     int S;
     43     int n,inter;
     44     int bug1,bug2,parent1,parent2;
     45     bool flag;//false:无同性恋,true:有同性恋
     46     scanf("%d",&S);
     47     for(int i=1; i<=S;i++)
     48     {
     49         scanf("%d%d",&n,&inter);
     50         flag = false;
     51         init(n);//初始化,使其父节点为自己
     52         for(int j = 1; j <= inter; j++)
     53         {
     54             scanf("%d%d",&bug1,&bug2);
     55             if(flag)continue;
     56             parent1 = find(bug1);
     57             parent2 = find(bug2);
     58             if(parent1==parent2)
     59             {
     60                 if(relation[bug1]==relation[bug2])//同性
     61                 flag = true;
     62             }
     63             union_set(bug1,bug2,parent1,parent2);
     64         }
     65         if(flag)
     66         printf("Scenario #%d:
    Suspicious bugs found!
    ",i);
     67         else
     68         printf("Scenario #%d:
    No suspicious bugs found!
    ",i);
     69         printf("
    ");
     70     }
     71     return 0;
     72 }
     73 
     74 wa  了不知为什么,,
     75 */    
     76 #include <cstdio>
     77 #include <iostream>
     78 #include <cstring>
     79 
     80 using namespace std;
     81 
     82 const int MAX = 2005;
     83 
     84 bool flag;
     85 int fa, fb, n;
     86 int father[MAX], rela[MAX];
     87 
     88 int find(int x)
     89 {
     90     if(father[x] == -1)
     91         return x;
     92     else
     93     {
     94         int tmp = father[x];
     95         father[x] = find(father[x]);
     96 
     97         rela[x] = (rela[x]+rela[tmp])%2;
     98         return father[x];
     99     }
    100 }
    101 
    102 int main()
    103 {
    104     int T, m, a, b;
    105     scanf("%d", &T);
    106     for(int k = 1; k <= T; ++k)
    107     {
    108         flag = true;
    109         scanf("%d%d", &n, &m);
    110         {
    111             memset(father, -1, (n+1)*sizeof(int));
    112             memset(rela, 0, (n+1)*sizeof(int));
    113             while(m--)
    114             {
    115                 scanf("%d%d", &a, &b);
    116                 if(!flag)
    117                     continue;
    118                 fa = find(a);
    119                 fb = find(b);
    120 
    121                 if(fa != fb)
    122                 {
    123                     father[fa] = fb;
    124                     rela[fa] = (rela[a] + rela[b] + 1)%2;
    125                 }
    126                 else
    127                 {
    128                     if(rela[a] == rela[b])
    129                         flag = false;
    130                 }
    131 
    132             }
    133 
    134             printf("Scenario #%d:
    ", k);
    135             if(flag)
    136                 printf("No suspicious bugs found!
    
    ");
    137             else
    138                 printf("Suspicious bugs found!
    
    ");
    139         }
    140     }
    141     return 0;
    142 }
  • 相关阅读:
    Java基础--day04
    Java基础--day03
    Java基础--day02
    高斯键盘设置指南
    博客园主题配置
    算法笔记--二分
    Test2反思
    树链剖分【模板】
    7.20关于莫队算法的一些初步理解
    分块(n根n复杂度)
  • 原文地址:https://www.cnblogs.com/z-712/p/7324533.html
Copyright © 2011-2022 走看看