#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll f[200005][2], mx[200005], mx2[200005], ans;
vector<ll> g[200005][2], h[200005][2];
vector<pair<ll, ll> > child[200005];
pair<ll, ll> par[200005];
void dfs(ll u)
{
// cout << "dfs " << u << '\n';
mx[u]=mx2[u]=-1e18;
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
dfs(v);
f[u][0]+=max(f[v][0], f[v][1]+w);
ll z=f[v][0]+w-max(f[v][0], f[v][1]+w);
if (mx[u]<=z)
{
mx2[u]=mx[u];
mx[u]=z;
}
else if (mx2[u]<z)
mx2[u]=z;
}
f[u][1]=f[u][0]+mx[u];
// cout << "f[" << u << "][0] " << f[u][0] << '\n';
// cout << "f[" << u << "][1] " << f[u][1] << '\n';
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
g[u][0].push_back(f[u][0]-max(f[v][0], f[v][1]+w));
if (mx[u]==f[v][0]+w-max(f[v][0], f[v][1]+w))
g[u][1].push_back(g[u][0][i]+mx2[u]);
else
g[u][1].push_back(g[u][0][i]+mx[u]);
}
}
void dfs2(ll u)
{
// cout << "dfs2 " << u << '\n';
ll p=par[u].first, j=par[u].second;
ll x=(u==1?0:child[p][j].second);
for (ll i=0; i<child[u].size(); i++)
{
ll v=child[u][i].first, w=child[u][i].second;
if (u==1)
{
h[u][0].push_back(g[u][0][i]);
h[u][1].push_back(g[u][1][i]);
}
else
{
h[u][0].push_back(g[u][0][i]+max(h[p][0][j], h[p][1][j]+x));
h[u][1].push_back(max(g[u][0][i]+h[p][0][j]+x, g[u][1][i]+max(h[p][0][j], h[p][1][j]+x)));
}
ans=max(ans, f[v][0]+max(h[u][0][i], h[u][1][i]+w));
dfs2(v);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin >> n;
for (ll i=1; i<n; i++)
{
ll u, v, w;
cin >> u >> v >> w;
par[v]={u, child[u].size()};
child[u].push_back({v, w});
}
dfs(1);
dfs2(1);
cout << max(ans, f[1][0]);
}
Compilation message
beads.cpp: In function 'void dfs(long long int)':
beads.cpp:12:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (ll i=0; i<child[u].size(); i++)
| ~^~~~~~~~~~~~~~~~
beads.cpp:29:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (ll i=0; i<child[u].size(); i++)
| ~^~~~~~~~~~~~~~~~
beads.cpp: In function 'void dfs2(long long int)':
beads.cpp:44:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (ll i=0; i<child[u].size(); i++)
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23712 KB |
Output is correct |
3 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23712 KB |
Output is correct |
3 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23712 KB |
Output is correct |
3 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23712 KB |
Output is correct |
3 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |