## COCI '18 Contest 4 #1 Elder

View as PDF

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 duels that were held one after another, answer the following questions:

1. Which wizard did the wand obey after all duels?
2. How many different wizards did the wand obey?

#### Input

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.

#### Output

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.

#### Scoring

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

C
3

#### 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

N
1

#### Sample Input 3

X
4
A X
B X
X A
D A

#### Sample Output 3

X
2

• commented on Jan. 26, 2023, 4:23 p.m.

what does a yellow "AC" mean?

• commented on Jan. 2, 2023, 2:05 p.m.

Hi. Can someone please review and assist me with my code? I keep getting 38/50 even though I am sure my code works correctly.

• commented on Jan. 2, 2023, 3:46 p.m.

NVM, I got it to work. Turns out, using a set is very useful.

• commented on Dec. 21, 2022, 2:44 a.m.

Can anyone explain to me how the third input and output works?

• commented on Dec. 25, 2022, 8:00 p.m. edited

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:

X

2

• commented on Dec. 19, 2022, 6:25 a.m.

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.

• commented on Dec. 19, 2022, 8:37 p.m.

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.

This tells us that the only way to get the wand is to defeat wizard A (where wizard A has the wand).

N
5
D A
N B
B A
C D
F A

In each line, the first wizard defeats the second wizard (in this case, D defeats A, then N defeats B, and so on). Remember, since N begins 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 N and is controlled by 1 wizard throughout.

• commented on Dec. 20, 2022, 12:15 a.m.

• commented on Sept. 4, 2022, 7:58 a.m.

Where are the solutions? How to check if my solution can be written better?

• commented on Sept. 4, 2022, 9:57 a.m. edited

You must solve the problem first if you wish to see the other solutions. Advice: think about the problem very carefully and test it fully on your local IDE before submitting.

A tip: you don't need to record every wizard. You only have to keep track of the current owner and all the previous owners.

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

• 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

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

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

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

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

Ohhh, I see now haha.

• 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 ?

• 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

A
3
B A
C B
A C

A
4
• 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?

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

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

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

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

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.

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

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

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

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

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

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

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

• commented on Dec. 6, 2022, 5:06 p.m.

I think using sets is a real elegant way of solving this, but if you're aiming for understanding how to better track elements in lists, then lists make sense. I used a set after realising that it fits really nicely here, as you don't have to worry about duplicates at all.