zoukankan      html  css  js  c++  java
  • 带权并查集

    就说两比较有代表性的题。

    食物链 POJ 1182

    动物王国中有三类动物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

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int pre[100005], rela[100005];
     5 
     6 int find(int x)
     7 {
     8     if(x==pre[x]) return x;
     9     else
    10     {
    11         int t=pre[x];
    12         pre[x] = find(pre[x]);
    13         rela[x] = (rela[x]+rela[t]) % 3;
    14         return pre[x];
    15     }
    16 }
    17 
    18 int main()
    19 {
    20     int n, k, i, d, x, y, fx, fy;
    21     int re;
    22     re = 0;
    23     scanf("%d %d", &n, &k);
    24     for(i=1;i<=k;i++)
    25     {
    26         pre[i] = i;
    27     }
    28 
    29     while(k--)
    30     {
    31         scanf("%d %d %d", &d, &x, &y);
    32         if((x==y&&d==2)||x>n||y>n) re++;
    33         else
    34         {
    35             fx = find(x);
    36             fy = find(y);
    37             if(fx==fy)
    38             {
    39                 if(d-1!=(rela[x]-rela[y]+3)%3) re++;
    40             }
    41             else
    42             {
    43                 pre[fx] = fy;
    44                 rela[fx] = (rela[y] - rela[x] + d - 1 )%3;
    45             }
    46         }
    47     }
    48     printf("%d
    ", re);
    49     return 0;
    50 }

    How Many Answers Are Wrong HDU 3088

    TT and FF are … friends. Uh… very very good friends -________-b

    FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).

    Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF’s question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

    BoringBoringa very very boring game!!! TT doesn’t want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

    The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

    However, TT is a nice and lovely girl. She doesn’t have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

    What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

    But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
    Input
    Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

    Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.

    You can assume that any sum of subsequence is fit in 32-bit integer.
    Output
    A single line with a integer denotes how many answers are wrong.
    Sample Input
    10 5
    1 10 100
    7 10 28
    1 3 32
    4 6 41
    6 6 1
    Sample Output
    1

    题意大概就是,有一堆数,已知多组从谁到谁这之间的所有数的和。让判断他所说的有多少是错的

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 
     5 long long pre[200000], rela[200000];
     6 long long re;
     7 
     8 long long find(long long x)
     9 {
    10     if(x!=pre[x])
    11     {
    12         int t = pre[x];
    13         pre[x] = find(pre[x]);
    14         rela[x] += rela[t];
    15         return pre[x];
    16     }
    17     else
    18     {
    19         return x;
    20     }
    21 }
    22 
    23 void unite(long long u, long long v, long long w)
    24 {
    25     int fu = find(u);
    26     int fv = find(v);
    27     if(fu!=fv)
    28     {
    29         pre[fv] = fu;
    30         rela[fv] = rela[u] - rela[v] + w;
    31     }
    32     else
    33     {
    34         if(w!=rela[v] - rela[u]) re++;
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     long long n, m, i, u, v, w;
    41     while(~scanf("%d %d", &n, &m))
    42     {
    43         re = 0;
    44         for(i=1; i<=n; i++)
    45         {
    46             pre[i] = i;
    47             rela[i] = 0;
    48         }
    49         while(m--)
    50         {
    51             scanf("%lld %lld %lld", &u, &v, &w);
    52             u--;
    53             unite(u, v, w);
    54         }
    55         printf("%lld
    ", re);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    HDU 4893 线段树
    Catalan数推导(转载)
    URAL 1992
    小乐乐吃糖豆
    排列组合问题总结
    G
    F
    C
    D
    B
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11356338.html
Copyright © 2011-2022 走看看