제출 #1357696

#제출 시각아이디문제언어결과실행 시간메모리
1357696xorshift구슬과 끈 (APIO14_beads)C++20
100 / 100
96 ms26200 KiB
#include <bits/stdc++.h>
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
using pint = pair<int, int>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using pill = pair<int, ll>;
using vpill = vector<pill>;
using plli = pair<ll, int>;
using vplli = vector<plli>;
#define e1 first
#define e2 second
#define all(x) begin(x), end(x)
const int INF = 1e9;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n; cin >> n;
    vint a(n-1), b(n-1), c(n-1);
    for (int i = 0; i < n-1; i++) cin >> a[i] >> b[i] >> c[i];
    vector<vpint> adj(n);
    for (int i = 0; i < n-1; i++)
        adj[a[i]-1].push_back({b[i]-1, c[i]}), adj[b[i]-1].push_back({a[i]-1, c[i]});

    vint p(n, -1); p[0] = 0;
    vint order; order.reserve(n);
    queue<int> bfs_qu;
    bfs_qu.push(0);
    while (!bfs_qu.empty()) {
        int v = bfs_qu.front(); bfs_qu.pop();
        order.push_back(v);
        for (auto& [ad, w] : adj[v]) if (p[ad] == -1) {
            p[ad] = v; bfs_qu.push(ad);
        }
    }
    reverse(all(order));
    vint size(n);
    for (auto& i : order) {
        int si = 1;
        for (auto& [ad, w]: adj[i]) if (ad != p[i]) si += size[ad];
        size[i] = si;
    }
    vint dp_ts_po(n), dp_ts_no(n);
    vint dp_po(n), dp_no(n);
    for (auto& i : order) {
        vint no_val; int no_res;
        if (size[i] == 1) dp_no[i] = 0;
        else {
            for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                int c_val = dp_no[ad];
                if (size[ad] > 1) c_val = max(c_val, w + dp_po[ad]);
                no_val.push_back(c_val);
            }
            no_res = accumulate(all(no_val), 0);
            dp_no[i] = no_res;
        }
        if (size[i] == 1) dp_po[i] = INF;
        else {
            vint po_val;
            for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                int c_val = w + dp_no[ad];
                po_val.push_back(c_val);
            }
            int max_diff = -INF;
            for (int j = 0; j < (int)po_val.size(); j++)
                max_diff = max(max_diff, po_val[j] - no_val[j]);
            int po_res = no_res + max_diff;
            dp_po[i] = po_res;
        }
        if (size[i] == 1) dp_ts_no[i] = 0;
        else {
            vint ts_no_val_1;
            for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                int c_val = 0;
                if (size[ad] > 1) c_val = max(c_val, w + dp_ts_po[ad]);
                ts_no_val_1.push_back(c_val);
            }
            int max_diff_1 = -INF;
            for (int j = 0; j < (int)ts_no_val_1.size(); j++)
                max_diff_1 = max(max_diff_1, ts_no_val_1[j] - no_val[j]);
            int ts_no_res_1 = no_res + max_diff_1;
            
            vint ts_no_val_2;
            for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                int c_val = dp_ts_no[ad];
                ts_no_val_2.push_back(c_val);
            }
            int max_diff_2 = -INF;
            for (int j = 0; j < (int)ts_no_val_2.size(); j++)
                max_diff_2 = max(max_diff_2, ts_no_val_2[j] - no_val[j]);
            int ts_no_res_2 = no_res + max_diff_2;

            int ts_no_res_3 = 0;
            if (no_val.size() > 1) {
                vint diff_vals;
                int j = 0;
                for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                    int c_val = w + dp_no[ad];
                    diff_vals.push_back(c_val - no_val[j]);
                    j++;
                }
                vint diff_vals_sorted(diff_vals); sort(all(diff_vals_sorted), greater<>());
                vint ts_no_res_val_3;
                j = 0;
                for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                    int c_val = w + dp_ts_no[ad];
                    int c_diff = c_val - no_val[j];
                    int idx = 0; if (diff_vals_sorted[idx] == diff_vals[j]) idx++;
                    int c_res = no_res + diff_vals_sorted[idx] + c_diff;
                    ts_no_res_val_3.push_back(c_res);
                    j++;
                }
                ts_no_res_3 = *max_element(all(ts_no_res_val_3));
            }
            dp_ts_no[i] = max({ts_no_res_1, ts_no_res_2, ts_no_res_3});
        }
        if (size[i] == 1) dp_ts_po[i] = INF;
        else {
            vint ts_po_val;
            for (auto& [ad, w]: adj[i]) if (ad != p[i]) {
                int c_val = w + dp_ts_no[ad];
                ts_po_val.push_back(c_val);
            }
            int max_diff = -INF;
            for (int j = 0; j < (int)ts_po_val.size(); j++)
                max_diff = max(max_diff, ts_po_val[j] - no_val[j]);
            int ts_po_res = no_res + max_diff;
            dp_ts_po[i] = ts_po_res;
        }
    }
    cout << dp_ts_no[0] << endl;
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…