-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
35 lines (35 loc) · 1.05 KB
/
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
34
35
class Solution {
public:
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
vector<int> p(n);
iota(p.begin(), p.end(), 0);
function<int(int)> find = [&](int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
};
vector<bool> ans;
for (auto& req : requests) {
int u = req[0], v = req[1];
int pu = find(u), pv = find(v);
if (pu == pv) {
ans.push_back(true);
} else {
bool ok = true;
for (auto& r : restrictions) {
int px = find(r[0]), py = find(r[1]);
if ((pu == px && pv == py) || (pu == py && pv == px)) {
ok = false;
break;
}
}
ans.push_back(ok);
if (ok) {
p[pu] = pv;
}
}
}
return ans;
}
};