F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Trie in Tina Town

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1753    Accepted Submission(s): 271


Problem 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
 

Hint

The first test case is a trie like this:
aaa *1 -> 3
aa *2 -> 4
a *3-> 3
b *2-> 2
bb *1- > 2
3+4+3+2+2 = 14

The second test case:
aaa *1 -> 3
aba *1 -> 3
a *4 -> 4
b *1 -> 1
aa *2 -> 4
3+3+4+1+4 = 15

If the stack size is too small, you can submit it in C++ and add ‘#pragma comment(linker, "/STACK:102400000,102400000”)’ at the head of your program.
Large input, recommend to use fast I/O.
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2025 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2025-03-29 01:38:04, Gzip enabled