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
|
#include <iostream>
#include <vector>
#include <queue>
const int INF = 0x3fff'ffff;
class Solution {
public:
int networkBecomesIdle(std::vector<std::vector<int>>& edges, std::vector<int>& patience) {
int n = patience.size();
std::vector<int> dist(n, INF);
std::vector<std::vector<int>> adj(n);
for (auto& e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
std::queue<int> q;
dist[0] = 0;
q.push(0);
while (!q.empty()) {
int u = q.front();
int d = dist[u];
q.pop();
for (auto& v : adj[u]) {
if (dist[v] == INF) {
dist[v] = d + 1;
q.push(v);
}
}
}
int res = 0;
for (int i = 1; i < n; ++i) {
res = std::max(res, (2 * dist[i] - 1) / patience[i] * patience[i] + 2 * dist[i] + 1);
}
return res;
}
};
|