結果

問題 No.2598 Kadomatsu on Tree
ユーザー merom686merom686
提出日時 2024-01-01 23:28:24
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,346 bytes
コンパイル時間 4,076 ms
コンパイル使用メモリ 249,508 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-02-16 23:25:43
合計ジャッジ時間 12,579 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 4 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 111 ms
7,808 KB
testcase_24 AC 102 ms
7,808 KB
testcase_25 AC 102 ms
7,808 KB
testcase_26 AC 101 ms
7,808 KB
testcase_27 AC 101 ms
7,808 KB
testcase_28 AC 103 ms
7,808 KB
testcase_29 AC 106 ms
7,936 KB
testcase_30 AC 103 ms
7,808 KB
testcase_31 AC 108 ms
7,808 KB
testcase_32 AC 106 ms
7,808 KB
testcase_33 AC 107 ms
7,936 KB
testcase_34 AC 98 ms
7,680 KB
testcase_35 AC 103 ms
7,936 KB
testcase_36 AC 101 ms
7,936 KB
testcase_37 AC 104 ms
7,936 KB
testcase_38 AC 102 ms
7,936 KB
testcase_39 AC 104 ms
7,936 KB
testcase_40 AC 104 ms
7,936 KB
testcase_41 AC 134 ms
7,936 KB
testcase_42 AC 107 ms
7,936 KB
testcase_43 AC 102 ms
7,936 KB
testcase_44 AC 107 ms
7,936 KB
testcase_45 AC 128 ms
7,936 KB
testcase_46 AC 108 ms
7,936 KB
testcase_47 AC 104 ms
7,936 KB
testcase_48 AC 128 ms
20,736 KB
testcase_49 AC 9 ms
6,676 KB
testcase_50 AC 71 ms
14,080 KB
testcase_51 AC 19 ms
7,296 KB
testcase_52 AC 29 ms
7,552 KB
testcase_53 AC 31 ms
6,676 KB
testcase_54 AC 29 ms
6,676 KB
testcase_55 AC 11 ms
6,676 KB
testcase_56 AC 48 ms
6,676 KB
testcase_57 AC 17 ms
6,676 KB
testcase_58 AC 95 ms
7,808 KB
testcase_59 AC 94 ms
7,808 KB
testcase_60 AC 114 ms
19,584 KB
testcase_61 AC 124 ms
21,120 KB
testcase_62 AC 115 ms
18,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll x = 0;

struct Graph {
    struct Vertex { int n; int a; };
    struct Edge { int i, n; };
    Graph(int n, int m) : v(n, { -1, 0 }), e(m), n(n), m(0) {}
    void add_edge(int a, int b) {
        e[m] = { b, v[a].n };
        v[a].n = m;
        m++;
    }
    int dfs(int i, int p) {
        int k = 1, k1[2] = {};
        auto f = [&](int i1, int l) {
            if (v[i].a != v[i1].a) {
                k1[v[i].a > v[i1].a] += l;
                x -= (ll)l * l;
            }
        };
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            if (o.i == p) continue;
            int l = dfs(o.i, i);
            k += l;
            f(o.i, l);
        }
        if (p >= 0) f(p, n - k);
        x += (ll)k1[0] * k1[0] + (ll)k1[1] * k1[1];
        return k;
    }
    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);
    for (int j = 0; j < n - 1; j++) {
        int u, v;
        cin >> u >> v;
        u--; v--;

        g.add_edge(u, v);
        g.add_edge(v, u);
    }
    for (int i = 0; i < n; i++) {
        cin >> g.v[i].a;
    }
    g.dfs(0, -1);

    cout << x / 2 % 998244353 << endl;

    return 0;
}
0