-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
69 lines (62 loc) · 1.46 KB
/
Solution.cpp
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
60
61
62
63
64
65
66
67
68
69
class UnionFind {
public:
UnionFind(int n) {
p = vector<int>(n);
size = vector<int>(n, 1);
iota(p.begin(), p.end(), 0);
}
bool unite(int a, int b) {
int pa = find(a), pb = find(b);
if (pa == pb) {
return false;
}
if (size[pa] > size[pb]) {
p[pb] = pa;
size[pa] += size[pb];
} else {
p[pa] = pb;
size[pb] += size[pa];
}
return true;
}
int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
int getSize(int x) {
return size[find(x)];
}
private:
vector<int> p, size;
};
class Solution {
public:
vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
g = vector<int>(n, -1);
uf = new UnionFind(n);
for (auto& e : edges) {
uf->unite(e[0], e[1]);
}
for (auto& e : edges) {
int root = uf->find(e[0]);
g[root] &= e[2];
}
vector<int> ans;
for (auto& q : query) {
ans.push_back(f(q[0], q[1]));
}
return ans;
}
private:
UnionFind* uf;
vector<int> g;
int f(int u, int v) {
if (u == v) {
return 0;
}
int a = uf->find(u), b = uf->find(v);
return a == b ? g[a] : -1;
}
};