제출 #670786

#제출 시각아이디문제언어결과실행 시간메모리
670786Arnch구슬과 끈 (APIO14_beads)C++17
0 / 100
2 ms2644 KiB
#include<bits/stdc++.h> using namespace std; #define Sz(x) (x).size() const int N = 1e5 + 10; long long dp[N][2]; vector<pair<int, int> > adj[N]; void DFS(int x, int p = 0, int cnt = 0) { for(auto [j, t] : adj[x]) { if(j == p) continue; DFS(j, x, t); } if(Sz(adj[x]) == 1 && x != 1) { return; } long long sum = 0; for(auto [j, t] : adj[x]) { if(j == p) continue; sum += dp[j][1]; } dp[x][0] = sum; vector<int> vc; for(auto [j, t] : adj[x]) { if(j == p) continue; vc.push_back(dp[j][0] + t - dp[j][1]); } sort(vc.begin(), vc.end(), greater<int>()); if(vc.size() > 1) { dp[x][0] = max(dp[x][0], sum + vc[0] + vc[1]); } dp[x][1] = dp[x][0]; if(vc.size() > 0) { dp[x][1] = max(dp[x][1], sum + vc[0] + cnt); } } int main() { int n; cin >>n; for(int i = 0; i < n - 1; i++) { int u, v, c; cin >>u >>v >>c; adj[u].push_back({v, c}), adj[v].push_back({u, c}); } DFS(1); cout<<dp[1][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...