結果

問題 No.2598 Kadomatsu on Tree
ユーザー t98slidert98slider
提出日時 2024-01-05 15:28:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,128 bytes
コンパイル時間 7,099 ms
コンパイル使用メモリ 263,844 KB
実行使用メモリ 32,560 KB
最終ジャッジ日時 2024-01-05 15:28:39
合計ジャッジ時間 14,237 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
6,676 KB
testcase_16 AC 4 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 4 ms
6,676 KB
testcase_19 AC 4 ms
6,676 KB
testcase_20 AC 4 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 164 ms
14,732 KB
testcase_24 AC 169 ms
14,948 KB
testcase_25 AC 170 ms
14,956 KB
testcase_26 AC 163 ms
14,752 KB
testcase_27 AC 161 ms
14,892 KB
testcase_28 AC 162 ms
14,948 KB
testcase_29 AC 225 ms
15,100 KB
testcase_30 AC 163 ms
14,740 KB
testcase_31 AC 163 ms
14,964 KB
testcase_32 AC 161 ms
14,920 KB
testcase_33 AC 179 ms
15,136 KB
testcase_34 AC 159 ms
14,700 KB
testcase_35 AC 169 ms
15,168 KB
testcase_36 AC 208 ms
15,152 KB
testcase_37 AC 180 ms
15,140 KB
testcase_38 AC 175 ms
15,172 KB
testcase_39 AC 176 ms
15,172 KB
testcase_40 AC 173 ms
15,172 KB
testcase_41 AC 179 ms
15,172 KB
testcase_42 AC 215 ms
15,172 KB
testcase_43 AC 183 ms
15,172 KB
testcase_44 AC 179 ms
15,172 KB
testcase_45 AC 180 ms
15,172 KB
testcase_46 AC 179 ms
15,172 KB
testcase_47 AC 187 ms
15,172 KB
testcase_48 AC 225 ms
31,920 KB
testcase_49 AC 13 ms
6,676 KB
testcase_50 AC 115 ms
21,248 KB
testcase_51 AC 27 ms
9,728 KB
testcase_52 AC 47 ms
10,752 KB
testcase_53 AC 45 ms
8,528 KB
testcase_54 AC 38 ms
7,760 KB
testcase_55 AC 14 ms
6,676 KB
testcase_56 AC 68 ms
11,204 KB
testcase_57 AC 23 ms
6,676 KB
testcase_58 AC 171 ms
14,904 KB
testcase_59 AC 149 ms
14,964 KB
testcase_60 AC 184 ms
30,472 KB
testcase_61 AC 163 ms
32,560 KB
testcase_62 AC 155 ms
28,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using mint = atcoder::modint998244353;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> a(n);
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    for(auto &&v : a) cin >> v;

    mint ans;
    auto dfs = [&](auto dfs, int v, int p) -> int {
        mint sml, lrg;
        int res = 1;
        for(auto &&u : g[v]){
            if(u == p) continue;
            int sz = dfs(dfs, u, v);
            res += sz;
            if(a[u] < a[v]){
                ans += sml * sz;
                sml += sz;
            }else if(a[u] > a[v]){
                ans += lrg * sz;
                lrg += sz;
            }
        }
        if(p != -1){
            if(a[p] < a[v]){
                ans += sml * (n - res);
            }else if(a[p] > a[v]){
                ans += lrg * (n - res);
            }
        }
        return res;
    };
    dfs(dfs, 0, -1);
    cout << ans.val() << '\n';
}
0