이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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][2];
vector<pp> adj[maxn];
void dfs(int r, int p, int vl){
ll sum = 0;
pp mx = {-inf, -inf};
for(auto[c, w]: adj[r]) if(c - p){
dfs(c, r, w);
sum += dp[c][1];
ll v = w - dp[c][1] + dp[c][0];
if(mx.ff < v) mx = {v, mx.ff};
else if(mx.ss < v) mx.ss = v;
}
dp[r][0] = max(sum, sum + mx.ff + mx.ss);
dp[r][1] = max(dp[r][0], sum + mx.ff + vl);
// er(r+1, dp[r][0], dp[r][1]);
}
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][0] << '\n';
return 0-0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |