結果

問題 No.2360 Path to Integer
ユーザー Kude
提出日時 2023-06-23 22:14:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 210 ms / 2,500 ms
コード長 3,420 bytes
コンパイル時間 2,515 ms
コンパイル使用メモリ 225,996 KB
最終ジャッジ日時 2025-02-15 01:08:19
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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';
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0