This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*huypheu
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
#include <bits/stdc++.h>
#define int long long
#define oo (LLONG_MAX/2)
using namespace std;
int dp[200007][2];
vector <pair<int,int> > ch[200007],v[200007];
int par[200007];
bool vst[200007];
void dfs(int u)
{
vst[u]=1;
for(int i=0;i<v[u].size();i++)
{
int k=v[u][i].first;
if(!vst[k])
{
ch[u].push_back(v[u][i]);
par[k]=u;
dfs(k);
}
}
return ;
}
void get_ans(int u)
{
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
get_ans(k);
}
dp[u][1]=-oo;
int tot=0;
vector <int> jo;
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
tot+=max(dp[k][0],dp[k][1]+ch[u][i].second);
jo.push_back(max(dp[k][0],dp[k][1]+ch[u][i].second));
}
for(int i=0;i<jo.size();i++)
{
int k=ch[u][i].first;
dp[u][1]=max(dp[u][1],tot-jo[i]+dp[k][0]+ch[u][i].second);
}
// for(int i=0;i<jo.size();i++)
// {
// cout << jo[i] << " ";
// }
// cout << endl;
dp[u][0]=tot;
priority_queue <int> que;
for(int i=0;i<jo.size();i++)
{
int k=ch[u][i].first;
que.push(dp[k][0]+ch[u][i].second-jo[i]);
}
int ma=0;
if(que.size()>=2)
{
ma+=que.top();que.pop();
ma+=que.top();que.pop();
}
dp[u][0]=max(dp[u][0],dp[u][0]+ma);
return ;
}
signed main()
{
int n;
cin >> n;
for(int i=1;i<n;i++)
{
int a,b,c;
cin >> a >> b >> c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
dfs(1);
get_ans(1);
// for(int i=1;i<=n;i++) cout << dp[i][0] << " " << dp[i][1] << endl;
// cout << "concak" << endl;
cout << dp[1][0] << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(long long int)':
beads.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[u].size();i++)
~^~~~~~~~~~~~
beads.cpp: In function 'void get_ans(long long int)':
beads.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:56:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<jo.size();i++)
~^~~~~~~~~~
beads.cpp:68:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<jo.size();i++)
~^~~~~~~~~~
# | 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... |