Problems
There are
On night
Every night, each cow will attempt to go to a party. If a cow is not hosting a party, they will check their best friend's barn, and if there is no party there, will follow their best friend to wherever they are going (who might also follow their best friend and so on). It is possible that a cow might never find a party and will then give up for the night.
Compute for each night, the number of cows that end up at a party of type
Input
The first line contains
The second line contains
The third line contains
The next
Output
Output
Subtask
| # | Score | Condition |
|---|---|---|
| #1 | 10 | |
| #2 | 20 | |
| #3 | 30 | |
| #4 | 40 | No additional constraints |
Example
5
2 3 4 5 4
4
2 C
4 C
4 W
2 O
2 0 0
5 0 0
2 0 3
0 2 3
On night 1, there is only one party of type
On night 2, there is a new party of type
On night 3, the party at barn 4 is changed to type
On night 4, the party at barn 2 is changed to type