CF919D.Substring

传统题 时间 3000 ms 内存 256 MiB 7 尝试 17 已通过 8 标签

Substring

Statement

You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 33. Your task is find a path whose value is the largest.

Input

The first line contains two positive integers n,mn, m (1n,m3000001 \leq n, m \leq 300\,000), denoting that the graph has nn nodes and mm directed edges. The second line contains a string ss with only lowercase English letters. The ii-th character is the letter assigned to the ii-th node. Then mm lines follow. Each line contains two integers x,yx, y (1x,yn1 \leq x, y \leq n), describing a directed edge from xx to yy. Note that xx can be equal to yy and there can be multiple edges between xx and yy. Also the graph can be not connected.

Output

Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.

样例

样例 1

输入:

5 4
abaca
1 2
1 3
3 4
4 5

输出:

3

样例 2

输入:

6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4

输出:

-1

样例 3

输入:

10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7

输出:

4

样例解释(英文原文)

In the first sample, the path with largest value is 13451 \to 3 \to 4 \to 5. The value is 33 because the letter 'a' appears 33 times.

在线编程 IDE

建议全屏模式获得最佳体验