Skip to content

Commit e8a8419

Browse files
authored
Create 2076.Process-Restricted-Friend-Requests.cpp
1 parent ae2eaa1 commit e8a8419

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
vector<int>Father;
3+
int FindFather(int x)
4+
{
5+
if (Father[x]!=x)
6+
Father[x] = FindFather(Father[x]);
7+
return Father[x];
8+
}
9+
10+
void Union(int x, int y)
11+
{
12+
x = Father[x];
13+
y = Father[y];
14+
if (x<y) Father[y] = x;
15+
else Father[x] = y;
16+
}
17+
public:
18+
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests)
19+
{
20+
Father.resize(n);
21+
for (int i=0; i<n; i++) Father[i] = i;
22+
23+
vector<bool>rets;
24+
for (auto& req: requests)
25+
{
26+
int x = req[0], y = req[1];
27+
int f_x = FindFather(x), f_y = FindFather(y);
28+
if (f_x == f_y)
29+
{
30+
rets.push_back(true);
31+
continue;
32+
}
33+
int flag = 1;
34+
for (auto& res: restrictions)
35+
{
36+
int a = res[0], b = res[1];
37+
int f_a = FindFather(a), f_b = FindFather(b);
38+
if ((f_a==f_x && f_b==f_y) || (f_a==f_y && f_b==f_x))
39+
{
40+
flag = 0;
41+
break;
42+
}
43+
}
44+
rets.push_back(flag==1);
45+
if (flag==1) Union(x,y);
46+
}
47+
return rets;
48+
}
49+
};

0 commit comments

Comments
 (0)