[HDU3038] How Many Answers Are Wrong - Weighted disjoint-set

description

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)

The meaning of problems

N and m give you a number of conditions, each condition tells you a range and ask how many conditions given is wrong (first condition is correct)

Thinking

I do not see is disjoint-set series.
Since this is not necessarily the number of n are positive, so that only a possible error conditions:
\ ([A, B] \) given and a \ ([a, x_1], [x_1 + 1, x_2], ..., [x_k + 1, b ] \) and given and the different.
So to use disjoint-set to maintain a \ (v_x \) represents the \ (X \) at this point and the root.
Each time a given condition \ (L, R & lt, X \) , if find(l-1) == r, the addition of the current interval \ ([l, r] \ ) can piece together the original some interval, then it is judged that a judgment \ (v [r ] -v [l-1] \ ) is equal to \ (X \) .
Otherwise combined \ (l-1 \) and \ (R & lt \) , updated v[find(r)]as v[l]+x-v[r]

L, R represent the root l, r of

Code

#include <cstdio>
const int maxn = 200000 + 10;
int n,m,fa[maxn],v[maxn],ans;
inline int find(int x) {
    if (fa[x] == x) return x;
    int tmp = fa[x];
    fa[x] = find(fa[x]);
    v[x] += v[tmp];
    return fa[x];
}
int main() {
    for (;scanf("%d%d",&n,&m) ^ EOF;ans = 0) {
        for (int i = 0;i <= n;i++) fa[i] = i,v[i] = 0;
        for (int l,r,x,a,b;m--;) {
            scanf("%d%d%d",&l,&r,&x); l--;
            a = find(l); b = find(r);
            if (a ^ b) {
                fa[b] = a;
                v[b] = v[l]+x-v[r];
            } else if (v[r]-v[l]^x) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrj124/p/11832514.html