#P9648. [SNCPC2019] Unrooted Trie

    ID: 9012 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>2019O2优化陕西树的遍历XCPC

[SNCPC2019] Unrooted Trie

题目描述

Recall the definition of a trie:

  • A trie of size nn is a rooted tree with nn vertices and (n1)(n-1) edges, where each edge is marked with a character;
  • Each vertex in a trie represents a string. Let s(x)s(x) be the string vertex xx represents;
  • The root of the trie represents an empty string. Let vertex uu be the parent of vertex vv, and let cc be the character marked on the edge connecting vertex uu and vv, we have s(v)s(v) = s(u)+cs(u) + c. 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 nn vertices and (n1)(n-1) 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?

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1051 \le n \le 10^5), indicating the size of the tree.

For the following (n1)(n-1) lines, the ii-th line contains two integers uiu_i, viv_i (1ui,vin1 \le u_i, v_i \le n) and a character cic_i separated by a space, indicating that there is an edge marked with a character cic_i connecting vertex uiu_i and viv_i. It's guaranteed that cic_i will only be lower-case English letters.

It's guaranteed that the given graph is a tree, and the sum of nn of all test cases will not exceed 10610^6.

输出格式

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

提示

For the first sample test case, we can only select vertex 1 or vertex 2 as the root, otherwise s(1)s(1) and s(2)s(2) will be the same.

For the second sample test case, no matter which vertex we select as the root, s(1)s(1) and s(2)s(2), or s(5)s(5) and s(6)s(6) will be the same.