-
Notifications
You must be signed in to change notification settings - Fork 3
/
3243.cpp
38 lines (38 loc) · 1.12 KB
/
3243.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
class Solution {
public:
int shortestPath(int n, vector<vector<int>>& graph) {
queue<int> q;
q.push(0);
int res = 0;
vector<bool> visited(n, false);
visited[0] = true;
while (!q.empty()) {
int m = q.size();
while (m--){
int node = q.front();
q.pop();
if (node == n - 1) return res;
for (auto& neighbor : graph[node]) {
if (visited[neighbor]) continue;
q.push(neighbor);
visited[neighbor] = true;
}
}
res++;
}
return n - 1;
}
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<vector<int>> graph(n);
for (int i = 0; i < n - 1; ++i) {
graph[i].push_back(i + 1);
}
int m = queries.size();
vector<int> res(m, 0);
for (int i = 0; i < m; ++i) {
graph[queries[i][0]].push_back(queries[i][1]);
res[i] = shortestPath(n, graph);
}
return res;
}
};