#include #define mrep(i, s, n) for (int i = (s); i < (int)(n); i++) #define rep(i, n) mrep(i, 0, n) using namespace std; using ll = long long; const int INF = int(1e+9)+1; const ll INFL = ll(1e+18)+1; ll N; vector L, A; vector>> graph, graph_rev; vector memo_L, memo_A; unordered_map memo0, memo1; int main() { cin >> N; L.resize(N+2, 0), A.resize(N+2, 1); rep(i, N-1) {cin >> L[i+2] >> A[i+2];} graph = vector>>(N+2); graph_rev = vector>>(N+2); rep(i, N) { int u = i+2, v = A[i+2]; graph[v].push_back(make_pair(u, L[i+2])); graph_rev[u].push_back(make_pair(v, L[i+2])); } vector level_sorted(N); copy(L.begin()+2, L.end(), level_sorted.begin()); sort(level_sorted.begin(), level_sorted.end()); memo_L.resize(N, 0); memo_A.resize(N, 0); for (int i = 0; i q; q.push(1); while(!q.empty()) { ans++; int s = q.front(); q.pop(); // 隣接している頂点を追加 for (auto un: graph[s]) { auto [u, l] = un; if (lvl < l) {continue;} // レベル不足 q.push(u); } } memo_L[i] = lvl; memo_A[i] = ans; } memo_L.push_back(INFL); int Q; cin >> Q; rep(_, Q) { int op, val; cin >> op >> val; if (op == 1) { if (memo0.count(val)) { cout << memo0[val] << endl; continue; } auto it = lower_bound(memo_L.begin(), memo_L.end(), ll(val)); if (*it != val) {it--;} int idx = it-memo_L.begin(); memo0[val] = memo_A[idx] - 1; cout << memo_A[idx]-1 << endl; } else { if (memo1.count(val)) { cout << memo1[val] << endl; continue; } bool ac = false; vector done(N+2, false); ll ans = -1; queue q; q.push(val); while(!q.empty()) { int s = q.front(); q.pop(); if (done[s]) {continue;} done[s] = true; if (s == 1) {ac = true; break;} for(auto ul: graph_rev[s]) { auto [u, l] = ul; if (done[u]) {continue;} ans = max(ans, l); if(l) {q.push(u);} } } if (!ac) {ans = -1;} memo1[val] = ans; cout << ans << endl; } } }