HDU-3038 How Many Answers Are Wrong 并查集
来源:互联网 发布:linux curl命令详解 编辑:程序博客网 时间:2024/05/22 15:23
题意 : 给你m个区间的区间和,让你判断有多少个答案是错误的。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int maxn = 2e5 + 10;int sum[maxn] = {0};int p[maxn] = {0};int find (int x) { if (x == p[x]) return x; int root = find (p[x]); sum[x] += sum[p[x]]; p[x] = root; return root;}int ans = 0;int main () { ios_base :: sync_with_stdio(false); int n,m; while (cin >> n >> m){ for (int i = 0;i <= n; ++ i) p[i] = i,sum[i] = 0; ans = 0; for (int i = 0;i < m; ++ i) { int l,r,val; cin >> l >> r >> val; --l; int pl = find (l); int pr = find (r); if (pl != pr) { p[pr] = pl; sum[pr] = sum[l] - sum[r] + val; } else if (sum[r] - sum[l] != val) ans ++; } cout << ans << endl; } return 0;}
。每一个集合中每个数的权值是这个点的前缀和减去根结点的前缀和。每次合并集合的时候只需要把一个集合的根节点的值设置为这个根节点的值减去要合并到根节点的值,然后将两个集合合并即可。
阅读全文
1 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 并查集
- hdoj 3038How Many Answers Are Wrong 并查集
- HDOJ 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【并查集的简单应用】
- 直播点赞动画实现
- 欢迎使用CSDN-markdown编辑器
- Mysql 根据时间戳按年月日分组统计
- ORACLE数据库事务隔离级别
- typescript 中通过构造函数的参数直接定义属性
- HDU-3038 How Many Answers Are Wrong 并查集
- 一维搜索方法
- 利用@value获取配置文件的信息
- hdu6166 最短路
- 华为eNSP模拟器中两个路由器IP互联
- springMVC记录(二)
- WindowBuilder1.9Release版插件安装
- LaTex在新详情的表现测试
- Java NIO系列教程(一) Java NIO 概述