HDU-3038-How Many Answers Are Wrong [带权并查集]
来源:互联网 发布:矩阵理论答案 编辑:程序博客网 时间:2024/06/06 09:37
题目传送门
题意:
每输入一行x y s,表示[x,y]的和为s,求有多少行与前面的有冲突。
思路:
可以理解为y比x-1大s,就可以用带权并查集做。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>#include <queue>using namespace std;int N,M;int fa[200000+100], sum[200000+100];void init(){ for (int i = 0; i < 200000; i++) { fa[i]=i; sum[i]=0; } return ;}int find(int x){ if (x==fa[x])return x; int t = fa[x]; fa[x] = find(fa[x]); sum[x] += sum[t]; return fa[x];}int main(void){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while (~scanf("%d %d", &N, &M)) { init(); int ans = 0; while (M--) { int x, y, s; scanf("%d %d %d", &x, &y, &s); x--; int a = find(x); int b = find(y); if (a==b && sum[x]!=sum[y]+s) { ans++; } else { if (a>b) { fa[b] = a; sum[b] = sum[x]-s-sum[y]; } else { fa[a] = b; sum[a] = sum[y]+s-sum[x]; } } } 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带权并查集
- 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 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(带权并查集)
- HDU 3038 How Many Answers Are Wrong (带权并查集)
- hdu-3038-How Many Answers Are Wrong-带权并查集
- java实现多线程有两种方法:1、集成Thread类、2、实现Runnable接口
- BLE 4.2的新功能加强了隐私和安全,解决了早期BLE的缺陷并且提高了能效
- IOS中使用iframe无法滚动解决办法
- 单词搜索 II-LintCode
- 【Linux】红帽7搭建本地YUM源
- HDU-3038-How Many Answers Are Wrong [带权并查集]
- 多目标优化问题中常见分解方法的理解
- hdu 4568 (状压dp TSP问题)
- 链表面试题之链表相交问题
- 机器学习导论
- HDU 1003 Max Sum
- 逆元粗解
- 图像差分的方法
- 接受图片流并且生成图片