-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1129.cpp
More file actions
57 lines (41 loc) · 1.75 KB
/
1129.cpp
File metadata and controls
57 lines (41 loc) · 1.75 KB
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
class Solution {//copied
public:
vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& red_edges, vector<vector<int>>& blue_edges) {
if(n==1) return {0};
vector<vector<pair<int, char>>> graph(n, vector<pair<int, char>>());
for(vector<int> edge : red_edges){
graph[edge[0]].push_back(make_pair(edge[1], 'r'));
}
for(vector<int> edge : blue_edges){
graph[edge[0]].push_back(make_pair(edge[1], 'b'));
}
queue<pair<int, pair<char, int> >> q;
q.push(make_pair(0, make_pair('r', 0)));
q.push(make_pair(0, make_pair('b', 0)));
vector<bool> visr(n, false);
vector<bool> visb(n, false);
vector<int> ans(n, -1);
ans[0] = 0;
while(!q.empty()){
auto value = q.front();
q.pop();
int& node = value.first;
char& color = value.second.first;
int& height = value.second.second;
for(auto& n : graph[node]){
if( (n.second=='b' && !visb[n.first]) || (n.second=='r' && !visr[n.first]))
{
if(color != n.second) {
ans[n.first] = ans[n.first]==-1? height + 1 : min(ans[n.first], height + 1);
q.push(make_pair(n.first, make_pair(n.second, height + 1)));
if(color == 'b')
visb[node] = true;
else
visr[node] = true;
}
}
}
}
return ans;
}
};