QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#209015#6430. Monster HunterHT008RE 0ms0kbC++141.2kb2023-10-10 02:31:362023-10-10 02:31:36

Judging History

你现在查看的是最新测评结果

  • [2023-10-10 02:31:36]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-10-10 02:31:36]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define int long long
using namespace std;
const int maxm=2e3+100;
vector<int> edge[maxm];
int dp[maxm][maxm][2],val[maxm],siz[maxm];
int n;
void dfs(int now)
{
	siz[now]=1;
	dp[now][0][0]=0;
	dp[now][1][1]=val[now];
	for(int i=0;i<edge[now].size();i++)
	{
		int v=edge[now][i];
		dfs(v);
		for(int j=siz[now];j>=0;j--)
			for(int k=siz[v];k>=0;k--)
			{
				dp[now][j+k][0]=min(dp[now][j+k][0],dp[now][j][0]+min(dp[v][k][0],dp[v][k][1]));
				dp[now][j+k][1]=min(dp[now][j+k][1],dp[now][j][1]+min(dp[v][k][0],dp[v][k][1]+val[v]));
			}
		siz[now]+=siz[v];
	}
}
void init()
{
	for(int i=0;i<=n;i++)
		for(int j=0;j<=n;j++)
			dp[i][j][0]=dp[i][j][1]=1e18;
	for(int i=1;i<=n;i++)
		edge[i].clear();
}
void work()
{
	
	scanf("%lld",&n);
	init();
	for(int i=2;i<=n;i++)
	{
		int x;
		scanf("%lld",&x);
		edge[x].push_back(i);
	}
	for(int i=1;i<=n;i++)
		scanf("%lld",&val[i]);
	dfs(1);
	for(int i=n;i>=0;i--)
		printf("%lld ",min(dp[1][i][1],dp[1][i][0]));
	puts("");
}
signed main()
{
	int t;
	scanf("%d",&t);
	while(t--) work();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

3
5
1 2 3 4
1 2 3 4 5
9
1 2 3 4 3 4 6 6
8 4 9 4 4 5 2 4 1
12
1 2 2 4 5 3 4 3 8 10 11
9 1 3 5 10 10 7 3 7 9 4 9

output:


result: