hdu3038How Many Answers Are Wrong(并查集)
来源:互联网 发布:淘宝开店买家付不了款 编辑:程序博客网 时间:2024/05/19 11:19
题意:给出一些区间[l,r]的和为S,问有多少个是矛盾的
思路:对给出的区间[l,r]将左边减一变成(l-1,r]合并,把数值小的当做根节点更新即可,有一点不懂的是只会用递归来更新,用while循环一直wa...
#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 200005#define LL long longint cas=1,T;int pre[maxn];int sum[maxn];int Find(int x){ if (x!=pre[x]){int r = pre[x];pre[x]=Find(pre[x]);sum[x]+=sum[r];}return pre[x];}int n;void init(){for (int i = 0;i<=maxn;i++){pre[i]=i; sum[i]=0;}}int main(){int m;while (scanf("%d%d",&n,&m)!=EOF){init();int ans = 0; for (int i = 0;i<m;i++){ int l,r,s;scanf("%d%d%d",&l,&r,&s);l--;int ra =Find(l),rb=Find(r);if (ra==rb){if (sum[r]-sum[l]!=s) ans++;}else{pre[rb]=ra;sum[rb]=sum[l]-sum[r]+s;}}printf("%d\n",ans);}//freopen("in","r",stdin);}
0 0
- hdu3038How Many Answers Are Wrong(并查集)
- HDU3038How Many Answers Are Wrong(带权并查集)
- hdu3038How Many Answers Are Wrong
- HDU3038How Many Answers Are Wrong
- HUD3038How Many Answers Are Wrong(并查集)经典
- How Many Answers Are Wrong(并查集)
- hdoj 3038How 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(并查集)
- hdu3038 How Many Answers Are Wrong --- 种类并查集
- hdu 3038 How Many Answers Are Wrong(并查集)
- Hdu3038 - How Many Answers Are Wrong - 并查集
- 并查集 How Many Answers Are Wrong HDU 3038
- HDOJ 3038 How Many Answers Are Wrong 并查集
- HDU3038 How Many Answers Are Wrong 【并查集】
- [HDU 3038]How Many Answers Are Wrong[并查集]
- How Many Answers Are Wrong 并查集
- OpenGL ES随手记——视图
- JS获取图片实际宽高及根据图片大小进行自适应
- github学习(2)
- Linux IPv6网络编程之UDP实例
- Xutls3的使用
- hdu3038How Many Answers Are Wrong(并查集)
- Android-内存优化-Overdraw-多度绘制
- PHP常量详解:define和const的区别
- 天声人語 20160131 1月の言葉から
- 【读书笔记】【Android 开发艺术探索】第11章Android 的线程和线程池
- 将本地jar包导入到maven local repository
- 弹出自适应图片大小的窗口弹出窗口根据图片大小,自动判断高和宽
- 安卓图片处理Picasso的解析使用
- 日经春秋 20160131