# Battle of Legends solution codechef

## Battle of Legends solution codechef

Arceus, the creator of the Pokémon world is very angry at humanity for what they did to Arceus in the past. Arceus is attacking to destroy the humanity. But the Legendary Trio of Dialga, Palkia and Giratina came to rescue the humanity. They are trying to battle Arceus with their full power. But Arceus is too powerful. So Dialga with its power over Time, sends Ash and his friends to past to change the past mistakes done by humanity. They have to complete the task given below to obtain the Jewel of Life and calm down Arceus by offering the Jewel.

Given a tree with NN vertices. Where each vertex has a character assigned to it. You have to answer QQ queries. For each query you are given a starting vertex and length of the string to be found. String is formed by choosing a simple path starting at given vertex and appending the characters on vertices from start to end. Also the string must be the lexicographically smallest string possible. You have to print the encoding of the this string modulomodulo 10000000071000000007.

encoding(S)=sum|S|1i=0f(S[i])Bi encoding(S)=sumi=0|S|−1f(S[i])∗Bi

where, BB = 31 and for ff is a mapping from character to integer such that, f(a)f(′a′) = 1, f(b)f(′b′) = 2, …, f(z)f(′z′) = 26.

### Input: Battle of Legends solution codechef

• First line will contain TT, number of test-cases. Then the test-cases follow.
• NN QQ, number of vertices and number of queries.
• NN space separated characters for each vertex from 11 to NN.
• Next NN – 11 lines contain two integers uu and vv, showing there is an edge.
• Next QQ lines contain two integers uu and dduu is the starting vertex and dd is the length of the string.

### Output: Battle of Legends solution codechef

For each query, print the encoding of the lexicographically smallest string of length d and starting vertex u.

### Constraints Battle of Legends solution codechef

• 1T101≤T≤1033
• 1N101≤N≤1055
• 1Q101≤Q≤1055
• EachEach charactercharacter belongsbelongs toto lowercaselowercase EnglishEnglish alphabetsalphabets [a,z][a,z].
• For each query of type uu dd,
• 1uN1≤u≤N
• 1dN1≤d≤N
• SumSum ofof NN overover allall testtest casescases 10≤1055.
• SumSum ofof QQ overover allall testtest casescases 10≤1055.

### Sample Input: Battle of Legends solution codechef

 1
4 2
a b c d
1 2
2 3
2 4
3 3
1 4


### Sample Output:

 1026
-1


### Sample Explanation:

 Query 1 : There are two ways. One is 33 - 22 - 11 and other is 33 - 22 - 44. Strings formed by respective paths are cba
and cbd. cba is lexicographically small than cbd. Hence encoding(cba) = 1026.
Query 2 : There is no path of size 4 starting from 1. Hence answer is -1.

# SOLUTION

Codechef is an platform built to programmers compete against others in the community and to improve their knowledge by facing various challenges. Challenges are categorised on the basis of their difficulty level , if your rating is above 2000 you are in top tier i.e, Division 3 and if your rating is between 2000 and 1600 you will be in tier 2 i.e, Division 3 and if your rating is below that you will be in lowest tier i.e, Division 3. You can improve your rating by successful submission with minimum penalty.