题目传送门

题目描述:给你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

hdu3038判断区间谎言(带权并查集)的更多相关文章

  1. HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...

  2. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

  3. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

  4. POJ-1733 Parity game(带权并查集区间合并)

    http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...

  5. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  6. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  7. UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

  8. HDU3038 How Many Answers Are Wrong —— 带权并查集

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...

  9. BZOJ 1202: [HNOI2005]狡猾的商人 [带权并查集]

    题意: 给出m个区间和,询问是否有区间和和之前给出的矛盾 NOIp之前做过hdu3038..... 带权并查集维护到根的权值和,向左合并 #include <iostream> #incl ...

随机推荐

  1. AudioTrack

    AudioTrack 在Java应用中,管理和播放一个单一的语音资源 The AudioTrack class manages and plays a single audio resource fo ...

  2. 如何关闭打开了多个activity的activity

    专门建立一个类,内部有一个静态的linklist对象,用来记录打开的activity,如果该ACTIVITY没有被打开过,在每一个activity oncreate方法中将自己的实例加入这个list. ...

  3. 校园客户端(DR)启动后提示我们缺失packet.dll,无法正常启动(7)

    有的时候校园客户端(DR)启动后提示我们缺失packet.dll,无法正常启动,然而我们重装了客户端后任然不感冒,那么问题来了,问题解决不了往往源于我们对问题的本质不够了解,如果了解问题的本质,那么问 ...

  4. js实现无刷新上传

    在新增数据项的时候,用ajax实现无刷新提交,但上传文件的时候,由于数据类型原因,不能将页面的<asp:FileUpload>中以字符串值的方式传到js里调用.我一共找到了两个方法予以解决 ...

  5. Ubuntu 切换到桌面 快捷键设置

    设置完以上步骤后,这接windows系统键+d,即可切换到桌面. ps:按Alt+Tab键,可以切换到自己想要的图标进程.

  6. [译]Javascript timing事件

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  7. 并发编程学习笔记之可见性&过期数据(二)

    想要使用多线程编程,有一个很重要的前提,那就是必须保证操纵的是线程安全的类. 那么如何构建线程安全的类呢? 1. 使用同步来避免多个线程在同一时间访问同一数据. 2. 正确的共享和安全的发布对象,使多 ...

  8. Aspose.Words CookieBook

    Aspose.Words.dll  版本 13.1.0.0 [原创]Aspose.Words组件介绍及使用—基本介绍与DOM概述 插入分页符 DocumentBuilder builder = new ...

  9. Join方法把阵列转为字符串

    string有一个方法,就是Join,可以把阵列转换为字符串: 下面是Join方法重载,可以指定那个元素开始,连续取几个元素: 可以传入一个IEnumerable<T>参数:

  10. timestamp 在curl中变成了Xtamp

    目前的解决方案 将timestemp放在数组最前面. [注意:请求的参数中需要将timestamp这个参数放在数组的最前面,不然在GET方式请求中,会出现浏览器将它变成Xtamp,最终导致签名失败]