-
Notifications
You must be signed in to change notification settings - Fork 3
/
1697.cpp
67 lines (64 loc) · 1.88 KB
/
1697.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
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
public:
DisjointSet(int size) {
parent.resize(size, 0);
rank.resize(size, 0);
for (int i = 0; i < size; ++i) parent[i] = i;
}
int find(int x) {
if (x != parent[x]) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void join(int x, int y) {
int xP = find(x);
int yP = find(y);
if (xP == yP) return;
if (rank[xP] > rank[yP]) {
parent[yP] = xP;
}
else if (rank[xP] < rank[yP]) {
parent[xP] = yP;
}
else {
parent[yP] = xP;
rank[xP]++;
}
}
bool isConnected(int x, int y) {
int xP = find(x);
int yP = find(y);
return xP == yP;
}
};
class Solution {
public:
static bool compare(const vector<int>& v1, const vector<int>& v2) {
return v1[2] < v2[2];
}
vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {
DisjointSet* disjointSet = new DisjointSet(n);
sort(edgeList.begin(), edgeList.end(), compare);
int querySize = queries.size();
for (int i = 0; i < querySize; ++i) {
queries[i].push_back(i);
}
sort(queries.begin(), queries.end(), compare);
vector<bool> res(querySize, false);
int edgeIdx = 0;
for (int i = 0; i < querySize; ++i) {
while (edgeIdx < edgeList.size() && edgeList[edgeIdx][2] < queries[i][2]) {
disjointSet->join(edgeList[edgeIdx][0], edgeList[edgeIdx][1]);
edgeIdx++;
}
int queryIdx = queries[i][3];
bool ans = disjointSet->isConnected(queries[i][0], queries[i][1]);
res[queryIdx] = ans;
}
return res;
}
};