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"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int dp[MAXN][2];
vector<PII>adj[MAXN];
void dfs(int nd,int pr=-1,int w_p=-1){
dp[nd][0]=0;int mx=-INF,mx1=-INF;
for(int i=0;i<int(adj[nd].size());i++){
int to=adj[nd][i].ff;
int w=adj[nd][i].ss;
if(to==pr)continue;
dfs(to,nd,w);
w+=dp[to][0]-dp[to][1];
dp[nd][0]+=dp[to][1];
if(mx<w) mx1=mx,mx=w;
else umax(mx1,w);
}
if(~pr)dp[nd][1]=dp[nd][0]+max(0,mx+w_p);
else dp[nd][0]+=max(0,mx+mx1);
}
int main(){
//freopen("file.in", "r", stdin);
int n;
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
adj[u].pb(mp(v,w));
adj[v].pb(mp(u,w));
}int ans=0;
vector<int>roots;
for(int i=1;i<=n;i++)roots.pb(i);
random_shuffle(all(roots));
for(int i=0;i<n;i++){
if((double)clock()/CLOCKS_PER_SEC>0.98)break;
dfs(roots[i]);
umax(ans,dp[roots[i]][0]);
}
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
beads.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | 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... |