题目传送门

题目描述:给你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. mybaits中date类型显示时分秒(orcle数据库)

    <insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...

  2. WKWebView的15条应用指南

    1.让一个web view充满屏幕 有时候你会看到有人向viewDidLoad()中添加代码,创建一个web view并让它充满整个可用区域.但这样效率很低,用起来很麻烦. 一个简单的方法是在你的视图 ...

  3. 每天一道算法题(15)——打印1到最大的n位数

    题目: 打印1到最大的n位数.如n=4,打印1-9999. 思路: 由于直接使用循环会导致int或者long long都不够存储.因此使用字符串来存储数据,这里涉及到数字转换成字符串以及字符串的加法. ...

  4. Entity Framework Code-First(20):Migration

    Migration in Code-First: Entity framework Code-First had different database initialization strategie ...

  5. 数据结构_find_lucky_number(寻找幸运值)

    数据结构_find_lucky_number(寻找幸运值) 问题描述 给出两个已按升序排列的数组 a[1..n],b[1..m],如果存在 i,j,使得a[i]+b[j]==k,我们便说已找到幸运值. ...

  6. python 趣味强制请吃饭

    # -*- coding: utf-8 -*- import easygui who = easygui.buttonbox("你想请谁吃饭 ?", "luckly qu ...

  7. C#中IDisposable的用法-垃圾回收

    在Net中,由GC垃圾回收线程掌握对象资源的释放,程序员无法掌控析构函数的调用时机.对于一些非托管资源,比如数据库链接对象等,需要实现IDisposable接口进行手动的垃圾回收.那么什么时候使用Id ...

  8. Algorithms - Bucket sort

    印象 图1 将元素分布在桶中 图2 元素在每个桶中排序 思想 桶排序将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序). 分析 时间复杂度: ...

  9. symbol访问法及symbor注册表

    symbol主要作用是防止对象属性名冲突 在ES6之前,对象的属性名只能是字符串,这样很容易造成字符串的冲突. 例:假设person对象是从外部库引入的一个对象 let person = { name ...

  10. 用Apache James 3.3.0 搭建个人邮箱服务器

    准备域名 比如域名为example.net,则邮箱格式为test@example.net.在自己的域名管理界面,添加一条A记录(mail.example.net  xxx.xxx.xxx.xxx),指 ...