File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(|V| + |E|)
2
+ // Space: O(|E|)
3
+
4
+ class Solution {
5
+ public:
6
+ int minimumTime (int n, vector<vector<int >>& relations, vector<int >& time) {
7
+ vector<vector<int >> adj (n);
8
+ vector<int > in_degree (n);
9
+ for (const auto & relation : relations) {
10
+ adj[relation[0 ] - 1 ].emplace_back (relation[1 ] - 1 );
11
+ ++in_degree[relation[1 ] - 1 ];
12
+ }
13
+ vector<int > q;
14
+ vector<int > dist (n);
15
+ for (int u = 0 ; u < n; ++u) {
16
+ if (in_degree[u]) {
17
+ continue ;
18
+ }
19
+ q.emplace_back (u);
20
+ dist[u] = time[u];
21
+ }
22
+ while (!empty (q)) {
23
+ vector<int > new_q;
24
+ for (const auto & u : q) {
25
+ for (const auto & v : adj[u]) {
26
+ dist[v] = max (dist[v], dist[u] + time[v]);
27
+ --in_degree[v];
28
+ if (!in_degree[v]) {
29
+ new_q.emplace_back (v);
30
+ }
31
+ }
32
+ }
33
+ q = move (new_q);
34
+ }
35
+ return *max_element (cbegin (dist), cend (dist));
36
+ }
37
+ };
You can’t perform that action at this time.
0 commit comments