forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe-time-when-the-network-becomes-idle.cpp
More file actions
33 lines (32 loc) · 1.06 KB
/
the-time-when-the-network-becomes-idle.cpp
File metadata and controls
33 lines (32 loc) · 1.06 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
// Time: O(|V| + |E|) = O(|E|) since graph is connected, O(|E|) >= O(|V|)
// Space: O(|V| + |E|) = O(|E|)
class Solution {
public:
int networkBecomesIdle(vector<vector<int>>& edges, vector<int>& patience) {
vector<vector<int>> adj(size(patience));
for (auto &edge : edges) {
adj[edge[0]].emplace_back(edge[1]);
adj[edge[1]].emplace_back(edge[0]);
}
vector<bool> lookup(size(patience));
lookup[0] = true;
vector<int> q{{0}};
int result = 0, step = 1;
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& v : adj[u]) {
if (lookup[v]) {
continue;
}
lookup[v] = true;
new_q.emplace_back(v);
result = max(result, ((step * 2) - 1) / patience[v] * patience[v] + (step * 2));
}
}
q = move(new_q);
++step;
}
return 1 + result;
}
};