QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#59561 | #2169. 'S No Problem | losPatrons# | WA | 3ms | 3540kb | C++17 | 2.3kb | 2022-10-31 01:30:59 | 2022-10-31 01:30:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) (x).size())
typedef long long ll;
typedef long double ld;
void ins(vector<int> &v, int x) {
v.push_back(x);
for (int i = sz(v) - 1; i > 0 && v[i] > v[i - 1]; i--)
swap(v[i], v[i - 1]);
if (sz(v) > 3)
v.pop_back();
}
void rem(vector<int> &v, int x) {
auto it = find(v.begin(), v.end(), x);
if (it != v.end())
v.erase(it);
}
struct all_dir_dp {
struct data {
int lp = 0, ld = 0;
};
vector<int> lp, ld;
void init(int i) {
lp.clear();
ld.clear();
}
void add(data &d) {
ins(lp, d.lp);
ins(ld, d.ld);
}
void remove(data &d) {
rem(lp, d.lp);
rem(ld, d.ld);
}
data compute() {
return {sz(lp) > 0 ? lp[0] : 0, max(sz(ld) > 0 ? ld[0] : 0, sz(lp) > 1 ? lp[0] + lp[1] : (sz(lp) > 0 ? lp[0] : 0))};
}
int n;
vector<data> dpup, dpdown, dp;
vector<vector<pair<int, int>>> &e;
all_dir_dp(vector<vector<pair<int, int>>> &e): n(sz(e)), dpup(n), dpdown(n), dp(n), e(e) {
dfsup(0);
dfsdown(0);
}
void dfsup(int i, int p = -1) {
int ud = 0;
for (auto [j, c] : e[i])
if (j != p)
dfsup(j, i);
else
ud = c;
init(i);
for (auto [j, c] : e[i])
if (j != p)
add(dpup[j]);
dpup[i] = compute();
dpup[i].lp += ud;
}
void dfsdown(int i, int p = -1) {
init(i);
if (p != -1)
add(dpdown[i]);
for (auto [j, c] : e[i])
if (j != p)
add(dpup[j]);
dp[i] = compute();
for (auto [j, c] : e[i])
if (j != p) {
remove(dpup[j]);
dpdown[j] = compute();
dpdown[j].lp += c;
add(dpup[j]);
}
for (auto [j, c] : e[i])
if (j != p)
dfsdown(j, i);
}
};
int n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(12);
cin >> n;
vector<vector<pair<int, int>>> e(n);
int a, b, d;
int ts = 0;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b >> d, a--, b--;
e[a].emplace_back(b, d);
e[b].emplace_back(a, d);
ts += 2 * d;
}
all_dir_dp dp(e);
int r = 0;
for (int i = 0; i < n; i++)
r = max(r, dp.dpup[i].ld + dp.dpdown[i].ld);
cout << ts - r << "\n";
}
详细
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 3540kb
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'