QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#140967 | #6848. City Upgrading | neko_nyaa | AC ✓ | 117ms | 10408kb | C++23 | 1.6kb | 2023-08-17 02:08:37 | 2023-08-17 02:08:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
int los(tuple<int, int, int> a) {
return get<0>(a) - min(get<0>(a), get<1>(a));
}
bool cmp(tuple<int, int, int> a, tuple<int, int, int> b) {
return los(a) < los(b);
}
tuple<int, int, int> go(int now, int prv, vector<vector<int>> &adj, vector<int> &a) {
// 1: this node is picked
// 2: this node is not picked, covered
// 3: this node is not covered
// 1 --> all child take min(1, 2, 3) + cost
// 2 --> one child takes 1, all takes min(1, 2)
// 3 --> all child take min(1, 2)
vector<tuple<int, int, int>> chz;
for (int u: adj[now]) {
if (u != prv) {
chz.push_back(go(u, now, adj, a));
}
}
int ans1 = a[now];
for (auto [x, y, z]: chz) {
ans1 += min(x, min(y, z));
}
int ans2 = 0;
if (chz.empty()) ans2 = INF;
else {
sort(chz.begin(), chz.end(), cmp);
ans2 += get<0>(chz[0]);
for (int i = 1; i < chz.size(); i++) {
ans2 += min(get<0>(chz[i]), get<1>(chz[i]));
}
}
int ans3 = 0;
for (auto [x, y, z]: chz) {
ans3 += min(x, y);
}
return {ans1, ans2, ans3};
}
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
auto [v1, v2, v3] = go(0, 0, adj, a);
cout << min(v1, v2) << '\n';
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 117ms
memory: 10408kb
input:
1000 23 97976 19679 92424 30861 84737 62896 66360 54204 29129 13621 23631 61405 66883 53853 23079 66231 77727 88749 71092 97425 85117 79396 67781 1 2 2 3 1 4 1 5 5 6 1 7 5 8 3 9 9 10 5 11 8 12 9 13 3 14 4 15 6 16 9 17 8 18 3 19 8 20 11 21 3 22 19 23 3 93601 96295 41363 1 2 2 3 26 19405 8067 19601 81...
output:
419504 96295 334958 636478 114964 628081 464114 560260 479222 121326 64291 607551 278318 56546 413182 159607 313038 362098 635804 380900 594905 358972 325402 765893 705879 755158 206101 49049 7285 244319 208205 77015 623675 348208 515431 96136 607270 610292 656473 119999 320041 403718 80158 141749 4...
result:
ok 1000 lines