1376.cpp (791B)
1 class Solution { 2 public: 3 int numOfMinutes(int n, int headID, const vector<int> &manager, const vector<int> &informTime) { 4 vector<int> time(n, -1); 5 time[headID] = 0; 6 int res = 0, crnt, sum1, sum2; 7 for (int i = 0; i < n; i++) { 8 if (informTime[i] != 0) continue; 9 crnt = i, sum1 = 0; 10 while (time[crnt] == -1) { 11 sum1 += informTime[crnt]; 12 crnt = manager[crnt]; 13 } 14 res = max(res, sum1 += time[crnt]); 15 crnt = i, sum2 = 0; 16 while (time[crnt] == -1) { 17 time[crnt] = sum1 - sum2; 18 sum2 += informTime[crnt]; 19 crnt = manager[crnt]; 20 } 21 } 22 23 return res + informTime[headID]; 24 } 25 };