After having watched all eight Harry Potter movies in a week, Nikola finally realized how the famous Elder Wand changes the wizard it obeys. If wizard
A, whom the wand is currently obeying, is defeated by wizard
B in a duel, then the wand will start obeying the wizard
Nikola is now wondering what would happen if 26 wizards labeled with uppercase letters of the English alphabet from
Z began fighting in duels for the fondness of the Elder Wand. If we know the label of the wizard that the wand had obeyed before all duels and the outcomes of all duels that were held one after another, answer the following questions:
- Which wizard did the wand obey after all duels?
- How many different wizards did the wand obey?
The first line contains an uppercase letter of the English alphabet, the label of the wizard that the wand obeyed at the beginning.
The second line contains an integer number , the number of duels from the text of the task.
In the next rows there are two different uppercase letters of the English alphabet and separated by a space, whereas the wizard with the label defeated the wizard with the label in the duel.
In the first line print an uppercase letter of the English alphabet, answer to the first question from the task description.
In the second line print an integer number, answer to the second question from the task description.
Correct answer to the first question is worth 2 points and the correct answer to the second question is worth 3 points. If you do not know how to solve some part of the task, then print any value in the corresponding line.
Sample Input 1
A 3 B A C B D A
Sample Output 1
Explanation for Sample Output 1
Before the first duel, the Elder Wand obeyed wizard
A. After the first duel, it obeyed wizard
B, and after the second wizard
C. The third duel didn't change anything.
Sample Input 2
N 5 D A N B B A C D F A
Sample Output 2
Sample Input 3
X 4 A X B X X A D A
Sample Output 3
Hint: Only read if you're stuck:
Don't forget that if a previous owner of the want defeats the current owner, you have to make sure to account for it. For example
Lets Say A starts with the want and 3 fights:
-B beats A
-C beats B
-A beats C
A currently has the wand, and the wand has been passed around by 3 different owners. if you don't account for the fact that A has had the wand before, you output a 4 instead of a 3. In other words, A will be counted as a New owner of the wand when he's actually a previous owner.
Can anyone explain to me how the third input and output works?
Wizard X starts with the wand and there are 4 total duels.
Duel 1: A defeats X. Now A has the wand, and the wand has been owned by A and X.
Duel 2: B defeats X. No change in wand status since neither B nor X owned the wand.
Duel 3: X defeats A. Now X has the wand back, and the wand has been owned by A and X.
Duel 4: D defeats A. No change in wand status since neither D nor A owned the wand.
At the end of all 4 duels, X owns the wand and the wand has had two total owners: X and A.
Therefore the output is:
Hey guys and gals. I'm confused about the problem. Why is the output of sample 2 N and 1? Any input would be greatly appreciated.
This tells us that the only way to get the wand is to defeat wizard A (where wizard A has the wand).
In each line, the first wizard defeats the second wizard (in this case,
B, and so on). Remember, since
Nbegins with the wand, the only way for someone else to get the wand is to defeat
N. Since nobody ever defeated
N, nobody else ever got the wand. Therefore, the wand ends with
Nand is controlled by 1 wizard throughout.
Thanks for your reply. I think I get it now.