QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186333 | #6742. Leaves | UrgantTeam# | WA | 2ms | 15700kb | C++23 | 3.2kb | 2023-09-23 17:23:51 | 2023-09-23 17:23:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int const maxn = 1005;
int m, a[maxn], sz[maxn], lf[maxn];
vector < int > g[maxn];
vector < int > dp[maxn][maxn / 2];
void dfs(int v) {
sz[v] = 1;
lf[v] = 0;
for (auto u : g[v]) {
dfs(u);
sz[v] += sz[u];
lf[v] += lf[u];
}
if (sz[v] == 1) {
dp[v][0] = {a[v]};
lf[v] = 1;
return;
}
int l = g[v][0], r = g[v][1];
for (int i = 0; i <= (sz[l] - 1) / 2; i++) {
for (int j = 0; j <= (sz[r] - 1) / 2 && i + j <= m; j++) {
int flag = 1;
if (dp[v][i + j].empty()) {
dp[v][i + j] = dp[l][i];
for (auto key : dp[r][j]) dp[v][i + j].push_back(key);
} else {
for (int pos = 0; pos < dp[v][i + j].size(); pos++) {
int cur_value;
if (pos < lf[l]) cur_value = dp[l][i][pos];
else cur_value = dp[r][j][pos - lf[l]];
if (cur_value > dp[v][i + j][pos]) break;
if (cur_value < dp[v][i + j][pos]) {
for (int tmp = pos; tmp < dp[v][i + j].size(); tmp++) {
if (tmp < lf[l]) cur_value = dp[l][i][tmp];
else cur_value = dp[r][j][tmp - lf[l]];
dp[v][i + j][tmp] = cur_value;
}
break;
}
}
}
}
}
swap(l, r);
for (int i = 0; i <= (sz[l] - 1) / 2; i++) {
for (int j = 0; j <= (sz[r] - 1) / 2 && i + j < m; j++) {
int flag = 1;
if (dp[v][i + j + 1].empty()) {
dp[v][i + j + 1] = dp[l][i];
for (auto key : dp[r][j]) dp[v][i + j + 1].push_back(key);
} else {
for (int pos = 0; pos < dp[v][i + j + 1].size(); pos++) {
int cur_value;
if (pos < lf[l]) cur_value = dp[l][i][pos];
else cur_value = dp[r][j][pos - lf[l]];
if (cur_value > dp[v][i + j + 1][pos]) break;
if (cur_value < dp[v][i + j + 1][pos]) {
for (int tmp = pos; tmp < dp[v][i + j + 1].size(); tmp++) {
if (tmp < lf[l]) cur_value = dp[l][i][tmp];
else cur_value = dp[r][j][tmp - lf[l]];
dp[v][i + j + 1][tmp] = cur_value;
}
break;
}
}
}
}
}
}
main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#endif // HOME
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, type, l, r;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> type;
if (type == 1) {
cin >> l >> r;
g[i].push_back(l);
g[i].push_back(r);
} else cin >> a[i];
}
dfs(1);
vector < int > ans = dp[1][0];
for (int i = m % 2; i <= m; i += 2) ans = min(ans, dp[1][i]);
for (auto key : ans) cout << key << " ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15504kb
input:
3 0 1 2 3 2 1 2 2
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15348kb
input:
7 1 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
2 4 3 1
result:
ok 4 number(s): "2 4 3 1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 15348kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
1 3 4 2
result:
ok 4 number(s): "1 3 4 2"
Test #4:
score: 0
Accepted
time: 0ms
memory: 15640kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 15700kb
input:
3 1 1 2 3 2 1 2 2
output:
1 2
result:
wrong answer 1st numbers differ - expected: '2', found: '1'