-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathD.cpp
89 lines (70 loc) · 1.52 KB
/
D.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
#define ll int
char str[300009];
ll val[300009];
vector <ll> graph[300009], start;
ll vis[300009];
ll mx = -1, nw, cnt[300009][30];
void dfs1(ll u)
{
if(vis[u] == 1) {
printf("-1\n");
exit(0);
}
if(vis[u] == 2)
return;
vis[u] = 1;
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
dfs1(nd);
}
vis[u] = 2;
}
void dfs(ll u)
{
//memcpy(counter, cnt, sizeof(cnt));
if(vis[u])
return;
cnt[u][ val[u] ]++;
vis[u] = 1;
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
//cout << counter[ val[nd] ] << endl;
dfs(nd);
for(ll j = 0; j < 26; j++)
cnt[u][j] = max(cnt[u][j], cnt[nd][j] + (ll)(j == val[u]));
}
}
int main()
{
ll n, e;
cin >> n >> e;
scanf("%s", str);
ll len = strlen(str);
for(ll i = 0; i < len; i++) {
val[i + 1] = (str[i] - 'a');
}
for(ll i = 1; i <= e; i++) {
ll u, v;
scanf("%d %d", &u, &v);
graph[u].push_back(v);
}
for(ll i = 1; i <= n; i++) {
if(vis[i] == 0) {
nw = 1;
dfs1(i);
if(nw != -1)
start.push_back(i);
}
}
memset(vis, 0, sizeof(vis));
for(ll i = 0; i < start.size(); i++) {
dfs(start[i]);
}
for(ll i = 1; i <= n; i++)
for(ll j = 0; j < 26; j++)
mx = max(mx, cnt[i][j]);
cout << mx << endl;
return 0;
}