Trie in Tina Town

Time Limit: 8000ms
Memory Limit: 524288KB
This problem will be judged on HDU. Original ID: 5394
64-bit integer IO format: %I64d      Java class name: Main

Description

Tina Town is a friendly place. People there care about each other.

A trie which was planted by the first mayor of Tina Town grows in the center of the town.

We define a palindrome substring in a trie a string that is a suffix of a string which the path from the root to any node represents and the string is a palindrome. Two palindromes are different if and only if their positions are different. Now, Tina wants to know the sum of the length of all palindrome substrings. Tina didn’t know the answer so she asked you to find out the answer for her.

Input

The first line contains a integer – number of cases
For each case, the first line is an integer $N$ representing the number of nodes in trie except the root.
The following $N$ lines contains a letter between $a$ and $d$ – the letter that node $n[i]$ stores and a number $f[i]$ – the index of $n[i]$’s father. It’s guaranteed that $fa[i] \leq i$. If $fa[i] = 0$ n[i] is the root.
$T \leq 10, N \leq 2*{10}^6$

Output

The first line contains a integer – number of cases
For each case, the first line is an integer $N$ representing the number of nodes in trie except the root.
The following $N$ lines contains a letter between $a$ and $d$ – the letter that node $n[i]$ stores and a number $f[i]$ – the index of $n[i]$’s father. It’s guaranteed that $fa[i] \leq i$. If $fa[i] = 0$ n[i] is the root.
$T \leq 10, N \leq 2*{10}^6$

Sample Input

2
5
a 0
a 1
a 2
b 1
b 4
5
a 0
a 1
a 2
b 1
a 4

Sample Output

14
15

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .