转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱花

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038

题目描述:某个无聊的骚年给他的女友(烧!)一系列三元组x,y,c,表示序列中ax+ax+1+...+ay=c。但是其中有一些是错误的,也就是与前面的信息出现了冲突,比如给了1,2,6和3,4,5接着却给了1,4,100。找出所有错误的三元组的数量

思路:正如一般带权并查集的方法。用par[i]记录父节点,d[i]记录与父节点的差值,如果x与y在不同的集合中则可以自由合并,如果在同一集合中那就有可能出现矛盾。判断d[y]-d[x]==c是否成立,成立的话自然只需无视,不成立的话就表明出现矛盾。注意Find路径压缩查找当中节点到根结点路径上的每一个点除了par要修改,d也要累计。另外,由于题中给的区间都是全闭的,会出现x=y的情况,为了方便起见,记录区间时变为左闭右开,即y++

#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 200000+10
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second int par[MAXN];
int d[MAXN];
int n,m; void init()
{
CLR(par,-);
CLR(d,);
} int find(int x) //压缩路径查找
{
int s,tot=;
for(s=x;par[s]>=;s=par[s])
tot+=d[s];
while(s!=x)
{
int temp=par[x];
par[x]=s;
int tmp=d[x];
d[x]=tot;
tot-=tmp;
x=temp;
}
return s;
} int main()
{_
while(~scanf("%d%d",&n,&m))
{
init();
int tot=;
REP(i,m)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
y++;
int r1=find(x),r2=find(y);
if(r1!=r2)
{
par[r2]=r1;
d[r2]=d[x]-d[y]+c;
}
else
if(d[y]-d[x]!=c)
tot++;
}
printf("%d\n",tot);
}
return ;
}

hdu3038 How Many Answers Are Wrong【基础种类并查集】的更多相关文章

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

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

  2. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  3. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  4. HDU3038 How Many Answers Are Wrong[带权并查集]

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. poj2492 A Bug's Life【基础种类并查集】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  6. hdu1829&&poj2492 A Bug's Life 基础种类并查集

    把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...

  7. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

  8. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  9. hdu3038(种类并查集,推荐)

    题目大意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的?? 很有意思的一道题目,要是没有做过种类并查集,我肯定会以为这种题目是线段树题目...... 思路:我 ...

随机推荐

  1. Java开发工具IntelliJ IDEA单元测试和代码覆盖率图解

    原文 http://www.cnblogs.com/xiongmaopanda/p/3314660.html Java开发工具IntelliJ IDEA使用教程:单元测试和代码覆盖率 本文将展示如何使 ...

  2. WPF 路径和几何图形

    原文 http://www.cnblogs.com/laoyang999/archive/2012/11/23/2783734.html 继承自Shap类的形状有:Rectangle.Ellipse. ...

  3. WF编译报错

    最近在研究WF的时候,遇到了一个未知的错误,错误信息时这样的 错误 102 扩展“Microsoft.Activities.Build.Validation.ValidationBuildExtens ...

  4. Spring、Bean的生命周期

    1.默认情况下,在Bean容器被实例化的时候,bean对象将被创建: public class PersonServiceImpl implements PersonIService { public ...

  5. @Transactional 注解说明

    先让我们看代码吧! 以下代码为在"Spring3事务管理--基于tx/aop命名空间的配置"基础上修改.首先修改applicationContext.xml如下: <pre ...

  6. js计算时间差,包括计算,天,时,分,秒

    收集两个计算时间差的计算方法代码片段: var date1=new Date(); //开始时间 var date2=new Date(); //结束时间 var date3=date2.getTim ...

  7. javascript实现小九九乘法口诀

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  8. iOS 开发常用设置

    1. iOS设置app应用程序文件共享 设置流程 xcode 打开项目----在 info.plist 文件,添加 UIFileSharingEnabled 并设置属性为 YES, 在app内部,将您 ...

  9. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  10. 大数据Lambda架构

    1 Lambda架构介绍 Lambda架构划分为三层.各自是批处理层,服务层,和加速层.终于实现的效果,能够使用以下的表达式来说明. query = function(alldata) 1.1 批处理 ...