-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1697-checking-existence-of-edge-length-limited-paths.cpp
88 lines (79 loc) · 2.74 KB
/
1697-checking-existence-of-edge-length-limited-paths.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// GETS TLE
/*
class Solution {
public:
vector<bool> distanceLimitedPathsExist(int n,
vector<vector<int>>& edges,
vector<vector<int>>& queries) {
unordered_map<int, vector<pair<int, int>>> g;
for (auto edge: edges) {
auto from = edge[0], to = edge[1], cost = edge[2];
g[from].emplace_back(to, cost),
g[to].emplace_back(from, cost);
}
vector<bool> visited(n, false);
function<bool(int, int, int)> go = [&](auto from, auto to, auto limit) {
if (visited[from]) return false;
if (from == to) return true;
visited[from] = true;
for (auto dests: g[from]) {
auto dest = dests.first, weight = dests.second;
if (weight >= limit) continue;
if (go(dest, to, limit)) return true;
}
return false;
};
vector<bool> res;
for (auto query: queries) {
auto from = query[0], to = query[1], limit = query[2];
visited = vector<bool>(n, false);
auto is_possible = go(from, to, limit);
res.push_back(is_possible);
}
return res;
}
};
*/
static class DSU {
vector<int> Parent, Rank;
public:
DSU(int n) {
Parent.resize(n);
Rank.resize(n, 0);
for (int i = 0; i < n; i++) Parent[i] = i;
}
int Find(int x) {
return Parent[x] = Parent[x] == x ? x : Find(Parent[x]);
}
bool Union(int x, int y) {
int xset = Find(x), yset = Find(y);
if (xset != yset) {
Rank[xset] < Rank[yset] ? Parent[xset] = yset : Parent[yset] = xset;
Rank[xset] += Rank[xset] == Rank[yset];
return true;
}
return false;
}
};
class Solution {
public:
vector<bool> distanceLimitedPathsExist(int n,
vector<vector<int>>& edgeList,
vector<vector<int>>& queries) {
DSU dsu(n);
for (int i = 0; i < queries.size(); i++)
queries[i].push_back(i);
sort(queries.begin(), queries.end(),
[](auto &l, auto &r) { return l[2] < r[2]; });
sort(edgeList.begin(), edgeList.end(),
[](auto &l, auto &r) { return l.back() < r.back(); });
int i = 0;
vector<bool> result(queries.size());
for (vector<int> &q:queries) {
while (i < edgeList.size() && edgeList[i][2] < q[2])
dsu.Union(edgeList[i][0],edgeList[i++][1]);
result[q.back()] = dsu.Find(q[0]) == dsu.Find(q[1]);
}
return result;
}
};