Ananna
시간 제한0.5초메모리 제한2048 MB
간선마다 글자가 붙은 방향 그래프가 주어질 때, U에서 V로 가는 어떤 보행이 회문을 이루는 서로 다른 두 도시 (U, V)의 개수를 센다.
문제
Danaland is a very typical country: it consists of cities, each identified by a distinct number. These cities are connected by unidirectional roads, where each road has a name.
Ananna is a bright little girl who lives in Danaland. Unfortunately, she was born with a terrible disease: she can only read backwards. After being a victim of terrible bullying by her peers, or, as Ananna calls them, sreep, she found solace in palindromes: words that are the same when read backwards.
Ananna’s mom, Eeve, is trying to help her with her condition. One way she helps is by taking her on road trips. A road trip is a sequence of roads that starts at some city and ends at a different city ; the same road may appear more than once.
While on a road trip, Eeve asks Ananna the first letter of each road name, so she can practice looking at the start of words. This is, obviously, a source of great anxiety to Ananna, so to avoid having a kcatta cinap, Eeve always makes sure that the sequence formed by taking the first letter of each road’s name, in the order they are traversed, is a palindrome.
Eeve is now looking at a map of Danaland, and she wonders: How many distinct pairs of cities , exist such that Eeve can take a road trip from to ?
입력
The first line contains two integers and (), indicating respectively the number of cities and the number of roads in Danaland. Each city is identified by a distinct integer from to .
Each of the next lines contains two integers and () and a lowercase letter , representing that there is a unidirectional road from to whose name starts with . Several roads may connect the same pair of cities, and a road may connect a city to itself.
출력
Output a single line with an integer indicating the number of pairs of cities , such that , there is a road trip from to , and the letters of the roads (in the order they are traversed) form a palindrome.