This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 200005;
int n;
vii adj[MAXN];
ll dp[MAXN][2],top[MAXN];
void dfs(int u, int p)
{
ll sum=0;
vi diff;
for(ii tmp: adj[u])
{
int v=tmp.F; ll w=tmp.S;
if(v==p) continue;
top[v]=w;
dfs(v,u);
sum+=max(dp[v][0],dp[v][1]);
diff.pb(-max(dp[v][0],dp[v][1])+dp[v][0]+w);
}
sort(diff.begin(), diff.end(), greater<ll>());
if(diff.size()>=1) dp[u][1]=top[u]+sum+diff[0];
dp[u][0]=sum;
if(diff.size()>=2) dp[u][0]=max(dp[u][0], sum+diff[0]+diff[1]);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
if(n<=2)
{
cout<<0<<'\n';
return 0;
}
forn(i,0,n-1)
{
int u,v; ll w; cin>>u>>v>>w; u--; v--;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
dfs(0,-1);
cout<<dp[0][0]<<'\n';
return 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... |