結果

問題 No.2598 Kadomatsu on Tree
ユーザー merom686merom686
提出日時 2024-01-01 22:59:01
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 3,122 ms
コンパイル使用メモリ 251,072 KB
実行使用メモリ 13,400 KB
最終ジャッジ日時 2024-01-01 22:59:13
合計ジャッジ時間 10,676 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 3 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 3 ms
6,548 KB
testcase_14 AC 4 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 4 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 3 ms
6,548 KB
testcase_19 AC 4 ms
6,548 KB
testcase_20 AC 3 ms
6,548 KB
testcase_21 AC 5 ms
6,548 KB
testcase_22 AC 4 ms
6,548 KB
testcase_23 AC 112 ms
9,292 KB
testcase_24 AC 108 ms
9,308 KB
testcase_25 AC 106 ms
9,324 KB
testcase_26 AC 105 ms
9,320 KB
testcase_27 AC 119 ms
9,340 KB
testcase_28 AC 120 ms
9,308 KB
testcase_29 AC 122 ms
9,348 KB
testcase_30 AC 130 ms
9,300 KB
testcase_31 AC 125 ms
9,332 KB
testcase_32 AC 117 ms
9,264 KB
testcase_33 AC 128 ms
9,408 KB
testcase_34 AC 117 ms
9,236 KB
testcase_35 AC 129 ms
9,464 KB
testcase_36 AC 124 ms
9,432 KB
testcase_37 AC 123 ms
9,412 KB
testcase_38 AC 141 ms
9,468 KB
testcase_39 AC 132 ms
9,468 KB
testcase_40 AC 129 ms
9,468 KB
testcase_41 AC 131 ms
9,468 KB
testcase_42 AC 128 ms
9,468 KB
testcase_43 AC 128 ms
9,468 KB
testcase_44 AC 123 ms
9,468 KB
testcase_45 AC 125 ms
9,468 KB
testcase_46 AC 126 ms
9,468 KB
testcase_47 AC 144 ms
9,468 KB
testcase_48 AC 131 ms
12,976 KB
testcase_49 AC 9 ms
6,548 KB
testcase_50 AC 75 ms
9,452 KB
testcase_51 AC 19 ms
6,548 KB
testcase_52 AC 30 ms
6,548 KB
testcase_53 AC 35 ms
6,548 KB
testcase_54 AC 29 ms
6,548 KB
testcase_55 AC 12 ms
6,548 KB
testcase_56 AC 54 ms
7,184 KB
testcase_57 AC 20 ms
6,548 KB
testcase_58 AC 117 ms
9,360 KB
testcase_59 AC 115 ms
9,332 KB
testcase_60 AC 134 ms
12,736 KB
testcase_61 AC 142 ms
13,400 KB
testcase_62 AC 124 ms
12,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Graph {
    struct Vertex { int n; int a; };
    struct Edge { int i, n; int k; };
    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, 0 };
        v[a].n = m;
        m++;
    }
    int dfs(int i, int p) {
        int k = 1;
        int j1 = -1;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            if (o.i == p) {
                j1 = j;
                continue;
            }
            int l = dfs(o.i, i);
            k += l;
            o.k = l;
            //cout << i + 1 << ' ' << o.i + 1 << ' ' << l << endl;
        }
        if (j1 >= 0) {
            e[j1].k = n - k;
            //cout << i + 1 << ' ' << p + 1 << ' ' << n - k << endl;
        }
        return k;
    }
    void solve() {
        dfs(0, -1);

        ll x = 0;
        for (int i = 0; i < n; i++) {
            //ll y = x;
            int k[2] = {};
            for (int j = v[i].n; j >= 0; j = e[j].n) {
                Edge &o = e[j];
                if (v[i].a == v[o.i].a) continue;
                int t = v[i].a > v[o.i].a;
                k[t] += o.k;
            }
            for (int j = v[i].n; j >= 0; j = e[j].n) {
                Edge &o = e[j];
                if (v[i].a == v[o.i].a) continue;
                int t = v[i].a > v[o.i].a;
                //cout << i + 1 << ' ' << o.i + 1 << ' ' << o.k << ' ' << (k[t] - o.k) << endl;
                x += (ll)(k[t] - o.k) * o.k;
            }
            //cout << i + 1 << ' ' << x - y << endl;
        }
        cout << x / 2 % 998244353 << 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);
    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.solve();

    return 0;
}
0