#DSU0000075. Những chuyến đi của đàn bò (COW Traversals)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những chuyến đi của đàn bò (COW Traversals)
COW Traversals
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
There are cows, with cow living in barn and having best friend , possibly itself. Over nights, on night , cow begins hosting a party of type . If it was already hosting a party, that type is replaced; all other parties continue.
Every cow checks its own barn for a party. If none exists, it visits the best friend's barn, repeating the process. A cow that enters a cycle without finding a party gives up. For each night, count the cows attending parties of types C, O, and W.
Every night, all cows start at their own barns rather than continuing from where they stopped the previous night. A cow joins the first party encountered on its route and stops moving. If it follows a cycle without encountering a party, it attends no party that night.
A party created on night takes effect that night and remains active on subsequent nights until its own barn changes the party type. Changing one barn's party does not cancel parties at other barns.
Input
The first line contains , the second contains , followed by and then lines containing .
Output
Print lines, each with three integers: the numbers of cows attending party types C, O, and W, respectively.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
5
2 3 4 5 4
4
2 C
4 C
4 W
2 O
Output
2 0 0
5 0 0
2 0 3
0 2 3
Explanation
On night , cows reach the C party at barn . On night , barn adds a C party attended by cows . On night , that party changes to W, giving cows at C and at W. On night , barn changes its party to O, so the result is .