結果

問題 No.2360 Path to Integer
ユーザー KudeKude
提出日時 2023-06-23 22:14:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,500 ms
コード長 3,420 bytes
コンパイル時間 2,699 ms
コンパイル使用メモリ 234,380 KB
実行使用メモリ 34,112 KB
最終ジャッジ日時 2023-09-13 17:15:27
合計ジャッジ時間 4,493 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 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 2 ms
4,376 KB
testcase_08 AC 12 ms
5,424 KB
testcase_09 AC 149 ms
26,788 KB
testcase_10 AC 127 ms
28,364 KB
testcase_11 AC 130 ms
28,312 KB
testcase_12 AC 96 ms
27,380 KB
testcase_13 AC 157 ms
27,012 KB
testcase_14 AC 153 ms
32,544 KB
testcase_15 AC 158 ms
26,808 KB
testcase_16 AC 139 ms
34,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

template<class S, class E>
struct Rerooting {
    const vector<vector<E>>& g;
    const int n;
    const int root;
    vector<vector<S>> dp, sl, sr;

    Rerooting(const vector<vector<E>>& g, int root=0): g(g), n(g.size()), root(root), dp(n), sl(n), sr(n) {
        dfs1(root);
        dfs2(root);
    }

    S dfs1(int u, int p=-1) {
        const int sz = g[u].size();
        dp[u].resize(sz);
        sl[u].resize(sz + 1);
        sr[u].resize(sz + 1);

        S res;
        for(int i = 0; i < sz; i++) {
            const E& e = g[u][i];
            int v = dest(e);
            if (v == p) continue;
            dp[u][i] = dfs1(v, u).apply(e);
            res = res.merge(dp[u][i]);
        }
        return res;
    }

    void dfs2(int u, int p=-1) {
        const int sz = g[u].size();

        {
            S s;
            for(int i = 0; i < sz; i++) {
                s = s.merge(dp[u][i]);
                sl[u][i + 1] = s;
            }
        }
        {
            S s;
            for(int i = sz - 1; i >= 0; i--) {
                s = dp[u][i].merge(s);
                sr[u][i] = s;
            }
        }

        for(int i = 0; i < sz; i++) {
            int v = dest(g[u][i]);
            if (v == p) continue;
            const int sz_v = g[v].size();
            for(int j = 0; j < sz_v; j++) {
                const E& e = g[v][j];
                int w = dest(e);
                if (w != u) continue;
                dp[v][j] = sl[u][i].merge(sr[u][i + 1]).apply(e);
                break;
            }
            dfs2(v, u);
        }
    }

    S get_acc(int v) { return sr[v][0]; }
    S get_res(int v, E e) { return sr[v][0].apply(e); }

    private:
    int dest(const E& e) {
        if constexpr (is_same<E, int>::value) return e;
        else return e.to;
    };
};

mint p10[20];
vector<mint> a;
VI l;

struct S {
  mint sm;
  int cnt = 0;
  S apply(int v) const {
    int c = cnt + 1;
    mint s = sm * p10[l[v]] + a[v] * c;
    return {s, c};
  }
  S merge(const S& rhs) const {
    return S{sm + rhs.sm, cnt + rhs.cnt};
  }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  p10[0] = 1;
  rep(i, 19) p10[i+1] = p10[i] * 10;
  int n;
  cin >> n;
  a.resize(n);
  l.resize(n);
  rep(i, n) {
    static string s;
    cin >> s;
    l[i] = s.size();
    mint x;
    for(char c: s) x = mint::raw(10) * x + mint::raw(c - '0');
    a[i] = x;
  }
  VVI to(n);
  rep(_, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  Rerooting<S, int> rt(to);
  mint ans;
  rep(i, n) {
    ans += rt.get_res(i, i).sm;
  }
  cout << ans.val() << '\n';
}
0