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 = 200010;
vector< pair<int,int> > g[N];
int n , dp[N][2] , q[N], in[N];
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 , st = 0 , en = 0 , curnode , w , sum = 0 , mx = 0 , i = 0;
for(int node=1;node<=n;node++){
if(g[node].size() == 1){
st = 0 , en = 0 ,sum = 0;
for(i=1;i<=n;i++){
in[i] = g[i].size() - (i != node);
if(in[i] == 0)
q[en++] = i;
}
while(st < en){
curnode = q[st++];
dp[curnode][1] = dp[curnode][0] = 0;
sum = 0 , mx = -oo;
for(i=0;i<g[curnode].size();i++){
if(in[g[curnode][i].first] != 0){
w = g[curnode][i].second;
in[g[curnode][i].first]--;
if(in[g[curnode][i].first] == 0){
q[en++] = g[curnode][i].first;
}
}
else{
sum += max(dp[g[curnode][i].first][0],dp[g[curnode][i].first][1]);
mx = max(mx,dp[g[curnode][i].first][1] + g[curnode][i].second - max(dp[g[curnode][i].first][0],dp[g[curnode][i].first][1]));
}
}
dp[curnode][1] = sum;
dp[curnode][0] = sum + mx + w;
}
ans = max(ans,dp[node][1]);
}
}
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:31:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<g[curnode].size();i++){
~^~~~~~~~~~~~~~~~~~
beads.cpp:11:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:14:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~
beads.cpp:45:35: warning: 'w' may be used uninitialized in this function [-Wmaybe-uninitialized]
dp[curnode][0] = sum + mx + 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... |