-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avoid explosion(GFG).cpp
64 lines (54 loc) · 1.63 KB
/
Avoid explosion(GFG).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
class Disjoint {
private:
vector<int> parent, size;
public:
Disjoint(int n) {
size.resize(n + 1, 1);
parent.resize(n + 1);
for(int i = 0; i <= n; i++)
parent[i] = i;
}
int findParent(int node) {
if(parent[node] == node) return node;
return parent[node] = findParent(parent[node]);
}
void unionBySize(int u, int v) {
int pu = findParent(u);
int pv = findParent(v);
if(pu == pv) return;
if(size[pu] < size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
}
};
class Solution {
public:
vector<string> avoidExplosion(vector<vector<int>> mix, int n, vector<vector<int>> danger, int m) {
Disjoint d(n);
vector<string> ans;
for(int i = 0; i < n; i++) {
int u = mix[i][0], v = mix[i][1];
int pu = d.findParent(u), pv = d.findParent(v);
bool flag = true;
for(int j = 0; j < m; j++) {
int u1 = danger[j][0], v1 = danger[j][1];
int pu1 = d.findParent(u1), pv1 = d.findParent(v1);
if((pu == pu1 and pv == pv1) or (pu == pv1 and pv == pu1)) {
flag = false;
break;
}
}
if(!flag)
ans.push_back("No");
else {
d.unionBySize(u, v);
ans.push_back("Yes");
}
}
return ans;
}
};