提交时间:2020-11-16 13:31:16
运行 ID: 1844
#include <bits/stdc++.h> #define MAXN 1000008 using namespace std; inline int read() { int ret = 0; char ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); while(ch <= '9' && ch >= '0') { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret; } void out(int x) { if (x >= 10) out(x / 10); putchar(x % 10 + '0'); } int n; int f[MAXN], val[MAXN]; struct Edge{int to, nxt;} edge[2 * MAXN]; int head[MAXN], cnt = 0; void add(int x, int y) { edge[++cnt].to = y; edge[cnt].nxt = head[x]; head[x] = cnt; } int st[MAXN], top = 0;//单调栈 int ans[MAXN]; void dfs(int now, int fa) { int chi, chv, cht = top;//change_id, change_val, change_top if(top == 0) { st[++top] = val[now]; chi = 1; chv = 0; } else { top = upper_bound(st + 1, st + top + 1, val[now], greater<int>()) - st; chi = top; chv = st[top]; st[top] = val[now]; } ans[now] = top; // cout << "now: " << now << endl; // for(int i = 1; i <= top; i++) { // cout << st[i] << " "; // } // cout << endl; for(int i = head[now]; i; i = edge[i].nxt) { int ne = edge[i].to; if(ne == fa) continue; dfs(ne, now); } st[chi] = chv; top = cht; } int main() { n = read(); for(int i = 2; i <= n; i++) { f[i] = read(); add(f[i], i); add(i, f[i]); } for(int i = 1; i <= n; i++) { val[i] = read(); } dfs(1, 0); for(int i = 1; i <= n; i++) { out(ans[i]); putchar(' '); } return 0; }