Submission #716532

# Submission time Handle Problem Language Result Execution time Memory
716532 2023-03-30T09:16:26 Z Stickfish Paths (RMI21_paths) C++17
56 / 100
600 ms 34636 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

struct node {
    node* l = nullptr;
    node* r = nullptr;
    ll val = 0;
    ll sm = 0;
};

node* copy(node* nd) {
    node* v = new node();
    v->l = nd->l;
    v->r = nd->r;
    v->val = nd->val;
    return v;
}

node* merge(node* l, node* r) {
    if (!l)
        return r;
    if (!r)
        return l;
    
}

const int MAXN = 1e5 + 123;
vector<pair<int, int>> edg[MAXN];
ll cost[MAXN * 3];
pair<int, int> edges[MAXN * 3];
vector<ll> dp[MAXN * 3];

void get_dp(int e, int k) {
    if (dp[e].size())
        return;
    auto [rt, v] = edges[e];
    if (k == 1)
        dp[e] = {0};
    for (auto [u, ne] : edg[v]) {
        if (u == rt)
            continue;
        get_dp(ne, k);
        if (k == 1) {
            dp[e][0] = max(dp[e][0], dp[ne][0]);
            continue;
        }
        vector<ll> ndp(dp[e].size() + dp[ne].size());
        merge(dp[e].rbegin(), dp[e].rend(), dp[ne].rbegin(), dp[ne].rend(), ndp.rbegin());
        ndp.resize(min(int(ndp.size()), k));
        dp[e] = ndp;
    }
    if (dp[e].empty()) {
        dp[e] = {cost[e]};
    } else {
        dp[e][0] += cost[e];
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i + 1 < n; ++i) {
        int u, v, c;
        cin >> u >> v >> c;
        --u, --v;
        edg[u].push_back({v, i * 2});
        edg[v].push_back({u, i * 2 + 1});
        edges[i * 2] = {u, v};
        edges[i * 2 + 1] = {v, u};
        cost[i * 2] = cost[i * 2 + 1] = c;
    }
    for (int e = 2 * n; e < 3 * n; ++e) {
        edges[e] = {n, e - 2 * n};
        get_dp(e, k);
        ll ans = 0;
        for (auto x : dp[e])
            ans += x;
        cout << ans << '\n';
    }
    //for (int e = 0; e < n * 2 - 1; ++e)
        //get_dp(e, k);
    //for (int v = 0; v < n; ++v) {
        //vector<ll> ans;
        //for (auto [u, e] : edg[v]) {
            //vector<ll> ndp(ans.size() + dp[e].size());
            //merge(dp[e].rbegin(), dp[e].rend(), ans.rbegin(), ans.rend(), ndp.rbegin());
            //ndp.resize(min(int(ndp.size()), k));
            //ans = ndp;
        //}
        //ll anssm = 0;
        //for (auto x : ans)
            //anssm += x;
        //cout << anssm << '\n';
    //}
}

Compilation message

Main.cpp: In function 'node* merge(node*, node*)':
Main.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
   28 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 7 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 7 ms 9812 KB Output is correct
8 Correct 7 ms 11476 KB Output is correct
9 Correct 8 ms 10836 KB Output is correct
10 Correct 7 ms 10452 KB Output is correct
11 Correct 43 ms 11316 KB Output is correct
12 Correct 8 ms 10936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 7 ms 9812 KB Output is correct
8 Correct 7 ms 11476 KB Output is correct
9 Correct 8 ms 10836 KB Output is correct
10 Correct 7 ms 10452 KB Output is correct
11 Correct 43 ms 11316 KB Output is correct
12 Correct 8 ms 10936 KB Output is correct
13 Correct 22 ms 29524 KB Output is correct
14 Correct 12 ms 14932 KB Output is correct
15 Correct 8 ms 10836 KB Output is correct
16 Correct 433 ms 25288 KB Output is correct
17 Correct 19 ms 19412 KB Output is correct
18 Correct 13 ms 14292 KB Output is correct
19 Correct 28 ms 34636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 27852 KB Output is correct
2 Correct 190 ms 30772 KB Output is correct
3 Correct 115 ms 27548 KB Output is correct
4 Execution timed out 1069 ms 20440 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 7 ms 9812 KB Output is correct
8 Correct 7 ms 11476 KB Output is correct
9 Correct 8 ms 10836 KB Output is correct
10 Correct 7 ms 10452 KB Output is correct
11 Correct 43 ms 11316 KB Output is correct
12 Correct 8 ms 10936 KB Output is correct
13 Correct 22 ms 29524 KB Output is correct
14 Correct 12 ms 14932 KB Output is correct
15 Correct 8 ms 10836 KB Output is correct
16 Correct 433 ms 25288 KB Output is correct
17 Correct 19 ms 19412 KB Output is correct
18 Correct 13 ms 14292 KB Output is correct
19 Correct 28 ms 34636 KB Output is correct
20 Correct 154 ms 27852 KB Output is correct
21 Correct 190 ms 30772 KB Output is correct
22 Correct 115 ms 27548 KB Output is correct
23 Execution timed out 1069 ms 20440 KB Time limit exceeded
24 Halted 0 ms 0 KB -