結果

問題 No.2598 Kadomatsu on Tree
ユーザー t98slidert98slider
提出日時 2024-01-05 15:26:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 3,290 bytes
コンパイル時間 2,675 ms
コンパイル使用メモリ 206,764 KB
実行使用メモリ 32,560 KB
最終ジャッジ日時 2024-01-05 15:26:59
合計ジャッジ時間 11,202 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 5 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 3 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 141 ms
14,732 KB
testcase_24 AC 141 ms
14,948 KB
testcase_25 AC 134 ms
14,956 KB
testcase_26 AC 140 ms
14,752 KB
testcase_27 AC 169 ms
14,892 KB
testcase_28 AC 138 ms
14,948 KB
testcase_29 AC 140 ms
15,100 KB
testcase_30 AC 131 ms
14,740 KB
testcase_31 AC 135 ms
14,964 KB
testcase_32 AC 134 ms
14,920 KB
testcase_33 AC 148 ms
15,136 KB
testcase_34 AC 142 ms
14,700 KB
testcase_35 AC 164 ms
15,168 KB
testcase_36 AC 138 ms
15,152 KB
testcase_37 AC 140 ms
15,140 KB
testcase_38 AC 143 ms
15,172 KB
testcase_39 AC 143 ms
15,172 KB
testcase_40 AC 146 ms
15,172 KB
testcase_41 AC 155 ms
15,172 KB
testcase_42 AC 167 ms
15,172 KB
testcase_43 AC 142 ms
15,172 KB
testcase_44 AC 134 ms
15,172 KB
testcase_45 AC 146 ms
15,172 KB
testcase_46 AC 145 ms
15,172 KB
testcase_47 AC 145 ms
15,172 KB
testcase_48 AC 169 ms
31,920 KB
testcase_49 AC 12 ms
6,676 KB
testcase_50 AC 101 ms
21,248 KB
testcase_51 AC 30 ms
9,728 KB
testcase_52 AC 38 ms
10,752 KB
testcase_53 AC 40 ms
8,528 KB
testcase_54 AC 34 ms
7,760 KB
testcase_55 AC 14 ms
6,676 KB
testcase_56 AC 73 ms
11,204 KB
testcase_57 AC 23 ms
6,676 KB
testcase_58 AC 151 ms
14,904 KB
testcase_59 AC 133 ms
14,964 KB
testcase_60 AC 165 ms
30,472 KB
testcase_61 AC 170 ms
32,560 KB
testcase_62 AC 172 ms
28,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<const unsigned int MOD> struct prime_modint {
    using mint = prime_modint;
    unsigned int v;
    prime_modint() : v(0) {}
    prime_modint(unsigned int a) { a %= MOD; v = a; }
    prime_modint(unsigned long long a) { a %= MOD; v = a; }
    prime_modint(int a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    prime_modint(long long a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    static constexpr int mod() { return MOD; }
    mint& operator++() {v++; if(v == MOD)v = 0; return *this;}
    mint& operator--() {if(v == 0)v = MOD; v--; return *this;}
    mint operator++(int) { mint result = *this; ++*this; return result; }
    mint operator--(int) { mint result = *this; --*this; return result; }
    mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; }
    mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; }
    mint& operator*=(const mint& rhs) {
        v = (unsigned int)((unsigned long long)(v) * rhs.v % MOD);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    mint pow(long long n) const {
        assert(0 <= n);
        mint r = 1, x = *this;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const { assert(v); return pow(MOD - 2); }
    friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; }
    friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; }
    friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; }
    friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; }
    friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); }
    friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); }
    friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; }
};
//using mint = prime_modint<1000000007>;
using mint = prime_modint<998244353>;

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 << '\n';
}
0