forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph Connectivity With Threshold.cpp
63 lines (52 loc) · 1.44 KB
/
Graph Connectivity With Threshold.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
// Runtime: 436 ms (Top 17.70%) | Memory: 66.6 MB (Top 47.85%)
// DSU Class Template
class DSU {
vector<int> parent, size;
public:
DSU(int n) {
for(int i=0; i<=n; i++) {
parent.push_back(i);
size.push_back(1);
}
}
int findParent(int num) {
if(parent[num] == num) return num;
return parent[num] = findParent(parent[num]);
}
void unionBySize(int u, int v) {
int parU = findParent(u);
int parV = findParent(v);
if(parU == parV) return;
if(size[parU] < size[parV]) {
parent[parU] = parV;
size[parV] += size[parU];
}
else {
parent[parV] = parU;
size[parU] += size[parV];
}
}
};
class Solution {
public:
vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) {
DSU dsu(n);
// Make connections with its multiple
for(int i=threshold+1; i<=n; i++) {
int j = 1;
while(i*j <= n) {
dsu.unionBySize(i, i*j);
j++;
}
}
vector<bool> res;
for(auto& query : queries) {
int u = query[0];
int v = query[1];
// Check if both cities belong to same component or not
if(dsu.findParent(u) == dsu.findParent(v)) res.push_back(true);
else res.push_back(false);
}
return res;
}
};