zoukankan      html  css  js  c++  java
  • POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492

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

    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!

    Hint

    Huge input,scanf is recommended.

    Source

    TUD Programming Contest 2005, Darmstadt, Germany
     
     
     
     
    题解:
    带权并查集。r[]用于表示当前结点与父节点是同性还是异性。
     
     
    带权并查集:
     
      带权:r[]数组可以记录当前结点与父节点的关系,可以是大小关系, 可以是逻辑关系(如此题)。对于相同的集合,由于在这棵树中,每个结点与父节点的关系已经确定,那么每个节点与集合中的其他结点的关系也可以一路推导出来。对于两个不同的集合,如果知道一对位于不同集合的结点的关系,那么这两个集合所有的结点之间的关系也可以推导出来了,即两个集合可以合并为一个集合。  
     
      路径压缩:对于被find()函数访问过的结点x, 它们的fa[x]都会直接指向根节点,同时需要更新r[x]数组(一路叠加)。问:那么对于被访问过的结点x的子树怎么办呢,不会被落下吗?答:结点x的子树的fa[]指针没有改变,仍然是指着x,即x的子树一直跟着x。
     
      合并:对于两个不同的集合,由于在对u、v调用find()函数时,u和v都分别指向了各自的根节点(路径压缩)。设fu为u所在集合的根节点(也是u的父节点), fv也如此,所以u和fu的关系即为r[u]、v和fv的关系即为r[v],且又知道u和v的关系, 那么就可以直接推出fu和fv的关系,这样就可以实现两个集合的合并。
     
     
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 2e3+10;
    19 
    20 int n, m;
    21 int fa[MAXN], r[MAXN];
    22 
    23 int find(int x)
    24 {
    25     if(fa[x]==-1) return x;
    26     int pre = find(fa[x]);
    27     r[x] = (r[x]+r[fa[x]])%2;
    28     return fa[x] = pre;
    29 }
    30 
    31 bool Union(int u, int v)
    32 {
    33     int fu = find(u);
    34     int fv = find(v);
    35     if(fu==fv)
    36         return r[u]==r[v];
    37 
    38     fa[fu] = fv;
    39     r[fu] = (-r[u]+1+r[v])%2;
    40     return false;
    41 }
    42 
    43 int main()
    44 {
    45     int T;
    46     scanf("%d", &T);
    47     for(int kase = 1; kase<=T; kase++)
    48     {
    49         scanf("%d%d", &n, &m);
    50         memset(fa, -1, sizeof(fa));
    51         memset(r, 0, sizeof(r));
    52 
    53         bool flag = true;
    54         for(int i = 1; i<=m; i++)
    55         {
    56             int u, v;
    57             scanf("%d%d", &u, &v);
    58             if(Union(u, v))
    59                 flag = false;
    60         }
    61 
    62         printf("Scenario #%d:
    ", kase);
    63         printf("%s
    
    ", flag?"No suspicious bugs found!":"Suspicious bugs found!");
    64     }
    65 }
    View Code

    代码二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 2e3+10;
    19 
    20 int n, m;
    21 int fa[MAXN], r[MAXN];
    22 
    23 int find(int x)
    24 {
    25     if(fa[x]==-1) return x;
    26     int pre = find(fa[x]);
    27     r[x] ^= r[fa[x]];
    28     return fa[x] = pre;
    29 }
    30 
    31 bool Union(int u, int v)
    32 {
    33     int fu = find(u);
    34     int fv = find(v);
    35     if(fu==fv)
    36         return r[u]==r[v];
    37 
    38     fa[fu] = fv;
    39     r[fu] = r[u]^1^r[v];
    40     return false;
    41 }
    42 
    43 int main()
    44 {
    45     int T;
    46     scanf("%d", &T);
    47     for(int kase = 1; kase<=T; kase++)
    48     {
    49         scanf("%d%d", &n, &m);
    50         memset(fa, -1, sizeof(fa));
    51         memset(r, 0, sizeof(r));
    52 
    53         bool flag = true;
    54         for(int i = 1; i<=m; i++)
    55         {
    56             int u, v;
    57             scanf("%d%d", &u, &v);
    58             if(Union(u, v))
    59                 flag = false;
    60         }
    61 
    62         printf("Scenario #%d:
    ", kase);
    63         printf("%s
    
    ", flag?"No suspicious bugs found!":"Suspicious bugs found!");
    64     }
    65 }
    View Code
  • 相关阅读:
    ExpandoObject与DynamicObject的使用
    ManualResetEvent 线程通信
    CancellationTokenSource 取消任务
    SQL Server 每日一题--老二解析
    说说 C# 8 using 新特性
    SQL Server 每日一题--老二
    C#中的坑--浮点类型
    开胃菜解析
    开胃菜
    快速入门 Arrow 日期处理库
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7652708.html
Copyright © 2011-2022 走看看