3
int networkBecomesIdle(vector<vector<int>> &edges, vector<int> &patience) {4
int n = patience.size();5
vector<vector<int>> graph(n);6
vector<int> time(n, -1);8
for (auto x : edges) { // create adjacency list9
graph[x[0]].push_back(x[1]);10
graph[x[1]].push_back(x[0]);20
for (auto child : graph[node]) {21
if (time[child] == -1) { // if not visited.22
time[child] = time[node] + 1; // calc time for child node29
for (int i = 1; i < n; i++) {30
int extraPayload = (time[i] * 2 - 1) / patience[i];31
// extra number of payload before the first message arrive back to data32
// server. since a data server can only send a message before first33
// message arrives back." and first message arrives at time[i]*2. so37
extraPayload * patience[i]; // find the last time when a data server sends a message38
int lastIn = lastOut + time[i] * 2; // this is the result for current data server40
res = max(res, lastIn);43
// at "res" time the last message has arrived at one of the data servers.44
// so at res+1 no message will be passing between servers.