/*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
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 |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |