HDU3038-并查集

How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17051 Accepted Submission(s): 5954

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.

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

题意:两个人玩游戏,有N个数 和 M个问题和回答,M个问题里面有[L,R]和这个区间的和S。问这样的区间和有多少个是错的
样例:1-10 == 100, 1-3 = 32 + 4-6 = 41 + 7-10 = 28 == 101 这个区间是1-10 和之前给的值不符,所以输出1
思路:一开始我觉得和poj的食物链很像,因为都是求错误的个数。。。但是不知道怎么做,后面去看了一下大佬的博客才知道也可以用向量来做。首先我们要用一个数组dis[]来储存X到父节点的距离(可以理解成[X,pre[X]]的区间和)
比如我们把[X,Y]的和S合并,我们画一个图

X
rootX
Y
rootY

rootX是X的父节点rootY是Y的父节点,这样我们可以得出X,Y合并就是
rootX—>rootY == X->Y + Y -> rootY - X->rootX
(不过我们要记得X-1,因为区间和是包括了X的)
这是合并,当我们遇到rootX==rootY的时候我们再画一个图:

X
root
Y

我们要判断这个X–>Y是不是相等的。现在我们知道X–>Y == S
只需要判断X–>root - Y–>root == S是不是就行了。
AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=200000+20;
long long int pre[maxn];
long long int dis[maxn];//当前节点到根节点的距离
int ans,n,m;			 
int find(int x)
{
	if (x!=pre[x])  //在这里找父节点的时候记得也要修改一下dis
		{
			int t=pre[x];
			pre[x]=find(pre[x]);
			dis[x]=dis[x]+dis[t];
		}
	return pre[x];
}
void Init()
{
	ans=0;
	for (int i=0;i<=n;i++)
		{
			pre[i]=i;
			dis[i]=0;
		}
	return ;
}
void unite(int x,int y,int z)
{
	int a=find(x);
	int b=find(y);
	if (a==b)
		{
			if (dis[x]-dis[y]!=z)  //相同的root判断
				ans++;
			return ;
		}
	pre[a]=b;
	dis[a]=-dis[x]+dis[y]+z;
	return ;
}
int main()
{
	int start,end,value;
	while (cin>>n>>m)
		{
			Init();
			while (m--)
				{
					scanf("%d%d%d",&start,&end,&value);
					start--;
					unite(start,end,value);
				}
			cout<<ans<<endl;
		}
	
	return 0;
}
发布了46 篇原创文章 · 获赞 2 · 访问量 3212

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/86626852