제출 #541699

#제출 시각아이디문제언어결과실행 시간메모리
541699Aldas25구슬과 끈 (APIO14_beads)C++14
0 / 100
4 ms5036 KiB
#include <bits/stdc++.h> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #define f first #define s second #define pb push_back typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef long double ld; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MAXN = 200100; const ll INF = 1e16; int n; vector<pair<int, ll>> adj[MAXN]; ll dp[MAXN][2][2], mx[MAXN]; void dfs (int v, int p, ll pw = -1) { ll mx1 = -INF, mx2 = -INF; for (auto e : adj[v]) { int u = e.f; ll w = e.s; if (u == p) continue; dfs (u, v, w); dp[v][0][0] += mx[u]; dp[v][1][0] += mx[u]; dp[v][1][1] += mx[u]; ll cr = w; if (mx[u] == dp[u][1][1]) cr = cr - mx[u] + max(dp[u][1][0], dp[u][0][0]); if (cr > mx2) swap(cr, mx2); if (mx2 > mx1) swap(mx2, mx1); } if (p != -1 && mx1 != -INF) { dp[v][1][1] += pw + mx1; } if (mx2 != -INF) { dp[v][1][0] += mx2 + mx1; } FOR(a, 0, 1) FOR(b, 0, 1) mx[v] = max(mx[v], dp[v][a][b]); } int main() { FAST_IO; cin >> n; REP(n-1) { int u, v; ll w; cin >> u >> v >> w; adj[u].pb({v, w}); adj[v].pb({u, w}); } dfs(1, -1); cout << mx[1] << "\n"; return 0; } /* 5 1 2 10 1 3 40 1 4 15 1 5 20 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...