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 = 10010;
vector< pair<int,int> > g[N];
int n , dp[N][2];
int calc(int node,bool b,int prnt,int w){
if(dp[node][b] != -1) return dp[node][b];
dp[node][b] = 0 ;
if(b == 1){
for(int i=0;i<g[node].size();i++){
if(g[node][i].first == prnt) continue;
dp[node][b] += max(calc(g[node][i].first,0,node,g[node][i].second),calc(g[node][i].first,1,node,g[node][i].second));
}
}
else{
int sum = 0 ;
vector< pair<int,int> > v;
for(int i=0;i<g[node].size();i++){
if(g[node][i].first == prnt){
v.push_back(make_pair(-1,-1));
continue;
}
int cur = calc(g[node][i].first,0,node,g[node][i].second);
int cur2 = calc(g[node][i].first,1,node,g[node][i].second);
v.push_back(make_pair(cur,cur2));
sum += max(cur,cur2);
}
for(int i=0;i<v.size();i++){
if(v[i].first == -1) continue;
sum -= max(v[i].first,v[i].second);
sum += w + g[node][i].second + v[i].second;
dp[node][b] = max(dp[node][b],sum);
sum -= (w + g[node][i].second + v[i].second);
sum += max(v[i].first,v[i].second);
}
}
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));
ans = max(ans,calc(i,1,-1,0));
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int calc(int, bool, int, int)':
beads.cpp:13:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:21: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<v.size();i++){
~^~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:48: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... |