-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedUnionFind.cpp
More file actions
59 lines (57 loc) · 1.72 KB
/
WeightedUnionFind.cpp
File metadata and controls
59 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
struct WeightedUnionFind {
int n;
vector<pair<int, int>> parent; // (node, weight)
WeightedUnionFind(int x) {
n = x;
parent.resize(n);
for (int i = 0; i < n; i ++) parent[i] = make_pair(i, 0);
}
pair<int, int> find(int x) {
if (parent[x].first == x) return parent[x];
auto res = find(parent[x].first);
return parent[x] = make_pair(res.first, parent[x].second + res.second);
}
bool same(int x, int y) {
return find(x).first == find(y).first;
}
void unite(int x, int y, int val) {
auto u = find(x);
auto v = find(y);
if (u.first == v.first) return;
parent[u.first] = make_pair(v.first, v.second - u.second + val);
}
int diff(int x, int y) {
return parent[x].second - parent[y].second;
}
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
WeightedUnionFind wuf(n);
for (int i = 0; i < m; i ++) {
int l, r, d;
scanf("%d %d %d", &l, &r, &d);
l --, r --;
if (wuf.same(l, r)) {
if (wuf.diff(l, r) != d) {
puts("No");
return 0;
}
} else {
wuf.unite(l, r, d);
}
}
puts("Yes");
return 0;
}