/*
$$$$$$$\ $$\ $$$$$$$\
$$ __$$\ \__| $$ __$$\
$$ | $$ | $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$ | $$ | $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\
$$$$$$$\ |$$ __$$\ $$ __$$\ $$ |$$ _____|$$$$$$$\ | \____$$\ $$ __$$\ $$ _____|\____$$\
$$ __$$\ $$ / $$ |$$ | \__|$$ |\$$$$$$\ $$ __$$\ $$$$$$$ |$$ | \__|$$ / $$$$$$$ |
$$ | $$ |$$ | $$ |$$ | $$ | \____$$\ $$ | $$ |$$ __$$ |$$ | $$ | $$ __$$ |
$$$$$$$ |\$$$$$$ |$$ | $$ |$$$$$$$ |$$$$$$$ |\$$$$$$$ |$$ | \$$$$$$$\\$$$$$$$ |
\_______/ \______/ \__| \__|\_______/ \_______/ \_______|\__| \_______|\_______|
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define PB push_back
#define MP make_pair
#define INS insert
#define LB lower_bound
#define UB upper_bound
#define pii pair <int,int>
#define pll pair <long long, long long>
#define X first
#define Y second
#define _ << " " <<
#define sz(x) (int)x.size()
#define all(a) (a).begin(),(a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i > (b); --i)
#define FORA(i, x) for (auto &i : x)
#define REP(i, n) FOR(i, 0, n)
#define BITS(x) __builtin_popcount(x)
#define SQ(a) (a) * (a)
#define TRACE(x) cout << #x " = " << (x) << '\n';
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define umap unordered_map
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <pii> vpi;
typedef vector <ll> vll;
typedef vector <pll> vpl;
typedef vector <string> vs;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
const int INF = 1e9 + 10;
const ll INFL = 1e18 + 10;
const int ABC = 30;
inline int sum(int a, int b){
if (a + b < 0)
return (a + b + MOD) % MOD;
return (a + b) % MOD;
}
inline void add(int &a, int b){
a = sum(a, b);
}
inline int mul(ll a, ll b){
return ((a % MOD) * ((ll)b % MOD)) % MOD;
}
inline int sub(int a, int b){
return (a - b + MOD) % MOD;
}
inline int fast_pot(ll pot, ll n){
ll ret = 1;
while (n){
if (n & 1LL)
ret = (ret * pot) % MOD;
pot = (pot * pot) % MOD;
n >>= 1LL;
}
return ret;
}
const int N = 2e5 + 10;
ll n, memo[3][N];
vpl e[N];
ll dp(int t, int node, int dad){
int d = 0;
FORA(nxt, e[node])
if (nxt.X != dad)
d++;
if (d == 0)
return (t != 1 ? 0 : -INFL);
ll &ret = memo[t][node];
if (~ret)
return ret;
ret = -INFL;
ll uk = 0;
vll v;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
uk += max(dp(1, nxt.X, node) + nxt.Y, dp(0, nxt.X, node));
v.PB(dp(2, nxt.X, node) - max(dp(1, nxt.X, node) + nxt.Y, dp(0, nxt.X, node)) + nxt.Y);
}
sort(all(v));
reverse(all(v));
if (t == 0 || t == 2){
ret = max(ret, uk);
if (sz(v) >= 2)
ret = max(ret, uk + v[0] + v[1]);
} else if (t == 1){
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
ret = max(ret, uk + dp(2, nxt.X, node) + nxt.Y - max(dp(1, nxt.X, node) + nxt.Y, dp(0, nxt.X, node)));
}
}
/*if (t == 2){
ll uk = 0;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
uk += max(dp(1, nxt.X, node), dp(0, nxt.X, node));
}
ret = max(ret, uk);
vpl v;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
ret = max(ret, dp(1, nxt.X, node) + nxt.Y + uk - dp(0, nxt.X, node));
v.PB({dp(2, nxt.X, node) - dp(0, nxt.X, node) + nxt.Y, nxt.X});
}
sort(all(v));
reverse(all(v));
if (sz(v) >= 2){
ret = max(ret, uk + v[0].X + v[1].X);
}
} else if (t == 1){
ll uk = 0;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
uk += dp(0, nxt.X, node);
}
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
ret = max(ret, dp(2, nxt.X, node) + nxt.Y + uk - dp(0, nxt.X, node));
}
} else {
ll uk = 0;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
uk += dp(0, nxt.X, node);
}
ret = max(ret, uk);
vpl v;
FORA(nxt, e[node]){
if (nxt.X == dad)
continue;
ret = max(ret, dp(1, nxt.X, node) + nxt.Y + uk - dp(0, nxt.X, node));
v.PB({dp(2, nxt.X, node) - dp(0, nxt.X, node) + nxt.Y, nxt.X});
}
sort(all(v));
reverse(all(v));
if (sz(v) >= 2){
ret = max(ret, uk + v[0].X + v[1].X);
}
}*/
//cout << t _ node + 1 _ ret << '\n';
return ret;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
REP(i, n - 1){
int x, y; cin >> x >> y;
ll w; cin >> w;
x--, y--;
e[x].PB({y, w});
e[y].PB({x, w});
}
memset(memo, -1, sizeof memo);
cout << dp(0, 0, -1) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9772 KB |
Output is correct |
3 |
Correct |
6 ms |
9644 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Incorrect |
6 ms |
9676 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9772 KB |
Output is correct |
3 |
Correct |
6 ms |
9644 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Incorrect |
6 ms |
9676 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9772 KB |
Output is correct |
3 |
Correct |
6 ms |
9644 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Incorrect |
6 ms |
9676 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9772 KB |
Output is correct |
3 |
Correct |
6 ms |
9644 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Incorrect |
6 ms |
9676 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |