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 = 210;
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 ;
for(int i=0;i<g[node].size();i++){
int newnode = g[node][i].first;
int newlen = g[node][i].second;
if(newnode != prnt){
int cur = solve(newnode,node,newlen,0);
sum+= cur;
}
}
dp[node][b] = sum;
if(b == 0){
for(int i=0;i<g[node].size();i++){
int newnode = g[node][i].first;
int newlen = g[node][i].second;
if(newnode == prnt) continue;
int cur = solve(newnode,node,newlen,0);
int cur2 = solve(newnode,node,newlen,1);
dp[node][b] = max(dp[node][b],sum - cur + cur2 + newlen + len);
}
for(int i=0;i<g[node].size();i++){
for(int j=i+1;j<g[node].size();j++){
int node1 = g[node][i].first;
int node2 = g[node][j].first;
int len1 = g[node][i].second;
int len2 = g[node][j].second;
if(node1 == prnt || node2 == prnt) continue;
int cur1 = solve(node1,node,len1,0);
int cur2 = solve(node1,node,len1,1);
int cur3 = solve(node2,node,len2,0);
int cur4 = solve(node2,node,len2,1);
dp[node][b] = max(dp[node][b],cur4 - cur3 + len1 + cur2 - cur1 + len2 + sum);
}
}
}
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));
}
int ans = 0 ;
for(int i=1;i<=n;i++){
memset(dp,-1,sizeof(dp));
int cur = solve(i,-1,0,1);
ans = max(ans,solve(i,-1,0,1));
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int solve(int, int, int, bool)':
beads.cpp:13:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:23:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:31:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:32:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=i+1;j<g[node].size();j++){
~^~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:61:7: warning: unused variable 'cur' [-Wunused-variable]
int cur = solve(i,-1,0,1);
^~~
beads.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |