-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathChecking Existence of Edge Length Limited Paths.cpp
53 lines (46 loc) · 1.82 KB
/
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
// Runtime: 390 ms (Top 98.27%) | Memory: 111.20 MB (Top 59.25%)
class Solution {
public:
vector<bool> distanceLimitedPathsExist(int length, vector<vector<int>>& adjList, vector<vector<int>>& queries) {
vector<int> parent(length);
vector<int> rank(length);
vector<int> weight(length);
for (int i = 0; i < length ; i++) parent[i] = i;
sort(adjList.begin(), adjList.end(), [](const vector<int>& a, const vector<int>& b) {
return a[2] < b[2];
});
for (vector<int>& edge : adjList) unionByRank(edge[0], edge[1], edge[2], parent, rank, weight);
vector<bool> answer;
for (vector<int>& query : queries)
answer.push_back(isConnectedAndWithinLimit(query[0], query[1], query[2], parent, weight));
return answer;
}
bool isConnectedAndWithinLimit(int p, int q, int limit, vector<int>& parent, vector<int>& weight) {
return find(p, limit, parent, weight) == find(q, limit, parent, weight);
}
int find(int x, int limit, vector<int>& parent, vector<int>& weight) {
while (x != parent[x]) {
if (weight[x] >= limit) {
break;
}
x = parent[x];
}
return x;
}
void unionByRank(int x, int y, int limit, vector<int>& parent, vector<int>& rank, vector<int>& weight) {
int x_ref = find(x, INT_MAX, parent, weight);
int y_ref = find(y, INT_MAX, parent, weight);
if (x_ref != y_ref) {
if (rank[x_ref] < rank[y_ref]) {
parent[x_ref] = y_ref;
weight[x_ref] = limit;
} else {
parent[y_ref] = x_ref;
weight[y_ref] = limit;
if (rank[x_ref] == rank[y_ref]) {
rank[x_ref]++;
}
}
}
}
};