COCI '18 Contest 4 #1 Elder

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

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 B.

Nikola is now wondering what would happen if 26 wizards labeled with uppercase letters of the English alphabet from A to 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 N duels that were held one after another, answer the following questions:

  1. Which wizard did the wand obey after all N duels?
  2. 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 N (1 \le N \le 100), the number of duels from the text of the task.

In the next N rows there are two different uppercase letters of the English alphabet Z1 and Z2 separated by a space, whereas the wizard with the label Z1 defeated the wizard with the label Z2 in the i^\text{th} 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


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


Sample Output 2


Sample Input 3


Sample Output 3



  • 0
    Pat_Rick  commented on June 26, 2022, 10:56 p.m.

    Can someone have a look at my code please.

    I'm getting 33/50, with all questions 3/5.

    I assume because the final elder wand holder answer was wrong.

    Could someone give me a sample input which my code will give the wrong final elder wand holder answer so I can troubleshoot please?

    Thank you.

  • -1
    KmiloKun  commented on June 22, 2022, 1:18 a.m. edit 2

    I need some help with my code :( I belive it's good but I don't achieve 50/50

  • -1
    UnicornMagic  commented on May 25, 2022, 12:18 p.m.

    Hey guys, it's my firs time asking for help on this website and I am just wondering what my error could be. I have 29/50 and I just don't know why. I have taken into consideration to remove the duplicate owners as well.

    • 0
      bethruk  commented on May 28, 2022, 9:27 a.m.

      Hi there, your code to remove the duplicates doesn't actually remove them. For each letter you are checking if it's in the list, and it always is in the list, so you are adding 1 to the count each time. Hope I've explained what I mean and that it helps.

      • -1
        UnicornMagic  commented on May 30, 2022, 4:50 a.m.

        I just updated my code and now it works, thanks for the tip! Sometimes you just need a pair of fresh eyes to look at your code.

      • -1
        UnicornMagic  commented on May 30, 2022, 4:40 a.m.

        Ohhh, I see now haha.

  • -1
    hathimerasathi  commented on March 14, 2022, 1:59 a.m.

    i am not able to understand why i only get 22/50 . i have tried two things. can anyone review and let me know of where can i make improvements ?

    • -1
      Spitfire720  commented on March 14, 2022, 10:07 a.m.

      The goal is to ask how many DIFFERENT wizards the wand obeyed, so if you input a test case like

      B A
      C B
      A C

      Your code prints out


  • -1
    carlosg22  commented on March 6, 2022, 3:55 p.m.

    My program is passing the first question, however I am having difficulties with sample input output for the total number of different wizards that the wand obeyed. Any way I can accomplish this?

    • -1
      Spitfire720  commented on March 7, 2022, 8:13 a.m.

      The number of wizards obeyed should start at 1, because the Elder Wand alredy obeys someone.

      You should also note that only if the wizard who has the Elder Wand is defeated, the Elder Wand goes to the victor.

  • -1
    Jzaragoza98  commented on Feb. 8, 2022, 10:34 p.m.

    Running into an EOFError. Any input on resolving this exception?

    • 1
      Spitfire720  commented on Feb. 9, 2022, 9:34 a.m.

      Looking at your last submission, you get a ValueError instead.

      The reason is that you read your input wrong. Your line

      Z1, Z2 = input()

      will read input B A(from the sample test case) as "B A".

      That string will go to Z1, but nothing will go to Z2. Therefore, you get an error.

      One way to solve this is using the split() function, so that the input will be separated by space and your input would be read as "B", "A", which would get put into Z1 and Z2, respectively, and your code won't output an error.

      Also, EOFError is End of File Error. Unless you created a file, reached the end and tried to read further, you should not get this error.

  • 1
    ServontiusT  commented on Sept. 6, 2021, 3:59 p.m.

    Would anyone be able to help me understand either the scoring on this or where I may be missing something? I'm fairly certain I am hitting both the Wizard and Wand count, but seemingly only getting a little over half the points. I don't understand what could be wrong. Any Hints welcome.

    • 0
      SmartyNotchy  commented on Jan. 5, 2022, 10:53 a.m. edit 3

      I had the same problem as you. Apparently, you have to output the number of different wizards that the wand has obeyed, not the number of times the wand changes possession.

      • -1
        carlosg22  commented on March 6, 2022, 8:55 p.m.

        How were you able to calculate when the wand change possessions?

        • -1
          CubixularHelix  commented on April 20, 2022, 4:50 a.m.

          A set (which automatically removes duplicate elements) would suffice.

        • -1
          Tripas  commented on March 9, 2022, 9:16 p.m.

          I would recommend using a list which tracks the different owners of the wand.

          Every time a duel leads to the Elder Wand changing hands your program checks the list. Is the latest owner of the Elder Wand already on your list of previous owners? If not, have them added to the list. But if they are, then don't have your program change anything.