Submission #543347

#TimeUsernameProblemLanguageResultExecution timeMemory
543347inventiontimeBeads and wires (APIO14_beads)C++17
28 / 100
1092 ms5844 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 node below 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];
int sub[maxn][2];
int par[maxn][2];

void dfs(int u, int p) {

    sub[u][0] = 0;
    sub[u][1] = 0;
    bool ord = false;
    int mindiff = inf;
    for(auto [v, w] : adj[u]) if(v != p) {
        dfs(v, u);
        sub[u][0] += max(sub[v][0], sub[v][1] + w);
        sub[u][1] += max(sub[v][0], sub[v][1] + w);
        ckmin(mindiff, max(sub[v][0], sub[v][1] + w) - (sub[v][0] + w));
    }
    sub[u][1] -= mindiff;

}

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});
    }

    int res = 0;
    loop1(i, n) {
        dfs(i, i);
        ckmax(res, sub[i][0]);
    }

    cout << res << 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 dfs(ll, ll)':
beads.cpp:46:10: warning: unused variable 'ord' [-Wunused-variable]
   46 |     bool ord = false;
      |          ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...