Under construction

Still being built. Things may change

Pablo Squarson's Headache

Interview

Time limit1sMemory limit128 MB

Summary
Given how N unit squares are attached one by one to an existing square in one of four directions, compute the width and height of the resulting figure.
Level

Medium4 of 10

Topics
Simulation, Array, Hash map, Implementation
Solved
No attempts yet

Problem

Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares".

In the center of his studio there is a huge table, and beside it are many squares, all of the same size. Pablo first places one square on the table. Then he places the remaining squares one by one, always attaching each new square flush against one side of a square that is already on the table, with machine-like precision. No square is ever placed on top of another.

When a piece is finished, Pablo wants to know the size of the box that would fit it, that is, the width and the height of the artwork. Given a record of how Pablo built a piece, write a program that computes its width and height. The side length of each square is 1.

Input

The input consists of several datasets. Each dataset describes how Pablo built one piece of artwork and has the following format.

N
n1 d1
n2 d2
...
nN-1 dN-1

The first line contains the number of squares NN used to make the piece. NN is a positive integer smaller than 200200.

The remaining N−1N-1 lines are square placement instructions. The squares are numbered in the order they are placed: the first square is numbered 00, and the squares placed after it are numbered 1,2,…,N−11, 2, \dots, N-1. No placement instruction is given for square 00.

The ii-th instruction "ni din_i\ d_i" means that square ii (1≤i≤N−11 \le i \le N-1) is placed next to square nin_i in the direction did_i, where did_i is 00 for left, 11 for down, 22 for right, and 33 for up.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output one line containing the width and the height of the piece of artwork, separated by a single space. Each line must not contain any other characters.

Examples4

  1. Example 1

    Input
    1
    5
    0 0
    0 1
    0 2
    0 3
    12
    0 0
    1 0
    2 0
    3 1
    4 1
    5 1
    6 2
    7 2
    8 2
    9 3
    10 3
    10
    0 2
    1 2
    2 2
    3 2
    2 1
    5 1
    6 1
    7 1
    8 1
    0
    
    Expected output
    1 1
    3 3
    4 4
    5 6
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    1 1
    
  3. Example 3

    Input
    2
    0 2
    0
    
    Expected output
    2 1
    
  4. Example 4

    Input
    2
    0 3
    0
    
    Expected output
    1 2