QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431392 | #6848. City Upgrading | ucup-team3591# | AC ✓ | 78ms | 10348kb | C++20 | 1.3kb | 2024-06-05 14:15:57 | 2024-06-05 14:15:57 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
template<typename T>
inline void chkmin(T &a, const T &b) {if (a > b) a = b;}
constexpr int N = 1e5 + 10;
constexpr i64 inf = 1e18;
int n;
std::basic_string<int> G[N];
i64 f[N][3];
int a[N];
inline void add(int u, int v) {
G[u].push_back(v);
}
inline void clear() {
for (int i = 1; i <= n; i++) {
G[i].clear();
f[i][0] = f[i][1] = f[i][2] = 0;
}
}
void dfs(int u, int fa) {
i64 sum = 0;
for (const int &v : G[u]) {
if (v == fa)
continue;
dfs(v, u);
f[u][1] += std::min({f[v][0], f[v][1], f[v][2]});
f[u][2] += std::min({f[v][0], f[v][1]});
sum += std::min(f[v][0], f[v][1]);
}
f[u][0] = inf;
for (const int &v : G[u]) {
if (v == fa)
continue;
chkmin(f[u][0], sum - std::min(f[v][0], f[v][1]) + f[v][1]);
}
}
void solve() {
std::cin >> n;
clear();
for (int i = 1; i <= n; i++)
std::cin >> f[i][1];
if (n == 1) {
std::cout << f[1][1] << '\n';
return ;
}
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
add(u, v), add(v, u);
}
dfs(1, 0);
std::cout << std::min(f[1][0], f[1][1]) << '\n';
}
int main() {
#ifdef HeratinoNelofus
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 78ms
memory: 10348kb
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