zoukankan      html  css  js  c++  java
  • 【并查集】PKU-1182 食物链

    食物链

    Description

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。
    现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
    有人用两种说法对这N个动物所构成的食物链关系进行描述:
    第一种说法是"1 X Y",表示X和Y是同类。
    第二种说法是"2 X Y",表示X吃Y。
    此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
    1) 当前的话与前面的某些真的话冲突,就是假话;
    2) 当前的话中X或Y比N大,就是假话;
    3) 当前的话表示X吃X,就是假话。
    你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。

    Input

    第一行是两个整数N和K,以一个空格分隔。
    以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
    若D=1,则表示X和Y是同类。
    若D=2,则表示X吃Y。

    Output

    只有一个整数,表示假话的数目。

    Sample Input

    100 7
    1 101 1 
    2 1 2
    2 2 3 
    2 3 3 
    1 1 3 
    2 3 1 
    1 5 5
    

    Sample Output

    3


    算法思路:

    在原有parent[]的基础上加上另一关系relative[x],用于表示动物x相对于其根节点的关系:

    1. 如果relative[x]=0, 说明x和根节点的关系是同类
    2. 如果relative[x]=1, 说明x和根节点的关系是x吃根节点
    3. 如果relative[x]=2, 说明x被根节点吃。

    对于偏移量的计算,分为两种情况:1、x,y在同一集合里(即根结点相同),此时判断真假即可;2、x,y在不同集合里,此时应把x,y归到同一集合下;

    还不明白,可以参考这个有耐心的大神http://blog.sina.com.cn/s/blog_626633790100ut9j.html

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<list>
     5 using namespace std;
     6 const int maxn = 50010;
     7 int ans;
     8 struct Animal
     9 {
    10     int relative; /*relative = 0, 相对于根结点是同类;
    11                     relative = 1, 相对于根结点是吃;
    12                     relative = 2, 相对于根结点是被吃;*/
    13     int parent;
    14 }animal[maxn];
    15 void MakeSet(int SizeOfSet)
    16 {
    17     for(int i = 0; i <= SizeOfSet; i++)
    18     {
    19         animal[i].parent = i;
    20         animal[i].relative = 0;
    21     }
    22 }
    23 int Find(int x)
    24 {
    25     int r = x;
    26     if(animal[r].parent == r)   return r;
    27     int tmp = animal[x].parent;
    28     animal[x].parent = Find(tmp);
    29     animal[x].relative = (animal[x].relative + animal[tmp].relative) % 3;
    30     return animal[x].parent;
    31 }
    32 void Solve(int d, int x, int y, int n)
    33 {
    34     if((d == 2 && x == y) || x > n || y > n) {ans++; return;}
    35     int rootx = Find(x), rooty = Find(y);
    36     if(rootx == rooty)//结点x, y在一个集合里
    37     {
    38         if(d == 1)
    39         {
    40             if(animal[x].relative != animal[y].relative) ans++;
    41         }
    42         else if(d == 2)
    43         {
    44             if((animal[x].relative + 1) % 3 != animal[y].relative)  ans++;
    45         }
    46     }
    47     else
    48     {
    49         animal[rooty].parent = rootx;
    50         if(d == 1)
    51         {
    52             animal[rooty].relative = (animal[x].relative - animal[y].relative + 3) % 3;
    53         }
    54         else if(d == 2)
    55         {
    56             animal[rooty].relative = ((animal[x].relative + 1) % 3 - animal[y].relative + 3) % 3;
    57         }
    58     }
    59 }
    60 int main()
    61 {
    62     int n, k;
    63     scanf("%d%d", &n, &k);
    64     MakeSet(n);
    65     while(k--)
    66     {
    67         int d, x, y;
    68         scanf("%d%d%d", &d, &x, &y);
    69         Solve(d, x, y, n);
    70     }
    71     printf("%d
    ", ans);
    72     return 0;
    73 }
  • 相关阅读:
    HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式
    centos7使用docker安装es(elasticsearch)
    springboot发送邮件
    Springboot在工具类(Util)中使用@Autowired注入Service
    Spring Boot应用程序启动器
    ssh端口转发功能
    小型自动化运维工具pssh和传输工具rsync
    用linux主机做网关搞源地址转换(snat)
    shell脚本判断里面的字符含义
    heartbeat高可用
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4256117.html
Copyright © 2011-2022 走看看