QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#377175 | #2169. 'S No Problem | Emilan | WA | 1ms | 3592kb | C++20 | 3.1kb | 2024-04-05 00:23:59 | 2024-04-05 00:24:00 |
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);
auto dfs1 = [&](auto&& self, int u) -> void {
vector paths = {0, 0};
for (auto [v, w] : adj[u]) {
erase(adj[v], Edge{u, w});
self(self, v);
dp1[u] = max(dp1[u], dp1[v] + w);
}
ranges::sort(adj[u], {}, [&](auto e) {
return dp1[e.v] + e.w;
});
for (int j = 0; auto [v, w] : adj[u]) {
if (j == 2) break;
hang[u] += dp1[v] + w;
j++;
}
};
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)) {
int cur = 0;
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 + hang[ adj[u][i].v ]);
}
}
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]);
}
cout << 2 * sum - ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3576kb
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: 3592kb
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: 3552kb
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: 3592kb
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: -100
Wrong Answer
time: 0ms
memory: 3592kb
input:
6 1 2 5 3 2 3 2 4 1 4 5 2 4 6 4
output:
17
result:
wrong answer 1st lines differ - expected: '16', found: '17'