QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603664#6430. Monster HunterlonelywolfWA 2ms3696kbC++201.6kb2024-10-01 18:14:062024-10-01 18:14:07

Judging History

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

  • [2024-10-01 18:14:07]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3696kb
  • [2024-10-01 18:14:06]
  • 提交

answer

#include <bits/stdc++.h>  
using namespace std;  

void solve() {
	int n;
	cin >> n;

	vector<vector<int>> adj(n + 1);
	for (int i = 2; i <= n; i++) {
		int p;
		cin >> p;
		adj[p].push_back(i);
	}

	vector<int> v(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> v[i];
	}

	vector<int> sz(n + 1);
	vector<vector<int>> ans;
	vector dp(n + 1, vector(n + 1, vector<int>(2, 1e9)));

	function<void(int)> dfs = [&](int x) {
		sz[x] = 1;
		for (auto y : adj[x]) {
			dfs(y);			
			sz[x] += sz[y];
		}

		dp[x][0][0] = v[x];
		dp[x][1][1] = 0;
		
		int p = 1;
		for (auto y : adj[x]) {
			vector ndp(sz[x] + 1, vector<int>(2, 1e9));
			for (int i = p; i >= 0; i--) {
				for (int j = 0; j <= sz[y] && i + j <= sz[x]; j++) {
					ndp[i + j][0] = min(ndp[i + j][0], dp[x][i][0] + min(dp[y][j][1], dp[y][j][0] + v[y]));
					ndp[i + j][1] = min(ndp[i + j][1], dp[x][i][1] + min(dp[y][j][1], dp[y][j][0]));
				}
			}
			dp[x] = ndp;
			// for (int i = p; i >= 0; i--) {
			// 	for (int j = 0; j <= sz[y] && i + j <= sz[x]; j++) {
			// 		dp[x][i + j][0] = min(dp[x][i + j][0], dp[x][i][0] + min(dp[y][j][1], dp[y][j][0] + v[y]));
			// 		dp[x][i + j][1] = min(dp[x][i + j][1], dp[x][i][1] + min(dp[y][j][1], dp[y][j][0]));
			// 	}
			// }
			p += sz[x];
		}
	};

	dfs(1);

	for (int i = 0; i <= n; i++) {
		cout << min(dp[1][i][0], dp[1][i][1]) << " \n"[i == n];
	}
}

signed main() {  
    ios::sync_with_stdio(false);
    cin.tie(nullptr);  

    int t;
    cin >> t;
    while (t--) {
    	solve();
    }

    return 0;
}  

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3572kb

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:

29 16 9 4 1 0
74 47 35 25 15 11 7 3 1 0
145 115 93 73 55 42 32 22 14 8 4 1 0

result:

ok 29 tokens

Test #2:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

179
20
1 1 1 4 5 5 7 7 9 9 11 12 13 14 5 16 17 16 19
3 9 3 2 7 7 2 8 5 7 5 4 7 4 2 4 9 2 7 9
19
1 1 3 4 3 6 7 6 6 10 10 12 13 13 12 16 16 18
8 8 3 6 10 1 1 1 2 2 3 3 3 10 5 5 7 10 5
2
1
10 4
2
1
2 7
14
1 1 3 4 4 6 4 8 9 10 11 8 13
4 4 6 6 10 8 9 5 7 1 4 7 9 8
6
1 2 3 3 5
2 7 5 6 1 6
11
1 2 3 3 5 6 6...

output:

209 182 159 137 117 99 81 65 56 47 39 32 25 19 15 11 8 6 4 2 0
178 151 129 108 89 74 64 54 44 36 29 23 18 13 10 7 5 2 1 0
18 4 0
16 2 0
172 137 111 93 78 63 49 39 31 23 16 10 5 1 0
52 33 21 9 3 1 0
109 72 53 39 29 22 16 10 5 2 1 0
105 69 47 35 25 17 12 7 3 0
156 133 113 97 82 68 56 44 33 26 19 14 10...

result:

ok 2178 tokens

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 3696kb

input:

177
10
1 2 3 3 3 6 7 8 3
750920741 600457069 885939487 614833472 917972842 716271451 234536309 280049219 394290544 802674020
5
1 2 2 4
381361244 652246733 111336853 652733347 864548837
7
1 2 2 4 4 4
374076965 100213690 316923584 132783452 321143617 617096325 590521323
14
1 1 3 4 4 6 6 6 4 10 11 10 1...

output:

1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 628826853 234536309 0
1000000000 1000000000 1000000000 492698097 111336853 0
1000000000 1000000000 1000000000 1000000000 823784001 365780594 100213690 0
1000000000 1000000000 1000000000 1000000000 1000000000 1000...

result:

wrong answer 1st words differ - expected: '11644969567', found: '1000000000'