이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//
//using namespace __gnu_pbds;
//
//template<typename T>
//using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
//template<typename T>
//using normal_queue = priority_queue<T, vector<T>, greater<>>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define popb pop_back
#define eb emplace_back
#define fi first
#define se second
#define itn int
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
template<typename T>
bool ckmn(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmx(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int bit(int x, int b) {
return (x >> b) & 1;
}
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const ll infL = 3e18;
const int infI = 1'000'000'000 + 7;
const int infMI = 0x3f3f3f3f; //a little bigger than 1e9
const ll infML = 0x3f3f3f3f3f3f3f3fLL; //4.5e18
const int N = 200002;
const int mod = 998244353;
const ld eps = 1e-9;
vector<pair<int, int>> g[N];
int dp[3][N];
//dp[0][v] - just simple, if we dont merge sons in our subtree and also not use our dad
//dp[1][v] - also if we dont merge sons in our subtree, but use our dad
//dp[2][v] - answer
int p[N], wp[N];
//wp - weight of edge to our parent
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int a, b, w;
cin >> a >> b >> w;
--a, --b;
g[a].emplace_back(b, w);
g[b].emplace_back(a, w);
}
p[0] = -1;
function<void(int)> dfs = [&dfs](int v) {
int mx1 = -infI, mx2 = -infI;
int sum = 0;
for (auto [to, w]: g[v]) {
if (to != p[v]) {
p[to] = v;
wp[to] = w;
dfs(to);
int mx_now = max(dp[0][to], dp[1][to]);
sum += mx_now;
int val = dp[0][to] + w - mx_now;
if (val > mx1) {
mx2 = mx1;
mx1 = val;
} else if (mx2 < val){
mx2 = val;
}
}
}
dp[0][v] = dp[2][v] = sum;
for (auto [to, w]: g[v]) {
if (to != p[v]) {
int mx_now = max(dp[0][to], dp[1][to]);
int val = dp[0][v] - mx_now;
if (p[v] != -1) {
ckmx(dp[1][v], dp[0][to] + w + val + wp[v]);
}
int mx = w + dp[0][to] - mx_now;
if (mx == mx1) {
mx = mx2;
} else {
mx = mx1;
}
ckmx(dp[2][v], dp[2][to] + val + mx + w);
ckmx(dp[2][v], dp[2][to] + val);
for (auto [u, k] : g[to]) {
if (u != p[to]) {
int valu = dp[0][to] - max(dp[0][u], dp[1][u]);
ckmx(dp[2][v], dp[2][u] + k + w + val + valu);
}
}
}
}
};
dfs(0);
cout << dp[2][0];
return 0;
}
# | 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... |