You are given a rooted tree that contains \(N\) nodes. Each node contains a lowercase alphabet.
You are required to answer \(Q\) queries of type \(u, c\), where \(u\) is an integer and \(c\) is a lowercase alphabet. The count of nodes in the subtree of the node \(u\) containing \(c\) is considered as the answer of all the queries.
Input format
- First line: Two space-separated integers \(N\ and\ Q\) respectively
- Second line: A string \(s\) of length \(N\) (where the \(i^{th}\) character of \(s\) represents the character stored in node \(i\))
- Next \(N - 1\) line: Two space-separated integers \(u\) and \(v\) denoting an edge between node \(u\) and node \(v\)
- Next \(Q\) lines: An integer \(u\) and a space-separated character \(c\)
Output format
For each query, print the output in a new line.
Constraints
- \(c\) is a lowercase alphabet
- \(s_i\) is a lowercase alphabet for all \(1 \leq i \leq N\)
- \(1\) is the root node
Note: It is guaranteed that the input generates a valid tree.
3 1 aba 1 2 1 3 1 a
2
Tree given in the sample input will look like that.
Number of nodes in the subtree of node 1 having 'a' stored in it is 2.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor