How Many Answers Are Wrong -- 并查集

题目:

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.
Boring~~Boring~~a 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


题目分析:

给出一个的总长度为n的区间1-n,然后给出m次回答,每次回答有一个初始点和一个结束点,还有一个区间和,区间和代表者区间内的数的求和。这些回答中会有矛盾,如果后面出现的回答与前面的回答发生冲突,那就是错误的回答,需要我们做的是找有多少错误的回答。

思路:

定义两个数组,一个ar[n],每个dst[i]指向它的区间左端点,另一个dst[n],每个dst[i]代表举例它区间左端点的长度。每次输入的时候,判断是否矛盾,然后更新左端点和距离,最后输出错误答案的个数就ok。


代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int ar[200005];
int dst[200005];

void init(int n)
{
    for(int i = 0; i <= n; i++)
        ar[i] = i;
}

int find_boss(int x)
{
    int j = ar[x];
    if(ar[x] != x)
    {
        ar[x] = find_boss(ar[x]);
        dst[x] = dst[x] + dst[j];
    }
    return ar[x];
}

int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        memset(dst, 0, sizeof(dst));
        init(n);
        int a, b, len, cnt = 0;
        while(m--)
        {
            cin >> a >> b >> len;
            a--;
            int aa = find_boss(a);
            int bb = find_boss(b);
            if(aa != bb)
            {
                ar[bb] = aa;
                dst[bb] = dst[a] + len - dst[b];
            }
            if(dst[b] - dst[a] != len && aa == bb)
                cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

有两点要说的:

1.在查找函数中我加入了一个dst[x] = dst[x] + dst[j];这一句话的作用是把当前节点到它最终父节点的距离算出来。

2.还有一个是我把左端点减了1,因为我在计算dst[b] - dst[a]时求出来的是前b个数减去前a个数剩下的数的和即:a+1到b的和,不是a到b,所以要a减一。还有个反例,是6 6 1,出现这组数据时,明显这个6是到本身,而且6和6的父节点相同,但是在最后一个if中却是6-6,这是计算了0个数,求和永远都是0,不可能与len相同,必定出错,因此把左端点减一是很有必要的,这样6 6 1变成5 6 1,计算的是前六个数减前五个数剩下的数的和就是第六个数是1,可以符合要求。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值