All problems

Problem 942

Largest Color Value in a Directed Graph

Hard · 30 pts · Graph, Topological Sort, Dynamic Programming

Problem

A directed graph has N nodes, and node i has the colour COLORS[i], a lowercase letter. The colour value of a path is the number of nodes on it with its most frequent colour. Find the largest colour value of any path, or print -1 if the graph contains a cycle. Process the nodes in topological order (Kahn's algorithm) and keep, for every node and every letter, the largest count of that letter on a path ending at the node.

Input

The first line contains N and M. The second line contains the string COLORS of length N. Each of the next M lines contains an edge u v (0-based, from u to v).

Output

Print the largest colour value, or -1 if there is a cycle.

Constraints

1 <= N <= 100000; 0 <= M <= 100000

Example input

5 4
abaca
0 1
0 2
2 3
3 4

Example output

3

Explanation

With colours abaca and edges 0→1, 0→2, 2→3, 3→4, the path 0 → 2 → 3 → 4 has colours a, a, c, a, three of them a. So the answer is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Score of a Path Between Two Cities Next: Find All Possible Recipes from Given Supplies