-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
33 lines (31 loc) · 1003 Bytes
/
Solution.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
class Solution {
public:
vector<int> p;
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
vector<bool> ans;
for (auto& req : requests) {
int u = req[0], v = req[1];
if (find(u) == find(v))
ans.push_back(true);
else {
bool valid = true;
for (auto& res : restrictions) {
int x = res[0], y = res[1];
if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x))) {
valid = false;
break;
}
}
ans.push_back(valid);
if (valid) p[find(u)] = find(v);
}
}
return ans;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};