QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377193 | #2169. 'S No Problem | Emilan | WA | 0ms | 3832kb | C++20 | 4.1kb | 2024-04-05 00:59:07 | 2024-04-05 00:59:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
using uint = unsigned;
using ull = unsigned long long;
using u128 = __uint128_t;
using pii = pair<int, int>;
int sz(auto&& x) { return int(size(x)); }
const auto& range = views::iota;
// End template
struct Edge {
int v, w;
friend auto operator<=>(Edge, Edge) = default;
};
void solve() {
int n;
cin >> n;
vector<vector<Edge>> adj(n);
for (auto _ : range(0, n - 1)) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector dp1(n, 0);
vector hang(n, 0);
vector hang2(n, 0);
auto dfs1 = [&](auto&& self, int u) -> void {
for (auto [v, w] : adj[u]) {
erase(adj[v], Edge{u, w});
self(self, v);
dp1[u] = max(dp1[u], dp1[v] + w);
hang[u] = max(hang[u], hang[v]);
hang2[u] = max(hang2[u], hang2[v] + w);
}
ranges::sort(adj[u], {}, [&](auto e) {
return dp1[e.v] + e.w;
});
{
int cur = 0;
for (int i = 0; auto [v, w] : adj[u]) {
if (i == 2) break;
cur += dp1[v] + w;
i++;
}
hang[u] = max(hang[u], cur);
}
{
int cur = 0;
for (int i = 0; auto [v1, w1] : adj[u]) {
int cur2 = hang[v1];
for (int j = 0, cnt = 0; auto [v2, w2] : adj[u]) if (i != j) {
if (cnt == 1) break;
cur2 += dp1[v2] + w2;
cnt++;
}
cur = max(cur, cur2);
i++;
}
hang2[u] = max(hang2[u], cur);
}
};
dfs1(dfs1, 0);
vector dp2(n, 0);
auto dfs2 = [&](auto&& self, int u) -> void {
auto m = sz(adj[u]);
vector pre(m + 1, 0);
pre[0] = dp2[u];
for (auto i : range(0, m)) {
auto [v, w] = adj[u][i];
pre[i + 1] = max(pre[i], dp1[v] + w);
}
vector suf(m + 1, 0);
for (auto i : range(0, m) | views::reverse) {
auto [v, w] = adj[u][i];
suf[i] = max(suf[i + 1], dp1[v] + w);
}
for (auto i : range(0, m)) {
auto [v, w] = adj[u][i];
dp2[v] = max(pre[i], suf[i + 1]) + w;
self(self, v);
}
};
dfs2(dfs2, 0);
int sum = 0;
for (auto u : range(0, n)) {
for (auto [v, w] : adj[u]) {
sum += w;
}
}
int ans = 0;
for (auto u : range(0, n)) {
int no_par = 0;
int par = 0;
for (int i = 0; auto [v, w] : adj[u]) {
if (i == 4) break;
if (i == 3) {
no_par = par + dp1[v] + w;
} else {
par += dp1[v] + w;
}
i++;
}
ans = max({ans, no_par, par + dp2[u]});
}
for (auto u : range(0, n)) {
auto m = sz(adj[u]);
for (auto i : range(0, m)) {
auto cur = hang[ adj[u][i].v ];
for (int cnt = 0; auto j : range(0, m)) if (j != i) {
if (cnt == 2) break;
auto [v, w] = adj[u][j];
cur += dp1[v] + w;
cnt++;
}
ans = max(ans, cur);
}
}
for (auto u : range(0, n)) {
vector hangs = {0, 0};
for (auto [v, w] : adj[u]) {
hangs.push_back(hang[v]);
}
ranges::sort(hangs | views::reverse);
ans = max(ans, hangs[0] + hangs[1]);
}
for (auto u : range(0, n)) {
auto m = sz(adj[u]);
for (auto i : range(0, m)) {
auto cur = hang2[ adj[u][i].v ] + adj[u][i].w;
for (auto j : range(0, m)) if (j != i) {
auto [v, w] = adj[u][j];
cur += dp1[v] + w;
break;
}
ans = max(ans, cur);
}
}
cout << 2 * sum - ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3832kb
input:
5 1 2 1 1 3 2 1 4 3 1 5 4
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
6 6 2 1 4 6 1 3 1 1 1 5 1 1 6 10
output:
15
result:
ok single line: '15'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
6 1 3 1 3 2 1 3 4 10 5 4 1 4 6 1
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
6 1 3 1 2 3 1 3 5 1 4 5 1 5 6 1
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
6 1 2 5 3 2 3 2 4 1 4 5 2 4 6 4
output:
16
result:
ok single line: '16'
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 3604kb
input:
6 1 6 8 2 6 2 3 6 3 4 6 5 5 6 1
output:
24
result:
wrong answer 1st lines differ - expected: '20', found: '24'