Submit | All submissions | Best solutions | Back to list |
CTTC - Counting Child |
Little boy Arik is playing with tree. He is counting the number of children of each node.
For this, he starts with root node 1. And traverse the full tree in DFS (Depth First Search) style. For simplicity, see the following figure.
For Tree - 1, his traversing sequence is 1 2 2 3 3 1. He starts with root node 1. Then he starts visiting first child 2, then finishes his traversing at 2. Then he starts visiting second child 3, then finishes his traversing at 3. And then finally finishes his traversing at 1.
For Tree - 2, his traversing sequence is 1 2 4 4 2 3 5 5 6 6 3 1.
Input
Input starts with an integer t (1 <= t <= 20), which denotes the case numbers.
For each case, you are given n (1 <= n <= 100) in first line. Next line contains 2n integers denoting his sequences of traversing. All nodes are between 1 to n. The root node is always node 1.
Output
For each case print the case number in the first line. Following n lines print each node from 1 to n and number of child of the node. Print a blank line after each test cases. See the sample I/O section for more details about output format.
Example
Input
2 3 1 2 2 3 3 1 6 1 2 4 4 2 3 5 5 6 6 3 1
Output
Case 1: 1 -> 2 2 -> 0 3 -> 0 Case 2: 1 -> 2 2 -> 1 3 -> 2 4 -> 0 5 -> 0 6 -> 0
Added by: | Sarwar |
Date: | 2017-12-30 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |
hide comments
2020-09-02 15:20:39
Use stack and map for counting;) |
|
2020-08-04 00:12:15 David
My bad. I finally understand the problem. Last edit: 2022-06-23 19:15:32 |
|
2020-05-23 10:40:48
Wrong answer, but my solution give me correct output based on the examples. Am I missing something? I'm printing: Case 1: 1 -> 2 2 -> 0 3 -> 0 Last edit: 2020-05-23 13:05:20 |
|
2020-01-06 01:08:45
Output is "1 -> 2" there are spaces before and after arrows ..... |
|
2018-09-18 15:26:00 Yauhen
no need of dsu or stack just parents array is enough. |
|
2017-12-31 16:15:01
no need of dsu just a stack is enough. |
|
2017-12-31 08:32:16
Happy New Year for all SPOJ users! |
|
2017-12-30 18:39:23
dsu works fine here |