이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 200228;
int n;
vector<pair<int, int> > g[MAXN];
int dp[MAXN][2];
int up[MAXN][2];
void dfs(int u, int pr = -1, int last = 0) {
int sum = 0;
int add = -1e9;
for (auto h: g[u]) {
if (h.first == pr) {
continue;
}
dfs(h.first, u, h.second);
sum += max(dp[h.first][0], dp[h.first][1]);
chkmax(add, dp[h.first][0] - max(dp[h.first][0], dp[h.first][1]) + h.second + last);
}
dp[u][0] = sum;
dp[u][1] = sum + add;
}
int ans = 0;
void calcdp(int u, int pr = -1, int last = 0) {
chkmax(ans, max(up[u][0], up[u][1]) + dp[u][0]);
int cnt = 0;
int mx1 = -1e9, mx2 = -1e9;
for (auto h: g[u]) {
if (h.first == pr) {
continue;
}
int add = dp[h.first][0] - max(dp[h.first][0], dp[h.first][1]) + h.second;
cnt++;
if (mx1 < add) {
mx2 = mx1;
mx1 = add;
} else {
chkmax(mx2, add);
}
}
for (auto h: g[u]) {
if (h.first == pr) {
continue;
}
int add = dp[h.first][0] - max(dp[h.first][0], dp[h.first][1]) + h.second;
int sum = dp[u][0] - max(dp[h.first][0], dp[h.first][1]);
if (u) {
up[h.first][1] = up[u][0] + last + h.second + sum;
}
if (cnt > 1) {
int val = (add == mx1 ? mx2: mx1);
chkmax(up[h.first][1], h.second + sum + val + max(up[u][0], up[u][1]));
}
up[h.first][0] = max(up[u][0], up[u][1]) + sum;
calcdp(h.first, u, h.second);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// read(FILENAME);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
g[a].pb({b, c});
g[b].pb({a, c});
}
dfs(0);
calcdp(0);
cout << ans << endl;
}
# | 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... |