No.762 PDCA Path
Problem Statement
You are given a graph consisting of $N$ vertices and $M$ undirected edges.
The $i$-th vertex has one of the characters P,D,C,A written on it.
The $j$-th edge connects the $u_j$-th vertex and the $v_j$-th vertex.
A way of going from one vertex to another in which the start and end vertices are different and no vertex is visited more than once is called a path.
A path of length 3 is a path consisting of 3 edges and 4 vertices.
A path of length 3 is called a PDCA path if, when its vertices are traversed from one end to the other, they can be read as PDCA.
Among the paths of length 3 in the graph, how many PDCA paths are there? Find the count modulo $10^9+7$.
Input
N M S u_1 v_1 ... u_M v_M
$2 \le N \le 10^5$
$0 \le M \le 10^5$
$|S| = N$
$1 \le u_i \lt v_i \le N$
The given graph does not contain multiple edges or self-loops.
Each character of the string $S$ is one of P,D,C,A.
The $i$-th character of $S$ indicates the character written on vertex number $i$.
Output
Output the number of PDCA paths modulo 1000000007.
Print a newline at the end.
Sample
Sample 1
Input
7 11 PAAPCDD 1 3 1 5 1 6 2 5 2 6 3 5 5 6 4 5 5 7 4 7 2 7
Output
4
There are 4 such paths: 1,6,5,2, 1,6,5,3, 4,7,5,2, 4,7,5,3.
Sample 2
Input
6 3 ACPPPD 1 2 3 4 4 5
Output
0
The graph is not necessarily connected, and there may be no PDCA paths.
Sample 3
Input
11 11 AAACCCDDPPP 1 4 1 5 2 5 4 7 5 7 7 9 7 10 2 4 3 6 6 8 8 11
Output
9
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
matsu7874