QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#396973 | #6742. Leaves | coldwind902 | WA | 1ms | 3828kb | C++20 | 2.6kb | 2024-04-23 15:05:57 | 2024-04-23 15:05:58 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using i64 = long long;
using PII = pair<int, int>;
const int g = 3, mod = 1e9 + 7, INF = 0x3f3f3f3f;
const int N = 510, M = N << 1;
int n, m;
int val[M], sz[M];
PII son[M];
struct bi{
vector<int> v;
friend bi operator+ (bi a, bi b){
for(int i : b.v) a.v.emplace_back(i);
return a;
}
friend bool operator< (bi a, bi b){
int sz1 = a.v.size(), sz2 = b.v.size();
int mnsz = min(sz1, sz2);
for(int i = 0; i < mnsz; ++ i){
if(a.v[i] < b.v[i]) return true;
else if(a.v[i] > b.v[i]) return false;
}
if(sz1 < sz2)return true;
return false;
}
}dp[M][N];
void dfs(int u){
sz[u] = 1;
int lroot = son[u].first, rroot = son[u].second;
if(lroot || rroot){
//递归计算左右儿子
dfs(lroot);
dfs(rroot);
sz[u] += sz[lroot];
sz[u] += sz[rroot];
//计算当前点, 不交换
for(int i = 0; i <= sz[lroot] / 2; ++ i){//左儿子用i次
for(int j = 0; j <= sz[rroot] / 2; ++ j){//右儿子j次
bi res = dp[lroot][i] + dp[rroot][j];
if(dp[u][i + j].v.empty()) dp[u][i + j] = res;
else if(res < dp[u][i + j]) dp[u][i + j] = res;
}
}
//交换
for(int i = 0; i <= sz[lroot] / 2; ++ i){//左儿子用i次
for(int j = 0; j <= sz[rroot] / 2; ++ j){//右儿子j次
bi res = dp[rroot][j] + dp[lroot][i];
if(dp[u][i + j + 1].v.empty()) dp[u][i + j + 1] = res;
else if(res < dp[u][i + j + 1]) dp[u][i + j + 1] = res;
}
}
}
else{//叶节点,无论怎么变化都一样的
for(int i = 0; i <= m; ++ i)
dp[u][i].v.emplace_back(val[u]);
}
}
void solve() {
cin >> n >> m;
int op, l, r;
for(int i = 1; i <= n; ++ i){
cin >> op;
if(op == 1){
cin >> l >> r;
son[i] = {l, r};
}
else {
cin >> l;//叶节点
val[i] = l;
}
}
if(n == 1){
cout << val[1] << endl;
return;
}
dfs(1);
bi ans = dp[1][m];
for(int j : ans.v)
cout << j << ' ';
cout << endl;
}
signed main() {
#ifdef ONLINE_JUDGE
#else
freopen("902.in", "r", stdin);
freopen("902.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
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: 1ms
memory: 3664kb
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: 1ms
memory: 3660kb
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: 1ms
memory: 3828kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
3 1 1 2 3 2 1 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3596kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 1 2 2 2 3 2 4
output:
2 1 4 3
result:
wrong answer 1st numbers differ - expected: '1', found: '2'