# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
33970 | mohammad_kilani | Beads and wires (APIO14_beads) | C++14 | 11 ms | 9728 KiB |
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>
using namespace std;
#define mod 1000000007
#define oo 2000000000
const int N = 300010;
int n , dp[N][2];
vector< pair<int,int> > g[N];
int solve(int node,int prnt,int len,bool b){
if(dp[node][b] != -1) return dp[node][b];
int sum = 0 ;
dp[node][b] = 0 ;
vector<int> v;
for(int i=0;i<g[node].size();i++){
if(g[node][i].first != prnt){
int cur = solve(g[node][i].first,node,g[node][i].second,0);
int cur2 = solve(g[node][i].first,node,g[node][i].second,1);
sum += cur2;
v.push_back(cur - cur2 + g[node][i].second);
}
}
dp[node][b] = sum;
sort(v.begin(),v.end());
if(v.size() > 1) dp[node][b] = max(dp[node][b],v[v.size()-1] + v[v.size()-2] + sum);
if(b){
if(v.size() > 2)
dp[node][b] = max(dp[node][b],sum + len + v[v.size()-1] + v[v.size()-2] + v[v.size()-3]);
else if(v.size() > 0)
dp[node][b] = max(dp[node][b],sum + len + v[v.size()-1]);
}
return dp[node][b];
}
int main() {
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int u , v, w;
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(make_pair(v,w));
g[v].push_back(make_pair(u,w));
}
memset(dp,-1,sizeof(dp));
cout<< solve(1,-1,0,0) << endl;
return 0;
}
Compilation message (stderr)
# | 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... |