class Solution { public: int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) { vector<vector<int>> gr(n+1); vector<int> indegree(n+1, 0), prev_time(n+1, 0); for(auto& edge : relations){ gr[edge[0]].push_back(edge[1]); indegree[edge[1]]++; } time.insert(time.begin(), 0); queue<int> q; for(int i =1; i <= n; i++) if(indegree[i] == 0) q.push(i); while(!q.empty()){ int node = q.front();q.pop(); for(auto adjV : gr[node]){ prev_time[adjV] = max(prev_time[adjV] , time[node] + prev_time[node]); if(--indegree[adjV] == 0) q.push(adjV); } } int ans = 0; for(int i =1 ; i < n + 1; ++i){ ans = max(ans, time[i]+prev_time[i]); } return ans; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter