제출 #402949

#제출 시각아이디문제언어결과실행 시간메모리
402949Drew_구슬과 끈 (APIO14_beads)C++17
0 / 100
5 ms5016 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define pb push_back #define ii pair<int, int> #define f1 first #define s2 second const int MAX = 2e5 + 69; int n; vector<ii> adj[MAX]; //DP[X][0]: vertex X, not using the edge from parent //DP[X][1]: vertex X, using the edge from parent int dp[MAX][2]; int cost[MAX]; void dfs(int node, int par, int parw) { int ctr = 0; //point for using 0 edge (X as centre) vector<int> v; //bonus point for using the edge (must free the child's parent edge) for (auto [to, w] : adj[node]) { if (to != par) { dfs(to, node, w); ctr += max(dp[to][0], dp[to][1]); if (dp[to][0] >= dp[to][1]) v.pb(w); else v.pb(dp[to][0] - dp[to][1] + w); } } if (v.empty()) // a leaf return; sort(v.begin(), v.end(), greater<int>()); dp[node][0] = ctr; dp[node][1] = ctr + v[0] + parw; if (v.size() >= 2) dp[node][0] = max(dp[node][0], ctr + v[0] + v[1]); } int main() { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n-1; ++i) { int u, v, w; cin >> u >> v >> w; adj[u].pb({v, w}); adj[v].pb({u, w}); } dfs(1, -1, -1); cout << dp[1][0] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...