{"problem":{"name":"[SNCPC2019] Unrooted Trie","description":{"content":"Recall the definition of a trie: - A trie of size $n$ is a rooted tree with $n$ vertices and $(n-1)$ edges, where each edge is marked with a character; - Each vertex in a trie represents a string. Le","description_type":"Markdown"},"platform":"Luogu","limit":{"time_limit":3000,"memory_limit":262144},"difficulty":{"LuoguStyle":"P4"},"is_remote":true,"is_sync":true,"sync_url":null,"sign":"LGP9648"},"statements":[{"statement_type":"Markdown","content":"Recall the definition of a trie:\n\n- A trie of size $n$ is a rooted tree with $n$ vertices and $(n-1)$ edges, where each edge is marked with a character;\n- Each vertex in a trie represents a string. Let $s(x)$ be the string vertex $x$ represents;\n- The root of the trie represents an empty string. Let vertex $u$ be the parent of vertex $v$, and let $c$ be the character marked on the edge connecting vertex $u$ and $v$, we have $s(v)$ = $s(u) + c$. Here $+$ indicates string concatenation, not the normal addition operation.\n\nWe say a trie is valid, if the string each vertex represents is distinct.\n\nGiven an unrooted tree with $n$ vertices and $(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?\n\n## Input\n\nThere are multiple test cases. The first line of the input contains an integer $T$, indicating the number of test cases. For each test case:\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^5$), indicating the size of the tree.\n\nFor the following $(n-1)$ lines, the $i$-th line contains two integers $u_i$, $v_i$ ($1 \\le u_i, v_i \\le n$) and a character $c_i$ separated by a space, indicating that there is an edge marked with a character $c_i$ connecting vertex $u_i$ and $v_i$. It's guaranteed that $c_i$ will only be lower-case English letters.\n\nIt's guaranteed that the given graph is a tree, and the sum of $n$ of all test cases will not exceed $10^6$.\n\n## Output\n\nFor 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.\n\n[samples]\n\n## Note\n\nFor the first sample test case, we can only select vertex 1 or vertex 2 as the root, otherwise $s(1)$ and $s(2)$ will be the same.\n\nFor the second sample test case, no matter which vertex we select as the root, $s(1)$ and $s(2)$, or $s(5)$ and $s(6)$ will be the same.","is_translate":false,"language":"English"}],"meta":{"iden":"LGP9648","tags":["2019","O2优化","陕西","树的遍历","省赛/邀请赛"],"sample_group":[["2\n6\n3 1 a\n3 2 a\n3 4 b\n4 5 c\n4 6 d\n6\n3 1 a\n3 2 a\n3 4 b\n5 4 c\n6 4 c\n","2\n0\n"]],"created_at":"2026-03-03 11:09:25"}}