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
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)
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.
hdu3038判断区间谎言(带权并查集)的更多相关文章
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
- 种类并查集——带权并查集——POJ1182;HDU3038
POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 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 ...
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- BZOJ 1202: [HNOI2005]狡猾的商人 [带权并查集]
题意: 给出m个区间和,询问是否有区间和和之前给出的矛盾 NOIp之前做过hdu3038..... 带权并查集维护到根的权值和,向左合并 #include <iostream> #incl ...
随机推荐
- intellij idea 设置Live Template快速生成自定义代码块
一.设置 类似于宏,话不多少,上步骤 File----->Setting 选择Live Template 新建触发规则 新建触发key 输入模版text 选择在哪个环境触发 选java 如果是其 ...
- 全局事务/分布式事务 (Global Transaction/ A distributed transaction)之我见
这里参考的是Oracle对于XA的支持,其他的应该雷同吧... 1个分布式事务由多个行为在不同的数据库上执行,1个分布式事务的执行成功意味着相关数据库上的行为执行均成功.“XA协定”(http://w ...
- 使用ffmpeg转换视频格式
命令: ffmpeg -i infile -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 6 -y outfile 说明: ...
- 线段树教做人系列(3) HDU 4913
题意及思路看这篇博客就行了,讲得很详细. 下面是我自己的理解: 如果只有2,没有3的话,做法就很简单了,只需要对数组排个序,然后从小到大枚举最大的那个数.那么它对答案的贡献为(假设这个数排序后的位置是 ...
- (转)typedef和#define的用法与区别
typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...
- BMP是可以保存alpha通道的。
一直以来都不太熟悉BMP格式,今天保存图片的时候发现bmp是可以保存透明通道的,突然想起来以前下载的一些游戏素材贴图里面就有一些bmp格式的贴图.记录一下.
- Linux minicom命令
一.简介 minicom是Linux下的超级终端,与Windows下的超级终端功能相似,可以通过串口控制外部的硬件设备,可以用于管理和控制嵌入式设备 二.安装 1)yum安装 yum install ...
- c++ 子类切勿重新定义父类 non-virtual函数
子类如果重新定义了父类的non-virtual函数则不会有多态效果. 为方便描述,这里的方法和继承方式都用public.至于原因嘛,你懂的! 1:子类直接继承父类方法 class BaseOption ...
- rpm bug
rpm无法安装 今天安装java时候,下载的rpm的包,但是用linux打开时候发现不能正常打开. 错误详情 主要是rpm安装时候的错误,java提示的错误就没有记录了. Reading packag ...
- excel导出工具
介绍 excel导出工具 整个项目的代码结构如下 com \---utils +---demo # 案例相关 | | ExcelExportApplication.java # springboot启 ...