forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess Restricted Friend Requests.cpp
77 lines (59 loc) · 2.03 KB
/
Process Restricted Friend Requests.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
// Runtime: 361 ms (Top 82.03%) | Memory: 22.1 MB (Top 38.96%)
/ Standard DSU Class
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]);
}
// Directly getting parents of u and v
// To avoid finding parent multiple times
void unionBySize(int parU, int parV) {
if(size[parU] < size[parV]) {
size[parV] += size[parU];
parent[parU] = parV;
}
else {
size[parU] += size[parV];
parent[parV] = parU;
}
}
};
class Solution {
public:
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
DSU dsu(n);
vector<bool> successful;
for(auto& request : requests) {
int u = request[0], v = request[1];
int parU = dsu.findParent(u), parV = dsu.findParent(v);
bool flag = true;
if(parU != parV) {
// Check if current friend requested is restricted or not.
for(auto& restriction : restrictions) {
int restricted_U = restriction[0], restricted_V = restriction[1];
int restricted_parU = dsu.findParent(restricted_U);
int restricted_parV = dsu.findParent(restricted_V);
if((parU == restricted_parU && parV == restricted_parV) || (parU == restricted_parV && parV == restricted_parU)) {
flag = false;
break;
}
}
// Union u and v by passing parents
// Since it is already calculated above
if(flag) {
dsu.unionBySize(parU, parV);
}
}
successful.push_back(flag);
}
return successful;
}
};