


The graph is connected.
You are given the following graph:

Here the path 0->1->2 has the colour value of 3, which is the maximum colour value of the graph. Hence the answer is 3.
The first line of input contains ‘T’ representing the number of test cases.
The first line of each test case contains two integers ‘N’ and ‘M’ representing the number of nodes and edges.
The next ‘M’ lines of the input contain two space-separated integers ‘a’ and ‘b’ representing a directed edge from ‘a’ to ‘b’.
The following line contains a string of ‘N’ characters, where ith character represents the colour of ‘ith’ node.
For each test case, print a single integer representing the largest colour value of the graph.
Print a separate line for each test case.
1 <= N, M <= 10^5
0 <= a, b < N
‘colour’ array contains lower case english alphabet characters.
Time Limit: 1 sec.
You do not need to print anything. It has already been taken care of. Just implement the given function.
In this approach, we will visit the nodes in topological order, as in a topological order every node that can lead up to a current node in a graph, will already be processed. Therefore we will do a normal top sort, but for each node, we will maintain a colour frequency which will represent the maximum frequency of every colour leading up to the current node. Then we will update the colour frequency of every neighbour of the current node.
In this approach, we will visit the nodes in topological sort, but here we will go toward the leaf of the graph and then go back and update the current node’s colour frequencies from their child’s colour frequencies.
We will create a dfs(node, colours, adjList, coloursCount, visited), where node is the current visiting node, colours is the string representing the colours of nodes, coloursCount is the map of frequencies and visited is the visited node array.
Algorithm: