QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#814353 | #2169. 'S No Problem | 口嗨战神 (Binyang Jiang, Dayu Wang, Hejun Dong)# | WA | 0ms | 3808kb | C++20 | 2.0kb | 2024-12-14 16:55:40 | 2024-12-14 16:55:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector G(n + 1, vector<pair<int, int>>());
ll all = 0, ans = 0;
for(int i = 1; i < n; i++) {
int a, b, w;
cin >> a >> b >> w;
G[a].push_back({b, w});
G[b].push_back({a, w});
all += 2 * w;
}
vector<multiset<int>> f(n + 1), g(n + 1);
vector<ll> res(n + 1);
auto calc = [&](int x) {
if(f[x].size() == 0) res[x] = 0;
else if(f[x].size() == 1) res[x] = *f[x].rbegin();
else if(f[x].size() >= 2) {
int mx = *f[x].rbegin();
f[x].erase(f[x].find(mx));
res[x] = mx + *f[x].rbegin();
f[x].insert(mx);
}
if(g[x].size()) res[x] = max(res[x], (ll)*g[x].rbegin());
};
auto dfs1 = [&](auto self, int x, int fz) -> void {
f[x].insert(0);
for(auto [y, w] : G[x]) if(y != fz) {
self(self, y, x);
f[x].insert(*f[y].rbegin() + w);
g[x].insert(res[y]);
}
calc(x);
};
dfs1(dfs1, 1, 0);
ans = res[1];
auto dfs2 = [&](auto self, int x, int fz) -> void {
for(auto [y, w] : G[x]) if(y != fz) {
f[x].erase(f[x].find(*f[y].rbegin() + w));
g[x].erase(g[x].find(res[y]));
calc(x);
ans = max(ans, res[x] + res[y]);
f[y].insert(*f[x].rbegin() + w);
g[y].insert(res[x]);
calc(y);
self(self, y, x);
f[y].erase(f[y].find(*f[x].rbegin() + w));
g[y].erase(g[y].find(res[x]));
calc(y);
f[x].insert(*f[y].rbegin() + w);
g[x].insert(res[y]);
calc(x);
}
};
dfs2(dfs2, 1, 0);
cout << all - ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3808kb
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'