/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 344.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 2ms 468.0 KiB
#5 Accepted 3ms 532.0 KiB
#6 Accepted 5ms 496.0 KiB
#7 Accepted 4ms 320.0 KiB
#8 Accepted 4ms 448.0 KiB
#9 Accepted 4ms 568.0 KiB
#10 Accepted 4ms 532.0 KiB
#11 Accepted 4ms 532.0 KiB
#12 Time Exceeded ≥3096ms ≥6.77 MiB
#13 Time Exceeded ≥3096ms ≥6.562 MiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<ll> v(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    vector<vector<int>> g(n + 1);
    for(int i = 1; i < n; i++) {
        int u1, v1; cin >> u1 >> v1;
        g[u1].push_back(v1);
        g[v1].push_back(u1); 
    }

    // int leaf = 1;
    // for(int i = 1; i <= n; i++) {
    //     if(g[i].size() == 1) {
    //         leaf = 1;
    //         break;
    //     }
    // }
    
    vector<bool> vis(n + 1, 0);
    auto dfs = [&](auto&& self, int u, ll sum, int c) -> ll {
        if(c == k) {
            return sum;
        }
        vis[u] = 1;
        ll mx = 0;
        for(auto &vv: g[u]) {
            if(vis[vv]) continue;
            mx = max(mx, self(self, vv, sum + v[vv], c + 1));
        }
        vis[u] = 0;
        return mx;
    };

    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dfs(dfs, i, v[i], 1));
    }
    cout << ans << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1161 Max path sum (Hard Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 15:43:30
Judged At
2025-02-17 15:43:30
Judged By
Score
22
Total Time
≥3096ms
Peak Memory
≥6.77 MiB