QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#396936#6742. Leavescoldwind902WA 1ms3684kbC++202.5kb2024-04-23 14:32:182024-04-23 14:32:18

Judging History

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

  • [2024-04-23 14:32:18]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3684kb
  • [2024-04-23 14:32:18]
  • 提交

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];
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 sz = min(sz1, sz2);
        for(int i = 0; i < sz; ++ 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){
    int lroot = son[u].first, rroot = son[u].second;
    if(lroot || rroot){
        //递归计算左右儿子
        dfs(lroot);
        dfs(rroot);

        //计算当前点
        for(int j = 0; j <= m; ++ j){//当前点为根的子树花费j
            for(int k = 0; k <= j; ++ k){//分配给左儿子
                bi res = dp[lroot][k] + dp[rroot][j - k];
                if(dp[u][j].v.empty()) dp[u][j] = res;
                else if(res < dp[u][j]) dp[u][j] = res;
            }
        }

        for(int j = 0; j < m; ++ j){//当前点为根,但是先花费1个步数给左右儿子交换
            for(int k = 0; k <= j; ++ k){
                bi res = dp[rroot][k] + dp[lroot][j - k];
                if(dp[u][j + 1].v.empty()) dp[u][j + 1] = res;
                else if(res < dp[u][j + 1]) dp[u][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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3596kb

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: 3632kb

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: 3564kb

input:

1 0
2 1000000000

output:

1000000000

result:

ok 1 number(s): "1000000000"

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3640kb

input:

3 1
1 2 3
2 1
2 2

output:

1 2 

result:

wrong answer 1st numbers differ - expected: '2', found: '1'