結果

問題 No.1221 木 *= 3
ユーザー merom686merom686
提出日時 2020-09-04 22:29:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,449 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 90,140 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2023-08-17 17:11:10
合計ジャッジ時間 3,259 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 53 ms
9,596 KB
testcase_08 AC 52 ms
9,532 KB
testcase_09 AC 55 ms
9,600 KB
testcase_10 AC 55 ms
9,588 KB
testcase_11 AC 54 ms
9,296 KB
testcase_12 AC 48 ms
5,604 KB
testcase_13 AC 46 ms
5,708 KB
testcase_14 AC 49 ms
5,628 KB
testcase_15 AC 48 ms
5,756 KB
testcase_16 AC 49 ms
5,672 KB
testcase_17 AC 52 ms
5,620 KB
testcase_18 AC 51 ms
5,704 KB
testcase_19 AC 53 ms
5,636 KB
testcase_20 AC 54 ms
5,892 KB
testcase_21 AC 52 ms
5,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct Vertex { int n, a, b; };
    struct Edge { int i, n; };
    Graph(int n, int m) : v(n, { -1, 0, 0 }), e(m), n(n), m(0) {}
    void add_edge(int i, int j) {
        e[m] = { j, v[i].n };
        v[i].n = m;
        m++;
    }
    void dfs(int i, int p, ll &s0, ll &s1) {
        ll r0 = v[i].a, r1 = 0;

        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge& o = e[j];
            if (o.i == p) continue;

            ll t0, t1;
            dfs(o.i, i, t0, t1);
            r0 += max(t0, t1);
            r1 += max(t0, t1 + v[o.i].b + v[i].b);
        }
        s0 = r0; s1 = r1;
    }
    void solve() {
        for (int i = 0; i < n; i++) {
            cin >> v[i].a;
        }
        for (int i = 0; i < n; i++) {
            cin >> v[i].b;
        }
        for (int i = 0; i < n - 1; i++) {
            int a, b;
            cin >> a >> b;
            a--; b--;

            add_edge(a, b);
            add_edge(b, a);
        }
        ll s0, s1;
        dfs(0, -1, s0, s1);
        cout << max(s0, s1) << endl;
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    Graph g(n, (n - 1) * 2);
    g.solve();

    return 0;
}
0