Problem:


Problem A: Pablo Squarson's Headache

Pablo Squarson is a well-known cubism artist.  This year's theme for Pablo Squarson is "Squares".  Today we are visiting his studio to see how his masterpieces are given birth.

At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size.  Pablo Squarson puts one of the squares on the table.  Then he places some other squares on the table in sequence.  It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.

Oh!  The first piece of artwork is done.  Pablo Squarson seems satisfied with it.  Look at his happy face.

Oh, what's wrong with Pablo?  He is tearing his hair!  Oh, I see.  He wants to find a box that fits the new piece of work but he has trouble figuring out its size.  Let's help him!

Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height.  It is known that the size of each square is 1.  You may assume that Pablo does not put a square on another.

I hear someone murmured "A smaller box will do".  No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".

Input

The input consists of a number of datasets.  Each dataset represents the way Pablo made a piece of his artwork.  The format of a dataset is as follows.

N

n1 d1

n2 d2

...

nN-1 dN-1

The first line contains the number of squares (= N) used to make the piece of artwork.  The number is a positive integer and is smaller than 200.

The remaining (N-1) lines in the dataset are square placement instructions.  The line &ldquo;ni di&rdquo; indicates placement of the square numbered i (≤ N-1).  The rules of numbering squares are as follows.  The first square is numbered "zero".  Subsequently placed squares are numbered 1, 2, ..., (N-1).  Note that the input does not give any placement instruction to the first square, which is numbered zero.

A square placement instruction for the square numbered i, namely &ldquo;ni di&rdquo;, directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
 

For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.

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

Output

For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space.  Each line should not contain any other characters.

Sample 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

Output for the Sample Input

1 1
3 3
4 4
5 6



Sample 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


Sample output:
1 1
3 3
4 4
5 6

Submission status: Accepted

Solution code:
"""
5
0 0
0 1
0 2
0 3
"""
import copy
while True:
  N = int(input())
  if N==0:
    break
  T = [[0,0]]
  for i in range(N-1):
    n,d = map(int,input().split())
    S = copy.copy(T[n])
    if d==0:
      S[0] = S[0]-1
    elif d==1:
      S[1] = S[1]-1
    elif d==2:
      S[0] = S[0]+1
    else:
      S[1] = S[1]+1
    T.append(S)
  W,H = 0,0
  T.sort(key=lambda x:x[0])
  W = T[N-1][0] - T[0][0] + 1
  T.sort(key=lambda x:x[1])
  H = T[N-1][1] - T[0][1] + 1
  print(W,H)

Submission status: Accepted

Solution code:
while True:
  N = int(input())
  if N==0:
    break
  squa = [[0,0]]
  xmax, xmin, ymax, ymin = 0, 0, 0, 0
  for i in range(N-1):
    n, d = map(int, input().split())
    if d==0:
      squa.append([squa[n][0]-1, squa[n][1]])
      xmin = min(xmin, squa[n][0]-1)
    elif d==1:
      squa.append([squa[n][0], squa[n][1]-1])
      ymin = min(ymin, squa[n][1]-1)
    elif d==2:
      squa.append([squa[n][0]+1, squa[n][1]])
      xmax = max(xmax, squa[n][0]+1)
    elif d==3:
      squa.append([squa[n][0], squa[n][1]+1])
      ymax = max(ymax, squa[n][1]+1)
  print(xmax-xmin+1, ymax-ymin+1)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    while True:
        N = int(input())
        if N == 0:
            break
        # 左, 下, 右, 上
        wak = {0: [0, 0]}
        ml = float("inf")
        md = float("inf")
        mr = -float("inf")
        mu = -float("inf")
        for i in range(1, N):
            n, d = map(int, input().split())
            if d == 0:
                wak[i] = [wak[n][0] - 1, wak[n][1]]
            elif d == 1:
                wak[i] = [wak[n][0], wak[n][1] - 1]
            elif d == 2:
                wak[i] = [wak[n][0] + 1, wak[n][1]]
            elif d == 3:
                wak[i] = [wak[n][0], wak[n][1] + 1]
        for k, v in wak.items():
            ml = min(v[0], ml)
            md = min(v[1], md)
            mr = max(v[0], mr)
            mu = max(v[1], mu)
        print(abs(mr - ml) + 1, abs(mu - md) + 1)
        # print(wak)
        # print("--------------------")

Submission status: Accepted

Solution code:
def main():
    while True:
        sq_sum = int(input())
        if sq_sum == 0: break
        
        abs_list = [[0, 0]]
        for i in range(sq_sum - 1):
            n, d = map(int, input().split())
            abs_list.append(abs_p(abs_list, n, d))
        xlist = []
        ylist = []
        for i in range(len(abs_list)):
            xlist.append(abs_list[i][0])
            ylist.append(abs_list[i][1])
        print(str(max(xlist) - min(xlist) + 1) + " " + str(max(ylist) - min(ylist) + 1))
    return 0

def abs_p(abs_list, fromm, wch):
    if wch == 0:return[abs_list[fromm][0] - 1, abs_list[fromm][1]]
    if wch == 1:return[abs_list[fromm][0], abs_list[fromm][1] + 1]
    if wch == 2:return[abs_list[fromm][0] + 1, abs_list[fromm][1]]
    if wch == 3:return[abs_list[fromm][0], abs_list[fromm][1] - 1]
    return 0
main()

Submission status: Accepted

Solution code:
while 1:
    N=int(input())
    if N==0:break
    tile=[[0,0]]
    xma,xmi,yma,ymi=0,0,0,0
    for i in range(N-1):
        n,d=map(int,input().split())
        if d==0:
            tile.append([tile[n][0]-1,tile[n][1]])
            xmi=min(xmi,tile[n][0]-1)
        elif d==1:
            tile.append([tile[n][0],tile[n][1]-1])
            ymi=min(ymi,tile[n][1]-1)
        elif d==2:
            tile.append([tile[n][0]+1,tile[n][1]])
            xma=max(xma,tile[n][0]+1)
        elif d==3:
            tile.append([tile[n][0],tile[n][1]+1])
            yma=max(yma,tile[n][1]+1)
    print(xma-xmi+1,yma-ymi+1)

Submission status: Accepted

Solution code:
n=int(input())
while n!=0:
    stack_x=[0]
    stack_y=[0]
    position=[[0,0] for i in range(n)]
    for i in range(1,n):
        x,y=map(int,input().split())
        if y==1:
            position[i][0]=position[x][0]
            position[i][1]=position[x][1]-1
            stack_y.append(position[i][1])
        elif y==3:
            position[i][0]=position[x][0]
            position[i][1]=position[x][1]+1
            stack_y.append(position[i][1])
        elif y==0:
            position[i][0]=position[x][0]-1
            position[i][1]=position[x][1]
            stack_x.append(position[i][0])
        elif y==2:
            position[i][0]=position[x][0]+1
            position[i][1]=position[x][1]
            stack_x.append(position[i][0])
    print(max(stack_x)-min(stack_x)+1,max(stack_y)-min(stack_y)+1)
    n=int(input())

Submission status: Accepted

Solution code:
def add(a,b):
    return (a[0]+b[0],a[1]+b[1])
    
dic={0:(-1,0),1:(0,-1),2:(1,0),3:(0,1)}

while 1:
    n=int(input())
    if n==0:
        break
    elif n==1:
        print(1,1)
        continue
    v=[(0,0)]
    for i in range(n-1):
        a,b=map(int,input().split())
        v.append(add(v[a],dic[b]))
    solve=sorted(v)
    print(solve[-1][0]-solve[0][0]+1,end=' ')
    solve=sorted(v,key=lambda x:x[1])
    print(solve[-1][1]-solve[0][1]+1)

Submission status: Runtime Error

Solution code:
while True:
    N=int(input())
    if N==0:break
    t=[0]
    y=[0]
    lst=list(range(N))
    for k in lst[1:]:
        [x,d]=input().slice()
        [x,d]=[int(x),int(d)]
        if d==0:
            t=t+[t[x]]
            y=y+[y[x]-1]
        if d==1:
            t=t+[t[x]-1]
            y=y+[y[x]]
        if d==2:
            t=t+[t[x]]
            y=y+[y[x]+1]
        if d==3:
            t=t+[t[x]+1]
            y=y+[y[x]]
    ht=0
    lt=0
    hy=0
    ly=0
    for f in range(N):
        if ht<t[f]:
            ht+=1
        if lt>t[f]:
            lt-=1
        if hy<y[f]:
            hy+=1
        if ly>y[f]:
            ly-=1
    print(ht-lt+1,hy-ly+1)