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
n
1
d
1
n
2
d
2
...
n
N
-1
d
N
-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 “
n
i
d
i
” 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 “
n
i
d
i
”, directs it to be placed next to the one that is numbered
n
i
, towards the direction given by
d
i
, 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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;

int n;

void solve() {
    vector<vector<int>> v (500,vector<int> (450,-1));
    v[220][220] = 0;
    int a,b;
    rep(k,n-1) {
        cin >> a >> b;
        rep(i,450) {
            rep(j,450) {
                if (v[i][j] == a) {
                    switch (b) {
                        case 0 : v[i][j-1] = k+1; break;
                        case 1 : v[i+1][j] = k+1; break;
                        case 2 : v[i][j+1] = k+1; break;
                        case 3 : v[i-1][j] = k+1; break;
                    }
                }
            }
        }
    }
    int up = 1e8, left = 1e8, down = 0, right = 0;
    rep(i,450) {
        rep(j,450) {
            if (v[i][j] >= 0) {
                up = min(up,i);
                left = min(left,j);
                down = max(down,i);
                right = max(right,j);
            }
        }
    }
    cout << right-left+1 << ' ' << down-up+1 << endl;
    return;
}

int main() {
    while (true) {
        cin >> n;
        if (n == 0) {
            return 0;
        }
        solve();
    }
}
accuracy: 1
time_ms: 590
memory_kb: 4040
final_rank: 16

code_snippet: #include <iostream>
#include <string>
#include <utility>
#include <limits.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using llint = long long;
using vec = vector<llint>;
using vvec = vector<vec>;

int main() {
	int N;

	while (true) {
		//入力
		cin >> N;
		if (N == 0) return 0;
		N--;
		vector<vector<int>> List(1000, vector<int>(1000, -1));
		List[500][500] = 0;
		vector<pair<int, int>> input(N);
		for (int i = 0; i < input.size(); i++) {
			cin >> input[i].first >> input[i].second;
		}

		//main_code
		int n = 1;
		for (int i = 0; i < input.size(); i++) {
			//x,yを探してくる。
			int index = input[i].first;
			int x;
			int y;
			for (int j = 0; j < List.size(); j++) {
				for (int k = 0; k < List[j].size(); k++) {
					if (index == List[j][k]) {
						y = j;
						x = k;
					}
				}
			}

			//隣をtrueにする
			int next = input[i].second;
			if (next == 0) {
				List[y][x - 1] = n;
			}
			else if (next == 1) {
				List[y + 1][x] = n;
			}
			else if (next == 2) {
				List[y][x + 1] = n;
			}
			else {
				List[y - 1][x] = n;
			}

			n++;
		}

		int hmin = List.size();
		int hmax = 0;
		int wmin = List[0].size();
		int wmax = 0;

		for (int i = 0; i < List.size(); i++) {
			for (int j = 0; j < List[i].size(); j++) {
				if (List[i][j] != -1) {
					hmin = min(hmin, i);
					hmax = max(hmax, i);
					wmin = min(wmin, j);
					wmax = max(wmax, j);
				}
			}
		}

		cout << wmax - wmin + 1 << " " << hmax - hmin + 1 << endl;

		//for (int i = 0; i < List.size(); i++) {
		//	for (int j = 0; j < List[i].size(); j++) {
		//		cout << List[i][j] << " ";
		//	}
		//	cout << endl;
		//}

	}

}
accuracy: 1
time_ms: 2230
memory_kb: 7332
final_rank: 20

code_snippet: #include <bits/stdc++.h>
//#include <ranges>
#define _USE_MATH_DEFINES
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
using namespace std;
//using mint = modint;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define V vector<ll>
#define Vi vector<int>
#define Vd vector<double>
#define Vb vector<bool>
#define Vs vector<string>
#define Vc vector<char>
#define VV vector<V>
using P = pair<ll,ll>;
using G = vector<vector<int>>;
#define VP vector<P>
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define INF 1LL << 60
#define inf 1e9
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

long long combi(long long n, long long k) {
  if (n == k || k == 0)
    return 1;
  else {
    return combi(n - 1, k - 1) + combi(n - 1, k);
  }
}
//整数かどうか
bool isNumber(const string& str)
{
  for (const char &c : str) {
        if (std::isdigit(c) == 0) return false;
    }
    return true;
}
//
/*
//最大公約数
ll gcd(ll a, ll b){
  if(b==0){
    return a;
  }else{
    return gcd(b, a%b);
  }
}
//最小公倍数
ll lcm(ll a, ll b){
  ll g=gcd(a,b);
  return a/g*b;
}
//
*/
//int di[] = {-1,0,1,0};
//int dj[] = {0,-1,0,1};

//s = regex_replace(s, regex("あ"), "う");
/*stiring で char を検索するときは
    s.find(c)!=string::npos
 */


/*//各桁の和
 int wa(int n){
  int sum =0;
  while(n>0){
    sum += n%10;
    n/=10;
  }
  return sum;
}
*/
/*
//階乗
 int ki(int i){
   int k = 1;
   for(int j = 1; j<=i; j++){
     k *= j;
   }
   return k;
 }
*/
/*log_x(b)
double logN(double x, double b) {
    return log(x) / log(b);
}
*/
//
/*//エラトステネスの篩 main関数内にsolve();を書き込む!!
const ll N = 1010101;//求める範囲
Vb isp(N+1,true);
void solve(){
  isp[0] = false;
  isp[1] = false;
  for(ll i = 2; i+i<=N;i++){
    if(isp[i])for(ll j = 2; i*j<=N;j++)isp[i*j] = false;
  }
}
//
*/
//
/*
//約数列挙　O(√n)
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
//
*/
//
/*
//素因数分解O(√n)
map< ll, ll > prime_factor(ll n) {
  map< ll, ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}
//
*/

/*
ll modpow(ll x, ll n, ll mod){
 while(n){
   ll resu = 1;
   if(n&1)res = (res * x) %mod;
   x = (x*x)%mod;
   n>>=1;
 }
 return res;
}
*/
/*
//最小二乗法
//aのb乗をmで割ったあまりを返す関数
//変数aはa^1→a^2→a^4→a^8→…と変化
ll power(ll a,ll b, ll m){
  ll p = a,ans = 1;
  rep(i,60){
    ll wari = (1LL<<i);
    if((b/wari)%2==1){
      ans=(ans*p)%m;
    }
    p=(p*p)%m;
  }
  return ans;
}
*/
/*
template <typename T> bool next_combination(const T first, const T last, int k) {
    const T subset = first + k;
    // empty container | k = 0 | k == n 
    if (first == last || first == subset || last == subset) {
        return false;
    }
    T src = subset;
    while (first != src) {
        src--;
        if (*src < *(last - 1)) {
            T dest = subset;
            while (*src >= *dest) {
                dest++;
            }
            iter_swap(src, dest);
            rotate(src + 1, dest + 1, last);
            rotate(subset, subset + (last - dest) - 1, last);
            return true;
        }
    }
    // restore
    rotate(first, subset, last);
    return false;
}
*/

//vector<int> dx = {1,0,-1,0,1,-1,-1,1};
//vector<int> dy = {0,1,0,-1,1,1,-1,-1};
//int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
//int dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};
//#define mod 998244353
//cout << mint.val() << endl;
//cout << fixed << setprecision(15) << y << endl;
#define yes "Yes"
#define no "No"
#define Yes "YES"
#define No "NO"

int S=409;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int main(){
  while(true){
    int N;cin >> N;
    if(N==0)return 0;
    else{
      if(N==1)cout << 1 << " "<< 1 << endl;
      else {
        VV g(S,V(S,0));
        VP p(N,{200,200});
        g[200][200]++;
        rep(i,N-1){
          int n,d;
          cin >> n >> d;
          auto [x,y]=p[n];
          x+=dx[d];
          y+=dy[d];
          g[y][x]++;
          p[i+1]={x,y};
        }
        ll sy=INF,sx=INF,ly=-INF,lx=-INF;
        rep(y,S)rep(x,S)if(g[y][x]){
          chmax(ly,y);
          chmax(lx,x);
          chmin(sy,y);
          chmin(sx,x);
        }
        /*rep(i,S){
          rep(j,S)cout << g[i][j] << " ";
          cout << endl;
        }*/
        cout << (lx-sx+1) << " " << (ly-sy+1) << endl;
      }
    }
  }

}
accuracy: 1
time_ms: 10
memory_kb: 4640
final_rank: 10

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

vi dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0};

pair<int, int> solve(int N){
    vc<pair<int, int>> place(N, {250, 250});
    vvi art(500, vi(500, 0));
    art[250][250] = 1;
    rep(i, N - 1){
        int n, d; cin >> n >> d;
        auto [x, y] = place[n];
        int nx = x + dx[d], ny = y + dy[d];
        art[nx][ny] = 1;
        place[i + 1] = {nx, ny};
    }
    int xl = inf, xr = 0, yl = inf, yr = 0;
    for (int i = 0; i < 500; i++) for (int j = 0; j < 500; j++) if (art[i][j]){
        xl = min(xl, i);
        xr = max(xr, i);
        yl = min(yl, j);
        yr = max(yr, j);
    }
    return {yr - yl + 1, xr - xl + 1};
}

int main(){
    vc<pair<int, int>> ans;
    while (true){
        int N; cin >> N;
        if (N == 0) break;
        ans.push_back(solve(N));
    }
    for (auto x : ans) cout << x.first << " " << x.second << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4096
final_rank: 7

code_snippet: # include <bits/stdc++.h>
# define all(v) std::begin(v), std::end(v)
# define rep(i, a, b) for (ll i = a; i < ll(b); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int inf = 1e9;

int main() {
    while (true) {
        ll N;
        cin >> N;
        if(N == 0) break;
        if(N == 1){
            cout << 1 << endl;
            continue;
        }
        vector<pll> pos(N + 1);
        vvl mas(410, vl(410, -1));
        int offset = 201;
        mas[0 + offset][0 + offset] = 0; 
        pos[0] = pll(0, 0);
        rep(i, 0, N - 1){
            ll n, d;
            cin >> n >> d;
            auto [nx, ny] = pos[n];
            if(d == 0) nx--;
            if(d == 1) ny++;
            if(d == 2) nx++;
            if(d == 3) ny--;
            pos[i + 1] = pll(nx, ny);
            mas[nx + offset][ny + offset] = i + 1;
        }
        ll minx = inf, maxx = -inf, miny = inf, maxy = -inf; 
        rep(i, 0, 410){
            rep(j, 0, 410){
                if(mas[i][j] != -1){
                    minx = min(minx, i);
                    miny = min(miny, j);
                    maxx = max(maxx, i);
                    maxy = max(maxy, j);
                }
            }
        }
        cout << abs(minx - maxx) + 1 << " " << abs(miny - maxy) + 1 << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4700
final_rank: 13

code_snippet: #include <iostream>
using namespace std;
#define INF (1 << 30)
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int N, n, d;
  while (cin >> N, N) {
    int f[500][500];
    for (int y = 0; y < 500; y++)
      for (int x = 0; x < 500; x++) {
        f[y][x] = -1;
      }
    f[250][250] = 0;
    for (int i = 1; i < N; i++) {
      cin >> n >> d;
      for (int y = 0; y < 500; y++)
        for (int x = 0; x < 500; x++) {
          if (f[y][x] == n) {
            f[y + dy[d]][x + dx[d]] = i;
          }
        }
    }
    int maxX = -INF, maxY = -INF, minX = INF, minY = INF;
    for (int y = 0; y < 500; y++)
      for (int x = 0; x < 500; x++) {
        if (f[y][x] != -1) {
          maxX = max(maxX, x);
          maxY = max(maxY, y);
          minX = min(minX, x);
          minY = min(minY, y);
        }
      }
    cout << maxX - minX + 1 << " " << maxY - minY + 1 << endl;
  }
}
accuracy: 1
time_ms: 460
memory_kb: 4064
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF =
    sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
template <typename V> typename V::value_type sum(const V &v) {
  return accumulate(all(v), 0);
}
void solve();
main {
  solve();
  return 0;
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void solve() {
  int N;
  while (cin >> N, N) {
    vpii e(N);
    e[0] = mp(300, 300);
    loop(i, 1, N) {
      int n, d;
      cin >> n >> d;
      e[i] = mp(e[n].fi + dy[d], e[n].se + dx[d]);
    }
    vvb t(600, vb(600));
    rep(i, N) t[e[i].fi][e[i].se] = true;
    int u = 0, l = 0, d = 599, r = 599;
    for (;;) {
      bool b = false;
      rep(i, 600) b |= t[u][i];
      if (b) {
        break;
      } else {
        u++;
      }
    }
    for (;;) {
      bool b = false;
      rep(i, 600) b |= t[i][l];
      if (b) {
        break;
      } else {
        l++;
      }
    }
    for (;;) {
      bool b = false;
      rep(i, 600) b |= t[d][i];
      if (b) {
        break;
      } else {
        d--;
      }
    }
    for (;;) {
      bool b = false;
      rep(i, 600) b |= t[i][r];
      if (b) {
        break;
      } else {
        r--;
      }
    }
    cout << r - l + 1 << ' ' << d - u + 1 << endl;
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3188
final_rank: 1

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int hw[450][450];
int m[210], d[210];
int main() {
  int n;
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i <= 420; i++) {
      for (int j = 0; j <= 420; j++) {
        hw[i][j] = -1;
      }
    }
    hw[201][201] = 0;
    for (int i = 1; i < n; i++) {
      cin >> m[i] >> d[i];
      for (int j = 1; j <= 410; j++) {
        for (int k = 1; k <= 410; k++) {
          if (hw[j][k] == m[i]) {
            if (d[i] == 0) {
              hw[j][k - 1] = i;
            }
            if (d[i] == 1) {
              hw[j + 1][k] = i;
            }
            if (d[i] == 2) {
              hw[j][k + 1] = i;
            }
            if (d[i] == 3) {
              hw[j - 1][k] = i;
            }
          }
        }
      }
    }
    int ansminw = 1000;
    for (int j = 1; j <= 410; j++) {
      for (int k = 1; k <= 410; k++) {
        if (hw[j][k] >= 0) {
          ansminw = min(ansminw, k);
          break;
        }
      }
    }
    int ansmaxw = 0;
    for (int j = 1; j <= 410; j++) {
      for (int k = 410; k > 0; k--) {
        if (hw[j][k] >= 0) {
          ansmaxw = max(ansmaxw, k);
          break;
        }
      }
    }
    int ansminh = 1000;
    for (int j = 1; j <= 410; j++) {
      for (int k = 1; k <= 410; k++) {
        if (hw[k][j] >= 0) {
          ansminh = min(ansminh, k);
          break;
        }
      }
    }
    int ansmaxh = 0;
    for (int j = 1; j <= 410; j++) {
      for (int k = 410; k > 0; k--) {
        if (hw[k][j] >= 0) {
          ansmaxh = max(ansmaxh, k);
          break;
        }
      }
    }
    cout << (ansmaxw - ansminw) + 1 << " " << (ansmaxh - ansminh) + 1 << endl;
  }
}
accuracy: 1
time_ms: 470
memory_kb: 3728
final_rank: 9

code_snippet: #include <cstring>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int p[801][801], n, N, d;
  while (cin >> N && N) {
    rep(i, 801) rep(j, 801) p[i][j] = -1;
    p[400][400] = 0;
    for (int k = 1; k < N; k++) {
      cin >> n >> d;
      rep(i, 801) rep(j, 801) if (p[i][j] == n) {
        if (d == 0)
          p[i][j - 1] = k;
        if (d == 1)
          p[i + 1][j] = k;
        if (d == 2)
          p[i][j + 1] = k;
        if (d == 3)
          p[i - 1][j] = k;
      }
    }
    int fx = 801, fy = 801, lx = 0, ly = 0;
    rep(i, 801) {
      rep(j, 801) {
        if (~p[i][j]) {
          lx = max(j, lx);
          fx = min(j, fx);
          ly = max(i, ly);
          fy = min(i, fy);
        }
      }
    }
    cout << lx - fx + 1 << " " << ly - fy + 1 << endl;
  }
}
accuracy: 1
time_ms: 1190
memory_kb: 5580
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
int n;
int main() {
  int dy[] = {0, 1, 0, -1};
  int dx[] = {-1, 0, 1, 0};
  while (cin >> n) {
    int data[500][500] = {};
    if (n == 0)
      break;
    vector<pii> point(300);
    point[0] = pii(250, 250);
    data[point[0].first][point[0].second] = 1;
    rep(i, n - 1) {
      int num, dir;
      cin >> num >> dir;
      data[point[num].first + dy[dir]][point[num].second + dx[dir]] = 1;
      point[i + 1] =
          pii(point[num].first + dy[dir], point[num].second + dx[dir]);
    }
    int maxw = 0;
    int left = 1000, right = -1;
    rep(i, 500) {
      rep(j, 500) {
        if (data[i][j] == 1) {
          left = min(left, j);
          right = max(right, j);
        }
      }
    }
    maxw = max(maxw, right - left + 1);
    int maxh = 0;
    int up = 1000, down = -1;
    rep(j, 500) {
      rep(i, 500) {
        if (data[i][j] == 1) {
          up = min(up, i);
          down = max(down, i);
        }
      }
    }
    maxh = max(maxh, down - up + 1);
    cout << maxw << " " << maxh << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4028
final_rank: 4

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
const int INF = 1e+8;
int s[500][500];
int dx[4] = {-1, 0, +1, 0};
int dy[4] = {0, +1, 0, -1};
void init() {
  for (int y = 0; y < 500; y++) {
    for (int x = 0; x < 500; x++) {
      s[y][x] = -1;
    }
  }
}
void search(int id, int d, int now) {
  for (int y = 0; y < 500; y++) {
    for (int x = 0; x < 500; x++) {
      if (s[y][x] == id) {
        int mx = x + dx[d];
        int my = y + dy[d];
        s[my][mx] = now;
        return;
      }
    }
  }
}
int main() {
  int N;
  while (cin >> N, N) {
    init();
    s[250][250] = 0;
    for (int i = 1; i < N; i++) {
      int n, d;
      cin >> n >> d;
      search(n, d, i);
    }
    int min_x, min_y, max_x, max_y;
    min_x = min_y = INF;
    max_x = max_y = 0;
    for (int y = 0; y < 500; y++) {
      for (int x = 0; x < 500; x++) {
        if (s[y][x] >= 0) {
          max_x = max(max_x, x);
          max_y = max(max_y, y);
          min_x = min(min_x, x);
          min_y = min(min_y, y);
        }
      }
    }
    cout << (max_x - min_x + 1) << " " << (max_y - min_y + 1) << endl;
  }
}
accuracy: 1
time_ms: 330
memory_kb: 4048
final_rank: 11

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int main() {
  const int dx[4] = {-1, 0, 1, 0};
  const int dy[4] = {0, 1, 0, -1};
  int c[500][500], N;
  while (cin >> N, N) {
    int min_x = 250, max_x = 250, min_y = 250, max_y = 250;
    for (int y = 0; y < 500; y++) {
      for (int x = 0; x < 500; x++) {
        c[y][x] = (x == 250 && y == 250) ? 0 : -1;
      }
    }
    for (int i = 0; i < N - 1; i++) {
      int n, d;
      cin >> n >> d;
      for (int y = min_y; y <= max_y; y++) {
        for (int x = min_x; x <= max_x; x++) {
          if (c[y][x] == n) {
            int mx = x + dx[d], my = y + dy[d];
            c[my][mx] = i + 1;
            max_x = max(max_x, mx);
            max_y = max(max_y, my);
            min_x = min(min_x, mx);
            min_y = min(min_y, my);
            break;
          }
        }
      }
    }
    cout << (max_x - min_x + 1) << " " << (max_y - min_y + 1) << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 4032
final_rank: 6

code_snippet: #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 2147483647
#define INF 3223372036854775807
#define MOD 1000000007
#define mod 998244353
#define INT_MAX_ 2147483647
#define EPS (1e-10)
//#define REP(i, a, n) fo-r (ll i = a; i < (ll)(n); i++)
//#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
//#define REP(i,x,y) for(register int i=(x); i<(y); i++)
//#define REPE(i,x,y) for(register int i=(x); i<=(y); i++)
//#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define pi acos(-1.0)
#define me memset
// /#define bit(n,k) ((n>>k)&1)
#define lg length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<Pll,ll> a,pair<Pll,ll> b){
        return a.fi.se>b.fi.se;
}
ll cmp2(pair<ll,ll> a,pair<ll,ll> b){
    if(a.fi!=b.fi)
        return  a.fi>b.fi;
    else
        return a.se<b.se;
}
//----------------------------------------------------------------------
int a[500][500];
//----------------------------------------------------------------------
int main(int argc, char * argv[]){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_t=clock();
    //freopen("big.txt", "r", stdin);
    //freopen("out3.txt", "w", stdout);
    //------------------------------
    int N;cin>>N;
    while(0!=N){
        me(a,-1,sizeof(a));
        int maxx=250,maxy=250;
        int minx=250, miny=250;
        a[250][250]=0;
        for(int i=0;i<N-1;i++){
            int n,d;cin>>n>>d;
            for(int x=0;x<500;x++){
                for(int y=0;y<500;y++){
                    if(a[x][y]==n){
                        if(d==0){
                            a[x][y-1]=i+1;
                            chmin(miny,y-1);
                        }
                        else if(d==1){
                            a[x-1][y]=i+1;
                            chmin(minx,x-1);
                        }
                        else if(d==2){
                            a[x][y+1]=i+1;
                            chmax(maxy,y+1);
                        }
                        else if(d==3){
                            a[x+1][y]=i+1;
                            chmax(maxx,x+1);
                        }
                    }
                }
            }
        }
        cout<<1+maxy-miny<<" "<<1+maxx-minx<<Endl;
        cin>>N;
    }
    //------------------------------
    //fclose(stdin);
    //fclose(stdout);
    //ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
    //------------------------------- 
    return 0;
}
//----------------------------------------------------------------------
accuracy: 1
time_ms: 490
memory_kb: 4456
final_rank: 17

code_snippet: #include<bits/stdc++.h>
//#include <atcoder/all>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a)  (a).begin(),(a).end()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define sz(x) ((int)x.size())

using namespace std;
//using namespace atcoder;
using ll = long long int;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
//using mint = modint1000000007;

const ll ZER = 0;
const ll MOD = 1e9 + 7;


int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    //hi, si, mi, ue
    int dy[4] = {0, 1, 0, -1};
    int dx[4] = {-1, 0, 1, 0};
    while(cin >> n){
        if(n == 0)break;
        vector<vector<int>> fld(500, vector<int>(500, -1));
        //a:何番目においたかの b:隣
        n--;
        vector<int> a(n), b(n);
        rep(i, n)cin >> a[i] >> b[i];
        fld[200][200] = 0;
        rep(i, n){
            rep(y, 401){
                rep(x, 400){
                    if(fld[y][x] == a[i]){
                        fld[y + dy[b[i]]][x + dx[b[i]]] = i + 1;
                    }
                }
            }
        }
        int lx = INF, rx = 0, ly = INF, ry = 0;
        rep(i, 401){
            rep(j, 401){
                if(fld[i][j] >= 0){
                    chmin(lx, j);
                    chmin(ly, i);
                    chmax(rx, j);
                    chmax(ry, i);
                }
            }
        }
        cout << rx - lx + 1 << " " << ry - ly + 1 << endl;
    }
}
accuracy: 1
time_ms: 500
memory_kb: 4168
final_rank: 15

code_snippet: # include <bits/stdc++.h>
# define rep(i,n) for(int i=0; i<(n); ++i)
# define FI first
# define SE second
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;

struct phash {
   inline size_t operator ()(const pair<int,int>& p) const {
      const auto h1 = hash<int>()(p.first);
      const auto h2 = hash<int>()(p.second);
      return h1 ^ (h2 << 1);
   }
};

void Main(int N, vi n, vi d) {
   unordered_map<int, pii> pos;
   pos[0] = { 0, 0 };
   rep (i, N-1) {
      int num = n[i];
      int dir = d[i];
      switch (dir) {
         case 0: // 左側
            pos[i+1] = { pos[num].FI - 1, pos[num].SE };
            break;
         case 1: // 下側
            pos[i+1] = { pos[num].FI, pos[num].SE - 1 };
            break;
         case 2: // 右側
            pos[i+1] = { pos[num].FI + 1, pos[num].SE };
            break;
         case 3: // 上側
            pos[i+1] = { pos[num].FI, pos[num].SE + 1 };
            break;
         default:
            pos[i+1] = { pos[num].FI, pos[num].SE };
      }
   }

   int width = 0;
   int height = 0;
   for (int i = 0; i < N; ++i) {
      for (int k = i + 1; k < N; ++k) {
         width = max(width, abs(pos[i].FI - pos[k].FI));
         height = max(height, abs(pos[i].SE - pos[k].SE));
      }
   }

   cout << width + 1 << " " << height + 1 << endl;
}

int main() {
   while (true) {
      int N;
      cin >> N;
      if (!N) break;
      vi n(N-1), d(N-1);
      rep (i, N-1) cin >> n[i] >> d[i];
      Main(N, n, d);
   }
   return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3364
final_rank: 3

code_snippet: /* ICPC */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pint;
const ll MOD = 1000000007;
#define rep(i, n) for(ll i = ll(0); i < ll(n); i++)
#define Rep(i, n) for(ll i = ll(1); i < ll(n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

bool Ret(ll a = 0, ll b = 0, ll c = 0, ll d = 0) {
  if (a == 0 && b == 0 && c == 0 && d == 0) return true;
  else return false;
}

struct Point{ll x, y;};

ll dx[4] = {-1, 0, 1, 0};
ll dy[4] = {0, 1, 0, -1};

void solve(ll n) {
  vector<ll> v(n), type(n);
  rep (i, n) cin >> v[i] >> type[i];
  vector<vector<bool>> flag(1000,vector<bool>(1000,false));
  map<ll, Point> mp;
  mp[0] = Point{500, 500};
  flag[mp[0].y][mp[0].x] = true;
  rep (i, n) {
    mp[i + 1] = Point{mp[v[i]].x + dx[type[i]], mp[v[i]].y + dy[type[i]]};
    flag[mp[v[i]].y + dy[type[i]]][mp[v[i]].x + dx[type[i]]] = true;
  }
  ll l = 1e16, r = -1;
  ll u = -1, d = 1e16;
  rep (i, 1000) {
    rep (j, 1000) {
      if (flag[i][j]) {
        chmin (l, j);
        chmax (r, j);
        chmax (u, i);
        chmin (d, i);
      }
    }
  }
  cout << r - l + 1 << ' ' << u - d + 1 << endl;
}

int main() {
  IOS;
  while (1) {
    ll n; cin >> n;
    if (Ret(n)) break;
    solve(n - 1);
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3256
final_rank: 2

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int a[600][600];
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {-1, 0, 1, 0};

void solve() {
	int n;
    while(cin >> n) {
        if(n == 0) break;
        vector<P> pos(n, {-1, -1});
        pos[0] = P{300, 300};
        rep(i, 600) {
            rep(j, 600) {
                a[i][j] = -1;
            }
        }
        a[300][300] = 0;
        rep(i, n - 1) {
            int m, d; cin >> m >> d;
            a[pos[m].fr + dx[d]][pos[m].sc + dy[d]] = i + 1;
            pos[i + 1] = P{pos[m].fr + dx[d], pos[m].sc + dy[d]};
        }
        int xmi = inf, xma = -inf, ymi = inf, yma = -inf;
        rep(i, 600) {
            rep(j, 600) {
                if(a[i][j] != -1) {
                    chmax(xma, i); chmin(xmi, i);
                    chmax(yma, j), chmin(ymi, j);
                }
            }
        }
        cout << yma - ymi + 1 << " " << xma - xmi + 1 << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4560
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int main() {
    int Map[500][500];
    int X[210], Y[210];
    while (true) {
        int N;
        cin >> N;
        if (N == 0) break;
        for (int i = 0; i < 500; ++i) {
            for (int j = 0; j < 500; ++j) {
                Map[i][j] = -1;
            }
        }
        for (int i = 0; i < 210; ++i) {
            X[i] = Y[i] = -1;
        }
        int offset = 500 / 2;
        Map[0 + offset][0 + offset] = 0;
        X[0] = Y[0] = offset;
        for (int i = 1; i < N; ++i) {
            int n, d;
            cin >> n >> d;
            int nx = X[n] + dx[d];
            int ny = Y[n] + dy[d];
            Map[nx][ny] = i;
            X[i] = nx;
            Y[i] = ny;
        }

        int lx = offset, rx = offset, ly = offset, ry = offset;
        for (int i = 0; i < 500; ++i) {
            for (int j = 0; j < 500; ++j) {
                if (Map[i][j] != -1) {
                    lx = min(lx, i);
                    rx = max(rx, i);
                    ly = min(ly, j);
                    ry = max(ry, j);
                }
            }
        }
        // cout << "-----" << endl;
        // for (int i = offset - 10; i < offset + 10; ++i) {
        //     for (int j = offset - 10; j < offset + 10; ++j) {
        //         cout << Map[i][j] << " ";
        //     }
        //     cout << endl;
        // }

        // cout << "-----" << endl;
        cout << ry - ly + 1 << " " << rx - lx + 1 << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4036
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main()
{
	int n;
	while(cin>>n,n!=0){
		vector<vector<int>> p(4*n,vector<int>(4*n,0));

		map<int,P> m;
		m[0]=P(2*n,2*n);
		p[2*n][2*n]=1;
		vector<tuple<int,int,int>> s(n-1);
		rep(i,n-1){
			int a,b;
			cin>>a>>b;
			tuple<int,int,int> t= make_tuple(a,i+1,b);
			s[i]=t;
		}
		sort(all(s));
		rep(i,n-1){
			tuple<int,int,int> t=s[i];
			int idx=get<1>(t);
			int from=get<0>(t);
			int angle=get<2>(t);

			P point=m[from];
			// cout<<angle<<endl;
			if(angle==0) point.second--;
			if(angle==1) point.first++;
			if(angle==2) point.second++;
			if(angle==3) point.first--;

			m[idx]=point;
			p[point.first][point.second]=1;
		}
		int l=-1,r=-1,u=-1,d=-1;
		rep(i,4*n){
			rep(j,4*n){
				if(p[i][j]==1){
					if(u<0)u=i;
					d=i;
				}
			}
		}
		rep(j,4*n){
			rep(i,4*n){
				if(p[i][j]==1){
					if(l<0)l=j;
					r=j;
				}
			}
		}
		
		cout<<abs(l-r)+1<<" "<<abs(u-d)+1<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5396
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <map>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
const int dy[] = {0, 1, 0, -1};
const int dx[] = {-1, 0, 1, 0};
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n, n)
    {
        map<int, pair<int, int>> mp;
        mp[0] = make_pair(250, 250);
        for(int i = 1; i < n; i++)
        {
            int m, d; cin >> m >> d;
            pair<int, int> p = mp[m];
            mp[i] = make_pair(p.first + dy[d], p.second + dx[d]);
        }
        int table[600][600] = {};
        for(auto x : mp) table[x.second.first][x.second.second] = 1;
        int b = 0, t = 600, l = 600, r = 0;
        for(int i = 0; i < 600; i++)
        {
            for(int j = 0; j < 600; j++)
            {
                if(table[i][j]) t = min(t, i);
            }
        }
        for(int i = 599; i >= 0; i--)
        {
            for(int j = 0; j < 600; j++)
            {
                if(table[i][j]) b = max(b, i);
            }
        }
        for(int j = 0; j < 600; j++)
        {
            for(int i = 0; i < 600; i++)
            {
                if(table[i][j]) l = min(l, j);
            }
        }
        for(int j = 599; j >= 0; j--)
        {
            for(int i = 0; i < 600; i++)
            {
                if(table[i][j]) r = max(r, j);
            }
        }
        cout << r - l  + 1 << " " << b - t + 1 << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4568
final_rank: 12