- 原理跟大多数题解中类似,但代码中没有进行排序,而是简单的记录了节点的几个数据
- 开始觉得可能是父节点的序号不一定比子节点小导致的,于是添加了注释部分验证想法,主要是想记录每个节点的高度,然后从最高层叶子往根计算答案
当然后面的几个测试点会超时,结果还是WA
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int notes[N][7];
int height_tree = 0;
int main(){
int n;
cin >> n;
memset(notes, 0, sizeof(notes));
for(int i = 1; i < n; i++){
cin >> notes[i][0];
int father = notes[i][0] - 1;
notes[i][5] = notes[father][5] + 1;
}
for(int i = 0; i < n; i++){
cin >> notes[i][1];
}
for(int i = 0; i < n; i++){
notes[i][4] += notes[i][6] * 9999;
}
for(int i = n - 1; i >= 0; i--){
if(notes[i][4] > notes[i][1])notes[i][2] += notes[i][4];
else notes[i][2] += notes[i][1];
if(notes[i][3] < notes[i][2])notes[i][3] = notes[i][2];
else notes[i][2] = notes[i][3];
int father = notes[i][0] - 1, d = notes[i][3] - notes[i][1];
notes[father][2] += notes[i][1];
if(notes[father][3] < notes[i][3])notes[father][3] = notes[i][3];
if(notes[father][4] > d)notes[father][4] = d;
}
for(int i = 0; i < n; i++){
cout << notes[i][2] << " ";
}
return 0;
}