/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.066 MiB
#2 Accepted 26ms 5.188 MiB
#3 Accepted 26ms 5.02 MiB
#4 Accepted 9ms 5.52 MiB
#5 Accepted 36ms 5.066 MiB
#6 Accepted 177ms 6.52 MiB
#7 Accepted 172ms 6.578 MiB
#8 Accepted 174ms 6.559 MiB
#9 Accepted 195ms 9.07 MiB
#10 Accepted 88ms 18.77 MiB
#11 Accepted 95ms 18.719 MiB
#12 Accepted 149ms 18.77 MiB
#13 Accepted 93ms 13.27 MiB
#14 Accepted 368ms 28.223 MiB
#15 Accepted 365ms 25.305 MiB
#16 Accepted 123ms 6.355 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

const int N = 2e5 + 5, INF = 1e15;
vector<pair<int,int> > g[N];
int n, a[N], sub[N], cth[N], ctp[N], ans;

void dfs_siz(int u, int p) {
    sub[u] = 1;
    for (auto &[v,w]: g[u]) if (!cth[v] && v ^ p) dfs_siz(v, u), sub[u] += sub[v];
}

int fc(int u, int p, int lim) {
    for (auto &[v,w]: g[u]) if (!cth[v] && v ^ p && sub[v] > lim) return fc(v, u, lim);
    return u;
}

void calc(int u, int p, int &b1, int &b2, int now, int lvl) {
    now += a[u];
    b1 = max(b1, now);
    if (lvl >= 2) b2 = max(b2, now);
    for (auto &[v,w]: g[u]) if (!cth[v] && v ^ p) calc(v, u, b1, b2, now - 2 * w, lvl + 1);
}

void decompose(int u, int p = 0, int h = 1) {
    dfs_siz(u, 0);
    u = fc(u, 0, sub[u] >> 1);
    cth[u] = h, ctp[u] = p;
    int now = -INF, best = -INF;
    for (auto &[v,w]: g[u]) {
        if (cth[v]) continue;
        int b1 = a[u] - 2 * w, b2 = -INF;
        calc(v, u, b1, b2, b1, 1);
        now = max({now, b2, best + b1 - a[u]});
        best = max(best, b1);
    }
    ans = max(ans, now);
    for (auto &[v,w]: g[u]) if (!cth[v]) decompose(v, u, h + 1);
}

void reset() {
    for (int i = 1; i <= n; ++i) {
        g[i].clear();
        cth[i] = 0;
    }
    ans = -INF;
}

void shelby() {
    cin >> n;
    reset();
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i < n; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    decompose(1);
    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1205 E. Make Cycle in Tree
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-26 15:12:26
Judged At
2025-07-26 15:12:26
Judged By
Score
100
Total Time
368ms
Peak Memory
28.223 MiB