hdu 3038 How Many Answers Are Wrong(并查集)
来源:互联网 发布:linux lcd 设备文件 编辑:程序博客网 时间:2024/05/24 15:43
题目大意:
给出m个询问,问【l,r】之间的和 ,求出有多少次询问不和之前的矛盾的。
思路分析:
用并查集记录当前节点到根节点的和。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define maxn 222222using namespace std;int set[maxn];int sum[maxn];int find(int x){ if(x!=set[x]) { int f=set[x]; set[x]=find(set[x]); sum[x]+=sum[f]; } return set[x];}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<=n;i++)set[i]=i,sum[i]=0; int ans=0; while(m--) { int l,r,s; scanf("%d%d%d",&l,&r,&s); l--; int xx=find(l); int yy=find(r); if(xx==yy) { if(sum[l]-sum[r]!=s)ans++; } else { set[xx]=yy; sum[xx]=sum[r]-sum[l]+s; } } printf("%d\n",ans); } return 0;}
0 0
- HDU 3038 How Many Answers Are Wrong(并查集)
- HDU 3038 How Many Answers Are Wrong - 并查集
- hdu 3038 - How Many Answers Are Wrong(并查集)
- hdu 3038 How Many Answers Are Wrong(并查集)
- 并查集 How Many Answers Are Wrong HDU 3038
- [HDU 3038]How Many Answers Are Wrong[并查集]
- hdu-3038-How Many Answers Are Wrong(并查集)
- 并查集-HDU-3038-How Many Answers Are Wrong
- HDU 3038 How Many Answers Are Wrong(并查集)
- 【HDU 3038 How Many Answers Are Wrong】+ 并查集
- HDU-3038 How Many Answers Are Wrong 并查集
- HDU 3038 How Many Answers Are Wrong (并查集)(需复习!)
- HDU 3038 how many answers are wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong (带权并查集+区间判断)
- hdu 3038 How Many Answers Are Wrong(并查集)
- HDU - 3038 How Many Answers Are Wrong (带权并查集)
- HDU 3038 How Many Answers Are Wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong (并查集)
- sql插入某些固定数据和其他子查询结果
- 享元模式
- 一篇%3CDIV%20style%3D%22FONT-SIZE%
- Java反射学习总结二(用反射调用对象的私有属性和方法)
- 关于Images.xcassets
- hdu 3038 How Many Answers Are Wrong(并查集)
- Android开发之手机拍照功能的实现(源代码分享)
- Unable to open log device ‘/dev/log/main’: No such file or directory
- vi的使用
- SSH+Oracle整合开发错误8 ,,,关于数据库的问题
- Linux学习笔记——vmware plarer中安装ubuntu
- 工厂模式
- equals和 hashcode的区别
- RUP