#include <bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define fi first
#define se second
using namespace std;
const int N = 3e5 + 5;
const ll mod = 1e9 + 7;
ll n, m, a[N], b[N], dp[N], gp[N], tong;
vector<pll> adj[N];
void dfs(ll u, ll p, ll s)
{
ll sum = 0;
ll mx1 = -mod, mx2 = -mod;
for (pll v : adj[u])
{
if (v.se == p)
continue;
dfs(v.se, u, v.fi);
sum += max(dp[v.se], gp[v.se]);
int val = dp[v.se] - max(dp[v.se], gp[v.se]) + v.fi;
if (mx1 < val)
{
mx2 = mx1;
mx1 = val;
}
else if (mx2 < val)
mx2 = val;
}
dp[u] = sum;
for (pll v : adj[u])
{
if (v.se == p)
continue;
int val = dp[v.se] - max(dp[v.se], gp[v.se]);
gp[u] = max(gp[u], sum + val + v.fi + s);
if (v.fi + dp[v.se] - max(dp[v.se], gp[v.se]) == mx1)
val = mx2;
else
val = mx1;
dp[u] = max(dp[u], dp[v.se] + val + v.fi + sum - max(dp[v.se], gp[v.se]));
}
}
void sol()
{
cin >> n;
for (int i = 1; i < n; i++)
{
ll x, y, w;
cin >> x >> y >> w;
adj[x].pb({w, y});
adj[y].pb({w, x});
}
dfs(1, 0, 0);
cout << dp[1];
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int test = 1;
//cin >> test;
while (test-- > 0)
sol();
return 0;
}
/*
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
*/
// ctrl + F8 = compile
// ctrl + F9 = run
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7284 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7368 KB |
Output is correct |
6 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7284 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7368 KB |
Output is correct |
6 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7284 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7368 KB |
Output is correct |
6 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7284 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7368 KB |
Output is correct |
6 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |