You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.3 KiB

2 years ago
#include <bits/stdc++.h>
// http://acm.hdu.edu.cn/showproblem.php?pid=3038
// https://blog.csdn.net/sunmaoxiang/article/details/80959300
// https://blog.csdn.net/qq_41552508/article/details/88636637
// https://yiqzq.blog.csdn.net/article/details/80152561
/**
: ,,dxyxy;
value[x]x,();
x, y, , , [x, y](x-1, y][x, y+1);
()......
valuei1i
ab[1,3],[3,7],value[3].
(1,3],(3,7]
[a,b] b a-1
ab a-1 b
*/
/**
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
1
*/
using namespace std;
const int N = 200010;
int fa[N]; //并查集数组
int value[N]; //用一个value[]数组保存从某点到其根节点距离(权值)
int n;//区间[1,n]
int m;//下面有m组数据
int ans;
//路径压缩+修改权值 [带权并查集路径压缩模板]
int find(int x) {
if (x == fa[x])
return x;
else {
int t = fa[x]; //记录原父节点编号
fa[x] = find(fa[x]); //父节点变为根节点此时value[x]=父节点到根节点的权值
value[x] += value[t]; //当前节点的权值加上原本父节点的权值
return fa[x];
}
}
int main() {
//读入,初始化
while (cin >> n >> m) {
for (int i = 0; i <= n; i++) {
fa[i] = i;
value[i] = 0;
}
//输入m组数据
while (m--) {
int l, r, dis;
cin >> l >> r >> dis; //l r v表示[l,r]区间和为v
l--; //这里我们用的是不包括前面的数
int RootA = find(l);
int RootB = find(r);
if (RootA == RootB) {
if (value[l] - value[r] != dis) ans++; //每输入一组数据,判断此组条件是否与前面冲突,输出冲突的数据的个数
} else {
fa[RootA] = RootB;
value[RootA] = -value[l] + value[r] + dis; //并查集的权值计算
}
}
cout << ans << endl;
}
return 0;
}