QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165550 | #6848. City Upgrading | PPP# | AC ✓ | 67ms | 13076kb | C++14 | 1.8kb | 2023-09-05 19:15:51 | 2023-09-05 19:15:52 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n;
const int maxN = 1e5 + 10;
int a[maxN];
vector<int> g[maxN];
ll dp[maxN][2][2];
ll ndp[2][2];
void dfs(int v, int p) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
dp[v][x][y] = 1e18;
}
}
dp[v][0][0] = 0;
dp[v][1][1] = a[v];
for (int to : g[v]) {
if (to == p) continue;
dfs(to, v);
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
ndp[x][y] = 1e18;
}
}
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
for (int z = 0; z < 2; z++) {
for (int t = 0; t < 2; t++) {
int ny = y | z;
if (!x && !t) continue;
ndp[x][ny] = min(ndp[x][ny], dp[v][x][y] + dp[to][z][t]);
}
}
}
}
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
dp[v][x][y] = ndp[x][y];
}
}
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
g[i].clear();
cin >> a[i];
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, -1);
cout << min(dp[1][0][1], dp[1][1][1]) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 67ms
memory: 13076kb
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