Submission #670128

#TimeUsernameProblemLanguageResultExecution timeMemory
670128radalBeads and wires (APIO14_beads)C++17
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,avx2") #pragma GCC optimize("unroll-loops,O2") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define all(x) (x).begin() , (x).end() #define pb push_back #define endl '\n' #define debug(x) cerr << #x << " : " << x << endl; using namespace std; typedef long long ll; typedef pair<int,int> pll; constexpr int N = 2e5+10,mod = 1e9+7,maxm = 210; constexpr ll inf = 1e9+10; inline int mkay(int a,int b){ if (a+b >= mod) return a+b-mod; // if (a+b < 0) return a+b+mod; return a+b; } inline int poww(int a,int k){ if (k < 0) return 0; int z = 1; while (k){ if (k&1) z = 1ll*z*a%mod; a = 1ll*a*a%mod; k >>= 1; } return z; } vector<pll> adj[N]; int dp[N][2]; int h[N]; void pre(int v,int p){ for (auto [u,w] : adj[v]){ if (u != p){ h[u] = h[v]+w; pre(u,v); } } } void dfs(int v,int p){ if ((int) adj[v].size() == 1 && p != 0){ dp[v][1] = -2*inf; dp[v][0] = 0; return; } // vector<int> ve; int s = 0,mx = -2*inf; for (auto [u,w] : adj[v]){ if (u == p) continue; dfs(u,v); int val = max(dp[u][0],dp[u][1]+w); s += val; mx = max(mx,dp[u][0]-val+w); // ve.pb(dp[u][0]-val+w); } // sort(all(ve)); dp[v][0] = s; dp[v][1] = s + mx; } int main(){ ios :: sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,1,n){ int u,v,w; cin >> u >> v >> w; adj[u].pb({v,w}); adj[v].pb({u,w}); } if (n <= 2){ cout << 0 << endl; return 0; } pre(1,0); int vert = 1,mx = 0; rep(i,1,n+1){ if (h[i] > mx){ mx = h[i]; vert = i; } } int ans = 0; dfs(vert,0); ans = max(ans,dp[vert][0]); /* rep(i,1,n+1){ if ((int)adj[i].size() > 1) continue; dfs(i,0); ans = max(ans,dp[i][0]); }*/ cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...