forked from WadekarPrashant/LeetCode-Problems-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1857.cpp
40 lines (36 loc) · 974 Bytes
/
Problem1857.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int largestPathValue(string colors, vector<vector<int>>& edges) {
const int n = colors.length();
int ans = 0;
int processed = 0;
vector<vector<int>> graph(n);
vector<int> inDegree(n);
queue<int> q;
vector<vector<int>> count(n, vector<int>(26));
// Build graph
for (const vector<int>& edge : edges) {
const int u = edge[0];
const int v = edge[1];
graph[u].push_back(v);
++inDegree[v];
}
// Topology
for (int i = 0; i < n; ++i)
if (inDegree[i] == 0)
q.push(i);
while (!q.empty()) {
const int out = q.front();
q.pop();
++processed;
ans = max(ans, ++count[out][colors[out] - 'a']);
for (const int in : graph[out]) {
for (int i = 0; i < 26; ++i)
count[in][i] = max(count[in][i], count[out][i]);
if (--inDegree[in] == 0)
q.push(in);
}
}
return processed == n ? ans : -1;
}
};