Advertisement
nikunjsoni

1376

Apr 27th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. class Solution {
  2. int ans = 0;
  3. public:
  4.     int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
  5.         vector<vector<int>> g;
  6.         g.resize(n);
  7.        
  8.         // Make graph.
  9.         for(int i=0; i<n; i++)
  10.             if(manager[i] != -1)
  11.                 g[manager[i]].push_back(i);
  12.        
  13.         // DFS traversal.
  14.         dfs(headID, 0, g, informTime);
  15.         return ans;
  16.     }
  17.    
  18.     void dfs(int node, int time, vector<vector<int>> &g, vector<int> &informTime){
  19.         ans = max(ans, time);
  20.         for(int child: g[node])
  21.             dfs(child, time+informTime[node], g, informTime);
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement