Submission #670239

#TimeUsernameProblemLanguageResultExecution timeMemory
670239MohammadAghilBeads and wires (APIO14_beads)C++17
100 / 100
184 ms48420 KiB
#include <bits/stdc++.h> // #pragma GCC optimize ("Ofast,unroll-loops") // #pragma GCC target ("avx2") using namespace std; typedef long long ll; typedef pair<ll, ll> pp; #define per(i,r,l) for(int i = (r); i >= (l); i--) #define rep(i,l,r) for(int i = (l); i < (r); i++) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define ss second #define ff first void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);} void IOS(){ cin.tie(0) -> sync_with_stdio(0); #ifndef ONLINE_JUDGE #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl #else #define er(args ...) 0 #endif } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod = 1e9 + 7, maxn = 1e6 + 5, maxk = 2e2 + 5, p = 9973, lg = 22, inf = ll(1e18) + 5; ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll,md);return k*k%md*(b&1ll?a:1)%md;} ll dp[maxn][4]; vector<pp> adj[maxn]; ll mx[maxn]; void dfs(int r, int p, int vl){ ll sum = 0; ll mxx = -inf; for(auto[c, w]: adj[r]) if(c - p){ dfs(c, r, w); sum += dp[c][2]; mxx = max(mxx, dp[c][3] - dp[c][2] + w); } dp[r][3] = sum; dp[r][2] = max(sum, sum + mxx + vl); ll tmp = -inf; for(auto[c, w]: adj[r]) if(c - p){ mx[c] = tmp; tmp = max(tmp, dp[c][3] - dp[c][2] + w); } reverse(all(adj[r])); tmp = -inf; for(auto[c, w]: adj[r]) if(c - p){ mx[c] = max(mx[c], tmp); dp[r][1] = max({dp[r][1], sum - dp[c][2] + dp[c][0], sum - dp[c][2] + mx[c] + dp[c][1] + w}); dp[r][0] = max(dp[r][0], vl + dp[c][1] + sum - dp[c][2] + w); tmp = max(tmp, dp[c][3] - dp[c][2] + w); } dp[r][0] = max(dp[r][0], dp[r][1]); // er(r, dp[r][0], dp[r][1], dp[r][2], dp[r][3]); } int main(){ IOS(); int n; cin >> n; rep(i,1,n){ int u, v, w; cin >> u >> v >> w; u--, v--; adj[u].pb({v, w}); adj[v].pb({u, w}); } dfs(0, -1, 0); cout << dp[0][1] << '\n'; return 0-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...