forked from Chayandas07/LeetCode-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1162. As Far from Land as Possible 11 feb
46 lines (39 loc) · 1.31 KB
/
1162. As Far from Land as Possible 11 feb
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
class Solution {
public:
vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& red_edges, vector<vector<int>>& blue_edges){
vector<int> res(n, -1), v1(n, 0), v2(n, 0);
vector<vector<pair<int, int>>> vp(n);
queue<pair<int, int>> q;
for(auto &it : red_edges) vp[it[0]].push_back({it[1], 1});
for(auto &it : blue_edges) vp[it[0]].push_back({it[1], 2});
res[0] = 0;
v1[0] = 1;
v2[0] = 1;
int temp = 1;
for(auto &it : vp[0])
{
q.push(it);
if(res[it.first] == -1) res[it.first] = temp;
}
while(!q.empty())
{
int len = q.size();
temp++;
for(int i=0; i<len; ++i)
{
auto val = q.front();
q.pop();
if(val.second == 1) v1[val.first] = 1;
else v2[val.first] = 1;
for(auto &it : vp[val.first])
{
if(val.second == 1 && (v2[it.first] || it.second==1)) continue;
if(val.second == 2 && (v1[it.first] || it.second==2)) continue;
q.push(it);
if(res[it.first] == -1) res[it.first] = temp;
}
}
}
return res;
}
};