Submission #525217

#TimeUsernameProblemLanguageResultExecution timeMemory
525217XII구슬과 끈 (APIO14_beads)C++17
100 / 100
183 ms29112 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "APIO14_beads"
void Fi(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp", "r", stdin);
        freopen(PROB".out", "w", stdout);
    }
}

const int N = 2e5 + 1;
const ll INF = 1e15;
int n;
ll dp[2][N];
using pi = pair<int, int>;
vector<pi> adj[N];

int tc[2][N];
ll f[2][N], dif[2][N];
ll ans;

void dfs1(int u, int p){
    dif[0][u] = dif[1][u] = -INF;
    for(auto [w, v]: adj[u]) if(v != p){
        dfs1(v, u);
        f[0][u] += max(f[0][v], f[1][v] + w);
        ll diftmp = (f[0][v] + w) - max(f[0][v], f[1][v] + w);
        if(dif[0][u] < diftmp){
            dif[1][u] = dif[0][u];
            tc[1][u] = tc[0][u];

            dif[0][u] = diftmp;
            tc[0][u] = v;
        } else if(dif[1][u] < diftmp){
            dif[1][u] = diftmp;
            tc[1][u] = v;
        }
    }
    f[1][u] = f[0][u] + dif[0][u];
//    cout << u << " " << f[0][u] << " " << f[1][u] << "\n";
//    cout << dif[0][u] << " " << dif[1][u] << "\n\n";
}

void dfs2(int u, int p, ll wei, ll down[2]){
    ll tot = f[0][u] + max(down[0], down[1] + wei);
    ll diftmp = (down[0] + wei) - max(down[0], down[1] + wei);
    ans = max(ans, tot);
//    cout << u << " " << down[0] << " " << down[1] << " " << tot << " " << diftmp << "\n";
    for(auto [w, v]: adj[u]) if(v != p){
        ll dwn[2];
        dwn[0] = tot - max(f[0][v], f[1][v] + w);
        if(v != tc[0][u]){
            dwn[1] = dwn[0] + max(dif[0][u], diftmp);
        } else{
            dwn[1] = dwn[0] + max(dif[1][u], diftmp);
        }
        dfs2(v, u, w, dwn);
    }
}

int main(){
    IOS;
    Fi();
    cin >> n;
    FOR(i, 1, n){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].eb(w, v);
        adj[v].eb(w, u);
    }
    dfs1(1, -1);
    ll down[2] = {0, -INF};
    dfs2(1, -1, -INF, down);
    cout << ans;
    return 0;
}

Compilation message (stderr)

beads.cpp: In function 'void Fi()':
beads.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(PROB".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(PROB".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...