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>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
vector<pll> v[N];
ll dp[N][2],n,x,y,c;
void DFS(ll nd,ll pa,ll pac){
vector<ll> vec;
ll sum=0;
for(auto i:v[nd]){
if(i.X!=pa){
DFS(i.X,nd,i.Y);
sum+=dp[i.X][0];
vec.pb(dp[i.X][1]-dp[i.X][0]+i.Y);
}
}
sort(vec.rbegin(),vec.rend());
dp[nd][1]=sum;
dp[nd][0]=max(dp[nd][1],(SZ(vec)>0?sum+vec[0]+pac:0LL));
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
REP1(i,n-1){
cin>>x>>y>>c;
v[x].pb(mp(y,c));
v[y].pb(mp(x,c));
}
ll ans=0;
REP1(i,n){
memset(dp,0,sizeof(dp));
DFS(i,0,0);
ans=max(ans,dp[i][1]);
}
cout<<ans<<"\n";
return 0;
}
/*
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
# | 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... |