#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;
typedef tuple<int,int,int> tii;
typedef vector<vector<ll>> mat;
const ll mod=1e9+7;
const int N=2e5+5;
int n,ans,dp1[N],dp2[N],dp3[N];
vector<pii> g[N];
void dfs(int x,int p){
vector<int> v;
for(auto &it : g[x]) if(it.fi!=p){
int u=it.fi,c=it.se;
dfs(u,x);
v.emplace_back(max(dp1[u],dp2[u])+c-max({dp1[u],dp2[u],dp3[u]+c}));
dp1[x]+=max({dp1[u],dp2[u],dp3[u]+c});
}
sort(v.begin(),v.end()); reverse(v.begin(),v.end());
if(v.size()>=2){
dp2[x]=dp1[x]+v[0]+v[1];
} else dp2[x]=-1e9;
if(v.size()>=1&&p){
dp3[x]=dp1[x]+v[0];
} else dp3[x]=-1e9;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
for(int u,v,c,i=1;i<n;i++){
cin>>u>>v>>c;
g[u].emplace_back(v,c);
g[v].emplace_back(u,c);
}
dfs(1,0);
cout<<max({dp1[1],dp2[1],dp3[1]});
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |