1. If John and Jon are synonyms, and Jon and Johnny are synonyms, then John and Johnny are also synonyms.
2. While printing the true frequency, out of the names with synonymic relationships, use the names list first.
Names = 5, Synonyms = 3
5
john 15
jon 12
chris 13
kris 4
christopher 19
3
jon john
chris kris
chris Christopher
The output for the above example will be:
john 27
chris 36
Since “jon” and “john” are synonyms, the final true frequency will be 15 + 12 = 27, and since john appears first in the first list, we’ll use “john” as the real name. Also, “chris” and “kris” are synonyms, and since “chris” and “christopher” are also synonyms, “chris” and “christopher” will also be synonyms, and the true frequency will be 13 + 4 + 19 = 36, and since “chris” appears first in the names list, we’ll use it as the real name.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test contains an integer 'N', representing the total number of names.
The next 'N' lines contain a string and an integer, representing a name and the number of times this name appears in the list.
The next line contains an integer 'M', representing the number of synonym relationships.
The next 'M' lines contain two strings that have a synonym relationship.
For every test case, print a new list of names with their true frequencies.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N, M <= 10^4
1 <= |Name| <= 10
1 <= Frequency <= 100
Where “Name” contains only lowercase English alphabets.
Time limit: 1 sec
Approach: We can visualize this question as a graph where each name will represent a particular node, and synonymic relations will connect two nodes. Each connected component of this graph will represent synonyms of a specific name. We will traverse through each connected component using DFS.
Algorithm:
Cakes
1-3 Palindrome
Longest Subarray With Zero Sum
Distance to a Cycle in Undirected Graph
Maximum GCD