Submission #1112853

#TimeUsernameProblemLanguageResultExecution timeMemory
1112853KawakiMeidoBeads and wires (APIO14_beads)C++17
0 / 100
6 ms4944 KiB
/*Author: KawakiMeido*/ #include <bits/stdc++.h> #define pb push_back #define endl "\n" #define ll long long #define int long long #define all(x) (x).begin(),(x).end() #define pii pair<int,int> #define fi first #define se second using namespace std; /*Constants*/ const int N = 2e5+10; const int INF = 1e9+7; const long long LLINF = 1e15+3; /*TestCases*/ int test=1; void solve(); void TestCases(bool v){ if (v) cin >> test; while(test--) solve(); } /*Global Variables*/ int n; int dp[N][2]; vector<pii> adj[N]; void DP(int u, int p, int w){ int res = 0; vector<pii> tmp; tmp.push_back({-LLINF,0}); tmp.push_back({-LLINF,0}); for (auto in:adj[u]){ int v,val; tie(v,val) = in; if (v==p) continue; DP(v,u,val); res+=dp[v][1]; tmp.push_back({dp[v][0] - dp[v][1] + val,v}); } sort(all(tmp),greater<pii>()); dp[u][0] = dp[u][1] = max(res,res+tmp[0].fi+tmp[1].fi); dp[u][1] = max(dp[u][1],res+tmp[0].fi+w); } /*Solution*/ void solve(){ cin >> n; for (int i=1; i<n; i++){ int u,v,w; cin >> u >> v >> w; adj[u].push_back({v,w}); adj[v].push_back({u,w}); } DP(1,0,0); cout << dp[1][0] << endl; } /*Driver Code*/ signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); TestCases(0); 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...