性感代码在线求调,85分,#8/9/10wa
查看原帖
性感代码在线求调,85分,#8/9/10wa
823835
yll0216楼主2024/3/20 00:19
  1. 原理跟大多数题解中类似,但代码中没有进行排序,而是简单的记录了节点的几个数据
  2. 开始觉得可能是父节点的序号不一定比子节点小导致的,于是添加了注释部分验证想法,主要是想记录每个节点的高度,然后从最高层叶子往根计算答案当然后面的几个测试点会超时,结果还是WA
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int notes[N][7];//[0]:父节点序号, [1]:w_i,[2]:ans,该节点答案,[3]:max,节点自身及子树中答案的最大值,[4]:min_d, 孩子节点中[3]-[1]的最小值 [5]:层数, [6]: 子结点个数
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;
		// notes[father][6]++;
		// if(height_tree < notes[i][5])height_tree = notes[i][5];
	}
	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 k = height_tree; k >= 0; k--){
	for(int i = n - 1; i >= 0; i--){
		// if(notes[i][5] != k)continue;
		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;
}
2024/3/20 00:19
加载中...