-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvoid Explosion.CPP
51 lines (39 loc) · 1.14 KB
/
Avoid Explosion.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
class Solution {
public:
int adj[1005];
int find_set(int v) {
if (v != adj[v]){
return adj[v] = find_set(adj[v]);
}
return adj[v];
}
vector<string> avoidExplosion(vector<vector<int>> mix, int n,vector<vector<int>> danger, int m) {
for (int i=1; i<=n; i++){
adj[i]=i;
}
vector<string> ans;
for (int i=0; i<n; i++){
int a=mix[i][0];
int b=mix[i][1];
bool flag=true;
int x2=find_set(a);
int y2=find_set(b);
for (int j=0; j<m; j++){
int x1=find_set(danger[j][0]);
int y1=find_set(danger[j][1]);
if ((x2==x1 && y2==y1) || (x2==y1 && y2==x1)){
flag=false;
break;
}
}
if (flag){
adj[x2]=y2;
ans.push_back("Yes");
}
else{
ans.push_back("No");
}
}
return ans;
}
};