#include <vector>
#include <algorithm>
static int depth_arr[10005];
static int max_d = 0;
static int max_node = 0;
static int max_before_B = 0;
int send_message(int N, int i, int Pi) {
int B = std::min(7, N - 1);
if (i == 1) {
depth_arr[0] = 0;
max_d = 0;
max_node = 0;
max_before_B = 0;
}
depth_arr[i] = depth_arr[Pi] + 1;
if (i == 1) {
max_d = depth_arr[1];
max_node = 1;
} else {
if (depth_arr[i] > max_d) {
max_d = depth_arr[i];
max_node = i;
}
}
if (i == N - B - 1) {
max_before_B = max_node;
}
if (i >= N - B) {
if (max_node <= N - B - 1) {
int step = i - (N - B);
int digit = (max_before_B >> (2 * step)) & 3;
return digit + 1;
} else {
if (i == max_node) return 0;
else return 1;
}
}
return 0;
}
std::pair<int,int> longest_path(std::vector<int> S) {
int N = S.size();
int B = std::min(7, N - 1);
int abort_seen = 0;
int true_max = -1;
for (int i = N - B; i < N; i++) {
if (S[i] == 0) {
abort_seen = 1;
true_max = i;
}
}
if (abort_seen) {
if (true_max == -1) true_max = 0;
return {0, true_max};
} else {
int val = 0;
for (int i = N - B; i < N; i++) {
int digit = S[i] - 1;
val |= (digit << (2 * (i - (N - B))));
}
return {0, val};
}
}