• POJ 2492 A Bug's Life (并查集)


    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.
     
    题解:有N个虫子 只有异性交配才是正常的 但我们不知道哪个虫子的性别是什么 给出M个交配事件 问能否发现同性交配的事件(种类并查集)
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 const int N=2005;
    29 const int mod=1e9+7;
    30 int n,q;
    31 int fath[2005],a[2005];
    32 void init()
    33 {
    34     for(int i=1;i<=n;i++){
    35         fath[i]=i;
    36         a[i]=0;
    37     }
    38 }
    39 int findd(int x)
    40 {
    41     if(x==fath[x])return x;
    42     int y=findd(fath[x]);
    43     a[x]=(a[x]+a[fath[x]])&1;
    44     fath[x]=y;
    45     return y;
    46 }
    47 bool SetUnion(int x,int y)
    48 {
    49     int fx=findd(x);
    50     int fy=findd(y);
    51     if(fx==fy){
    52         if(a[x]==a[y]) return true;
    53         else return false;
    54     }
    55     fath[fx]=fy;
    56     if(a[y]){
    57         a[fx]=a[x];
    58     }
    59     else{
    60         a[fx]=1-a[x];
    61     }
    62     return false;
    63 }
    64 int main()
    65 {
    66     int t,case1=1,x,y;
    67     scanf("%d",&t);
    68     while(t--){
    69         bool flat=true;
    70         scanf("%d%d",&n,&q);
    71         init();
    72         for(int i=0;i<q;i++){
    73             scanf("%d%d",&x,&y);
    74             if(SetUnion(x,y))flat=false;
    75         }
    76         printf("Scenario #%d:
    ",case1++);
    77         if(!flat)puts("Suspicious bugs found!
    ");
    78         else puts("No suspicious bugs found!
    ");
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    可在广域网部署运行的QQ高仿版 -- GG叽叽(源码)
    区间合并
    二分查找算法模板
    神经网络详解(RNN/LSTM)
    反向传播算法推导过程(非常详细)
    机器学习-回归问题(Regression)
    从RNN到LSTM
    神经网络浅讲:从神经元到深度学习
    部署高并发python后端(Systemd+Nginx+Gunicorn+Gevent+Supervisor+Flask )
    产品笔记 | 软件版本号—规范与命名规则
  • 原文地址:https://www.cnblogs.com/wydxry/p/7272687.html
Copyright © 2020-2023  润新知