QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#141990#6848. City UpgradingsupepapupuAC ✓65ms18132kbC++171.3kb2023-08-18 09:41:082023-08-18 09:41:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-18 09:41:10]
  • 评测
  • 测评结果:AC
  • 用时:65ms
  • 内存:18132kb
  • [2023-08-18 09:41:08]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;

inline int read() {
    int f = 1, k = 0;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        k = k * 10 + c - '0';
        c = getchar();
    }
    return f * k;
}

int w[N];
ll f[N], g[N], h[N];
vector<int> G[N];

void dfs(int u, int fa) {
    f[u] = w[u], h[u] = 0;
    ll mn = 1e18;
    for (int v: G[u]) 
        if (v != fa) {
            dfs(v, u);
            f[u] += min(f[v], min(g[v], h[v]));
            h[u] += min(f[v], g[v]);
            mn = min(mn, max(0ll, f[v] - g[v]));
        }
    g[u] = h[u] + mn;
    // cout << u << ' ' << f[u] << ' ' << g[u] << ' ' << h[u] << el;
}

void solve() {
    int n = read();
    for (int i = 1; i <= n; ++i) w[i] = read(), G[i].clear();
    for (int i = 1; i < n; ++i) {
        int a = read(), b = read();
        G[a].emplace_back(b), G[b].emplace_back(a);
    }
    dfs(1, 0);
    cout << min(f[1], g[1]) << el;
}

int main() {
    // ios::sync_with_stdio(0); cin.tie(0);
    int tcase = 1;
    cin >> tcase;
    while (tcase--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 65ms
memory: 18132kb

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