結果

問題 No.2360 Path to Integer
ユーザー milanis48663220milanis48663220
提出日時 2023-07-14 01:42:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,500 ms
コード長 3,501 bytes
コンパイル時間 1,292 ms
コンパイル使用メモリ 132,492 KB
実行使用メモリ 29,880 KB
最終ジャッジ日時 2023-10-13 13:33:40
合計ジャッジ時間 4,070 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 8 ms
4,636 KB
testcase_09 AC 82 ms
17,540 KB
testcase_10 AC 58 ms
18,628 KB
testcase_11 AC 61 ms
18,604 KB
testcase_12 AC 52 ms
17,488 KB
testcase_13 AC 79 ms
17,512 KB
testcase_14 AC 85 ms
27,608 KB
testcase_15 AC 86 ms
17,536 KB
testcase_16 AC 93 ms
29,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<string> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<vector<int>> g(n);
    for(int i = 0; i < n-1; i++){
        int u, v; cin >> u >> v; u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<vector<mint>> dp(n);
    vector<int> subtree_size(n);
    for(int i = 0; i < n; i++) dp[i].resize(g[i].size());
    function<mint(int, int)> dfs1 = [&](int v, int p){
        subtree_size[v] = 1;
        mint pw = mint(10).pow(a[v].size());
        mint ans = pw;
        for(int i = 0; i < g[v].size(); i++){
            int u = g[v][i];
            if(u == p) continue;
            dp[v][i] = dfs1(u, v);
            ans += dp[v][i]*pw;
            subtree_size[v] += subtree_size[u];
        }
        return ans;
    };
    dfs1(0, -1);
    function<void(int, int, mint)> dfs2 = [&](int v, int p, mint from_par){
        int deg = g[v].size();
        for(int i = 0; i < deg; i++){
            if(g[v][i] == p){
                dp[v][i] = from_par;
                break;
            }
        }
        mint pw = mint(10).pow(a[v].size());
        vector<mint> from_right(deg+1, 0);
        for(int i = deg-1; i >= 0; i--){
            from_right[i] = from_right[i+1] + dp[v][i];
        }
        mint from_left = 0;
        for(int i = 0; i < deg; i++){
            int u = g[v][i];
            if(u != p){
                mint x = (from_left+from_right[i+1]+1)*pw;
                dfs2(u, v, x);
            }
            from_left = from_left + dp[v][i];
        }
    };
    dfs2(0, -1, mint(0));
    mint ans = 0;
    for(int v = 0; v < n; v++){
        mint x = stoll(a[v]);
        mint sum = 0;
        for(int i = 0; i < g[v].size(); i++){
            int to = g[v][i];
            if(subtree_size[to] > subtree_size[v]){
                sum += dp[v][i]*x*subtree_size[v];
            }else{
                sum += dp[v][i]*x*(n-subtree_size[to]);
            }
        }
        sum += x*n;
        ans += sum;
        // cout << v << ':' << sum << endl;
    }
    cout << ans << endl;
}
0