#P9648. [SNCPC2019] Unrooted Trie
[SNCPC2019] Unrooted Trie
Description
Recall the definition of a trie:
- A trie of size is a rooted tree with vertices and edges, where each edge is marked with a character;
- Each vertex in a trie represents a string. Let be the string vertex represents;
- The root of the trie represents an empty string. Let vertex be the parent of vertex , and let be the character marked on the edge connecting vertex and , we have = . Here indicates string concatenation, not the normal addition operation.
We say a trie is valid, if the string each vertex represents is distinct.
Given an unrooted tree with vertices and edges, where each edge is marked with a character, how many different vertices can be selected as the root of the tree so that the tree becomes a valid trie?
Input Format
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the size of the tree.
For the following lines, the -th line contains two integers , () and a character separated by a space, indicating that there is an edge marked with a character connecting vertex and . It's guaranteed that will only be lower-case English letters.
It's guaranteed that the given graph is a tree, and the sum of of all test cases will not exceed .
Output Format
For each test case output one line containing one integer, indicating the number of different vertices that can be selected as the root of the tree to make it a valid trie.
2
6
3 1 a
3 2 a
3 4 b
4 5 c
4 6 d
6
3 1 a
3 2 a
3 4 b
5 4 c
6 4 c
2
0
Hint
For the first sample test case, we can only select vertex 1 or vertex 2 as the root, otherwise and will be the same.
For the second sample test case, no matter which vertex we select as the root, and , or and will be the same.
京公网安备 11011102002149号