Submission #543806

#TimeUsernameProblemLanguageResultExecution timeMemory
543806inventiontime구슬과 끈 (APIO14_beads)C++17
100 / 100
205 ms32796 KiB
// [node][0] is current node is *ordinary*
// [node][1] is current node is *connector* 
// connector: node between two blue wires
// condition for connector: 
    // atleast one child node is ordinary

#include <bits/stdc++.h>
using namespace std;

#define int ll
//#define endl '\n'

#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl

typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef priority_queue<int> pq;

template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }

const int inf = 1e17;
const int maxn = 2e5 + 5;

int n;
vii adj[maxn];
ii sub[maxn];
ii min_diff[maxn];
ii min_diff2[maxn];

void calc_dfs(int u, int p) {

    for(auto [v, w] : adj[u]) if(v != p)
        calc_dfs(v, u);

    sub[u][0] = 0;
    sub[u][1] = 0;
    min_diff[u] = {inf, -1};
    for(auto [v, w] : adj[u]) if(v != p) {
        sub[u][0] += max(sub[v][0], sub[v][1] + w);
        sub[u][1] += max(sub[v][0], sub[v][1] + w);
        ii min_d = {max(sub[v][0], sub[v][1] + w) - (sub[v][0] + w), v};
        if(min_d <= min_diff[u]) {
            min_diff2[u] = min_diff[u];
            min_diff[u] = min_d;
        } else if(min_d < min_diff2[u]) {
            min_diff2[u] = min_d;
        }
    }
    sub[u][1] -= min_diff[u][0];

}

void move(int u, int v, int w) {

    ii sub_u = sub[u];
    ii sub_v = sub[v];

    sub[u][0] -= max(sub_v[0], sub_v[1] + w);
    sub[u][1] -= max(sub_v[0], sub_v[1] + w);
    if(min_diff[u][1] == v) {
        sub[u][1] += min_diff[u][0];
        sub[u][1] -= min_diff2[u][0];
    }

    sub[v][0] += max(sub[u][0], sub[u][1] + w);
    sub[v][1] += max(sub[u][0], sub[u][1] + w);
    if(min_diff[v][1] == u) sub[v][1] += min_diff2[v][0];
    else sub[v][1] += min_diff[v][0];

    ii min_d = {max(sub[u][0], sub[u][1] + w) - (sub[u][0] + w), u};
    if(min_d <= min_diff[v]) {
        min_diff2[v] = min_diff[v];
        min_diff[v] = min_d;
    } else if(min_d < min_diff2[v]) {
        min_diff2[v] = min_d;
    }
    sub[v][1] -= min_diff[v][0];

}

int dfs(int u, int p) {

    int res = sub[u][0];
    for(auto [v, w] : adj[u]) if(v != p) {
        move(u, v, w);
        ckmax(res, dfs(v, u));
        move(v, u, w);
    }

    return res;

}

void solve() {
    
    cin >> n;
    loop(i, n-1) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }

    calc_dfs(1, 1);
    cout << dfs(1, 1) << endl;

}

signed main() {

    fast_io;

    int t = 1; //cin >> t;
    while(t--)
        solve();

    //cout << flush;

    return 0;

}

Compilation message (stderr)

beads.cpp: In function 'void move(ll, ll, ll)':
beads.cpp:68:8: warning: variable 'sub_u' set but not used [-Wunused-but-set-variable]
   68 |     ii sub_u = sub[u];
      |        ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...