-
Notifications
You must be signed in to change notification settings - Fork 0
/
1857.cpp
55 lines (50 loc) · 1.18 KB
/
1857.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
int dfs(int node, unordered_map<int, vector<int>> &m, set<int> &vis, set<int> &path, vector<vector<int>> &dp, string &colors)
{
if (path.count(node))
{
return INT_MAX;
}
if (vis.count(node))
{
return 0;
}
vis.insert(node);
path.insert(node);
dp[node][colors[node] - 'a'];
for (auto i : m[node])
{
if (dfs(i, m, vis, path, dp, colors) == INT_MAX)
{
return INT_MAX;
}
for (int clr = 0; clr < 26; clr++)
{
dp[node][clr]=max(dp[node][clr],(clr==colors[node]?1:0)+dp[i][clr]);
}
}
path.erase(node);
return *max_element(dp[node].begin(),dp[node].end());
}
int largestPathValue(string colors, vector<vector<int>> &edges)
{
unordered_map<int, vector<int>> m;
for (auto i : edges)
{
m[i[0]].push_back(i[1]);
}
int n = colors.size();
int ans = 0;
set<int> vis;
set<int> path;
vector<vector<int>> dp(n, vector<int>(26, 0));
for (int i = 0; i < n; i++)
{
ans = max(dfs(i, m, vis, path, dp, colors), ans);
}
return ans == INT_MAX ? -1 : ans;
}
int main()
{
}