hdu3038判断区间谎言(带权并查集)

题目传送门

题目描述:给你n,m,n代表从1到n这么大的数组,m组v,u,val,代表v到u这个区间的总和是val,然后让你判断m组关系中有几组是错误的。

思路:带权并查集,这道题其实算是让我知道什么是真正的带权并查集吧,之前有一道食物链的题目非常经典但是一直没完全理解,其实带权并查集就是除了表达自己和父节点这个true的关系(fa[x])==y,意思就是,x->y true),还表达了自己和父节点的某一个权值关系。这个思想是看了大牛的博客

点击打开链接  此处膜大牛。

其实带权并查集主要就是两个集合父节点的关系的更新,用了向量的思想,比如A的父节点是B,C的父节点是D,则A、B与父节点的权值表示为AB,CD,然后现在给你一组AC,也就是把AC两个节点放到一个集合里,那A、C的父节点B、D就也在一个集合里了,如果此时总爸爸是D,那B和D之间应该也有一个“BD”这样的关系,BD=BA+AC+CD=-AB+AC+CD.这就是向量。

而这道题u v区间要先表达成 u-1,v这样一个区间,比如告诉了你1-10的总和,现在判断1-4 和5-10 ,其实真正判断是(0,4】+(4,10】这样一个区间和是不是与【1,10】相等。

上代码咯

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
const int maxn=200000+5;
int fa[maxn],w[maxn];
int find(int x)
{
	if(x==fa[x])return x;
	int tep=fa[x];
	fa[x]=find(fa[x]);//先更新父节点 和 父节点的 w 
	w[x]+=w[tep];//当前节点到父节点的关系+父节点到总节点的关系=当前节点到总节点的关系 
	return fa[x];
}
int main(){
	int n,m;
	while(cin>>n>>m){
	int ans=0;
	for(int i=0;i<=n;i++)
	{
		fa[i]=i;
		w[i]=0;
	}
	while(m--)
	{
		int u,v,val;
		scanf("%d%d%d",&u,&v,&val); 
		u--;//val表达的是区间值   所以是【u-1,v】 
		int x=find(u);
		int y=find(v);
		if(x!=y)
		{
			fa[x]=y;
			w[x]=w[v]+val-w[u];//向量相加原则 
		}else{
			if(w[u]-w[v]!=val)ans++;//这里的加减要注意  看是谁认谁做爸爸 
		} 
	}
	cout<<ans<<endl;
}
}

How Many Answers Are Wrong

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


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 51 10 1007 10 281 3 324 6 416 6 1
 

Sample Output
1

原文地址:https://www.cnblogs.com/mountaink/p/9536717.html