1
/ Standard DSU Class class DSU {2
vector<int> parent, size;6
for (int i = 0; i <= n; i++) {12
int findParent(int num) {13
if (parent[num] == num) return num;14
return parent[num] = findParent(parent[num]);17
// Directly getting parents of u and v18
// To avoid finding parent multiple times19
void unionBySize(int parU, int parV) {20
if (size[parU] < size[parV]) {21
size[parV] += size[parU];24
size[parU] += size[parV];32
vector<bool> friendRequests(int n, vector<vector<int>> &restrictions,33
vector<vector<int>> &requests) {36
vector<bool> successful;38
for (auto &request : requests) {39
int u = request[0], v = request[1];41
int parU = dsu.findParent(u), parV = dsu.findParent(v);46
// Check if current friend requested is restricted or not.47
for (auto &restriction : restrictions) {48
int restricted_U = restriction[0], restricted_V = restriction[1];50
int restricted_parU = dsu.findParent(restricted_U);51
int restricted_parV = dsu.findParent(restricted_V);53
if ((parU == restricted_parU && parV == restricted_parV) ||54
(parU == restricted_parV && parV == restricted_parU)) {60
// Union u and v by passing parents61
// Since it is already calculated above63
dsu.unionBySize(parU, parV);67
successful.push_back(flag);