hdu 3038(带权并查集)

原文链接:https://blog.csdn.net/yjr3426619/article/details/82315133 

http://acm.hdu.edu.cn/showproblem.php?pid=3038

How Many Answers Are Wrong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25513    Accepted Submission(s): 8618


 

Problem 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.

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
 

有M个数,不知道它们具体的值,但是知道某两个数之间(包括这两个数)的所有数之和,现在给出N个这样的区间和信息,需要判断有多少个这样的区间和与前边已知的区间和存在矛盾。例如给出区间和[1,4]为20,[3,4]为15,再给出[1,2]为30,显然这个[1,2]的值就有问题,它应该为20-15=5。

这个题目需要分析什么时候会产生矛盾,不妨就具体看看上面的这个矛盾的例子

在这里这个图是画不了的,因为使用的都是闭区间,闭区间会加上端点的值,这就提醒我们这个题在处理的时候应该将闭区间的某一端变成开区间,比如将[1,4]变成(0,4],将[3,4]变成(2,4],将[1,2]变成(0,2]

如果已知(2,4],(0,4],那么(0,2]的值就可以求出来,如果说再给出(0,2]的值,而又不与(0,2]求出来的值相等,就产生了矛盾,这是矛盾产生的唯一情况:某个区间的值可以由前边的区间求出来,而又给出了这个区间错误的值。注意,有的地方对这个题的分析存在很大的误解,有的博客中说如果已知[1,10]为100,[1,5]为1000,[1,5]的值不可能大于100所以矛盾,这是不对的,因为本题题目中没有说所有的数都是正数,因此矛盾只有在不相等的时候产生。

那么需要考虑什么时候一个区间的值可以由之前的区间求出来呢?观察上边这张图,红色的这条边是求不出来的,而绿色的是可以求出来的,为什么呢?因为0、2属于同一个并查集,0、3不属于同一个并查集,因此不难得出本题的求解思路:如果给定区间的两个端点属于同一个并查集,判断这个区间的值是否与计算得到的值相等;如果给定区间的两个端点不属于同一个并查集,将这两个并查集合并。并查集边的权值就等于题干中的区间和。

 
#include<iostream>

using namespace std;

const int N = 200000 + 10;

int dis[N]; //dis[i] 为 i 到 find(i) 的距离 (i+1) + ....+(find(i))
int p[N];

int find(int x)
{
	if (x != p[x])
	{
		int y = p[x];
		p[x] = find(p[x]);
		dis[x] += dis[y];
	}
	return p[x];
}

int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		for (int i = 0; i <= n; i++)
		{
			dis[i] = 0;
			p[i] = i;
		}
		int ans = 0;
		while (m--)
		{
			int l, r, w;
			cin >> l >> r >> w;
			l--;
			int pl = find(l);
			int pr = find(r);
			if (pl == pr)
			{
				if (dis[l] - dis[r] != w) ans++;
			}
			else
			{
				p[pl] = pr;
				dis[pl] = dis[r] + w - dis[l];
			}
		}
		cout << ans << endl;
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值