QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#616118 | #2169. 'S No Problem | Andycipation | WA | 0ms | 3544kb | C++20 | 2.6kb | 2024-10-05 22:30:44 | 2024-10-05 22:30:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
vector<int> x(n - 1), y(n - 1), w(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> x[i] >> y[i] >> w[i];
--x[i]; --y[i];
g[x[i]].push_back(i);
g[y[i]].push_back(i);
}
vector<int> pv(n);
vector<int> pe(n);
vector<int> dist(n);
auto Bfs = [&](int start) {
ranges::fill(dist, -1);
pv[start] = -1;
pe[start] = -1;
dist[start] = 0;
vector<int> que(1, start);
for (int b = 0; b < n; b++) {
int v = que[b];
for (int id : g[v]) {
int u = x[id] ^ y[id] ^ v;
if (dist[u] != -1) {
continue;
}
dist[u] = dist[v] + w[id];
pv[u] = v;
pe[u] = id;
que.push_back(u);
}
}
};
vector<int> diam;
{
Bfs(0);
int far = ranges::max_element(dist) - dist.begin();
Bfs(far);
int v = ranges::max_element(dist) - dist.begin();
diam.push_back(v);
while (v != far) {
v = pv[v];
diam.push_back(v);
}
ranges::reverse(diam);
}
const int D = diam.size();
vector<int> pos(D);
for (int i = 0; i < D; i++) {
pos[i] = dist[diam[i]];
}
vector<int> down(n);
vector<int> best(n);
function<void(int, int, int)> Dfs = [&](int v, int p, int p2) {
vector<int> downs;
for (int id : g[v]) {
int u = x[id] ^ y[id] ^ v;
if (u == p || u == p2) {
continue;
}
Dfs(u, v, -1);
downs.push_back(down[u]);
down[v] = max(down[v], w[id] + down[u]);
best[v] = max(best[v], best[u]);
}
if (downs.size() < 2) {
int sum = accumulate(downs.begin(), downs.end(), 0);
best[v] = max(best[v], sum);
} else {
int i = ranges::max_element(downs) - downs.begin();
swap(downs[i], downs[0]);
int second = *max_element(downs.begin() + 1, downs.end());
best[v] = max(best[v], downs[i] + second);
}
};
for (int i = 1; i < D - 1; i++) {
Dfs(diam[i], diam[i - 1], diam[i + 1]);
}
int ans = pos.back() + *ranges::max_element(best);
vector<int> pref(D + 1);
for (int i = 0; i < D; i++) {
pref[i + 1] = max(pref[i], down[diam[i]] + pos[i]);
}
for (int i = D - 1; i >= 0; i--) {
int lenR = down[diam[i]] + (pos.back() - pos[i]);
int lenL = pref[i];
ans = max(ans, lenL + lenR);
}
int total = accumulate(w.begin(), w.end(), 0);
cout << 2 * total - ans << '\n';
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3544kb
input:
5 1 2 1 1 3 2 1 4 3 1 5 4
output:
13
result:
wrong answer 1st lines differ - expected: '10', found: '13'