結果

問題 No.2360 Path to Integer
ユーザー au7777au7777
提出日時 2023-07-06 00:38:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,500 ms
コード長 5,263 bytes
コンパイル時間 3,763 ms
コンパイル使用メモリ 247,132 KB
実行使用メモリ 32,436 KB
最終ジャッジ日時 2024-07-19 18:53:41
合計ジャッジ時間 5,771 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 14 ms
5,376 KB
testcase_09 AC 160 ms
22,784 KB
testcase_10 AC 126 ms
24,112 KB
testcase_11 AC 131 ms
23,988 KB
testcase_12 AC 125 ms
22,828 KB
testcase_13 AC 160 ms
22,912 KB
testcase_14 AC 147 ms
30,644 KB
testcase_15 AC 148 ms
22,784 KB
testcase_16 AC 139 ms
32,436 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void dfs2(int, int, std::vector<std::__cxx11::basic_string<char> >&, std::vector<std::vector<int> >&, std::vector<std::vector<atcoder::static_modint<998244353> > >&, mint)':
main.cpp:121:23: warning: 'parIdx' may be used uninitialized [-Wmaybe-uninitialized]
  121 |         dp[cur][parIdx] = 1 + psum;
      |                       ^
main.cpp:113:13: note: 'parIdx' was declared here
  113 |         int parIdx;
      |             ^~~~~~
main.cpp: In function 'int dfs3(int, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
main.cpp:153:24: warning: 'parIdx' may be used uninitialized [-Wmaybe-uninitialized]
  153 |         num[cur][parIdx] = edge.size() - ret;
      |                        ^
main.cpp:141:9: note: 'parIdx' was declared here
  141 |     int parIdx;
      |         ^~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
typedef long long int ll;
using namespace std;
typedef pair<ll, ll> P;
using namespace atcoder;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define USE998244353
#ifdef USE998244353
const ll MOD = 998244353;
// const double PI = 3.141592653589;
using mint = modint998244353;
#else
const ll MOD = 1000000007;
using mint = modint1000000007;
#endif
const int MAX = 20000001;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll x, ll y) {
   if (y == 0) return x;
   else if (y > x) {
       return gcd (y, x); 
   }
   else return gcd(x % y, y);
}
ll lcm(ll x, ll y) {
   return x / gcd(x, y) * y;
}
ll my_sqrt(ll x) {
    // 
    ll m = 0;
    ll M = 3000000001;
    while (M - m > 1) {
        ll now = (M + m) / 2;
        if (now * now <= x) {
            m = now;
        }
        else {
            M = now;
        }
    }
    return m;
}
ll keta(ll num, ll arity) {
    ll ret = 0;
    while (num) {
        num /= arity;
        ret++;
    }
    return ret;
}
ll ceil(ll n, ll m) {
    // n > 0, m > 0
    ll ret = n / m;
    if (n % m) ret++;
    return ret;
}
ll pow_ll(ll x, ll n) {
    if (n == 0) return 1;
    if (n % 2) {
        return pow_ll(x, n - 1) * x;
    }
    else {
        ll tmp = pow_ll(x, n / 2);
        return tmp * tmp;
    }
}
vector<ll> compress(vector<ll>& v) {
    // [3 5 5 6 1 1 10 1] -> [1 2 2 3 0 0 4 0] 
    vector<ll> u = v;
    sort(u.begin(), u.end());
    u.erase(unique(u.begin(),u.end()),u.end());
    map<ll, ll> mp;
    for (int i = 0; i < u.size(); i++) {
        mp[u[i]] = i;
    }
    for (int i = 0; i < v.size(); i++) {
        v[i] = mp[v[i]];
    }
    return v;
}

mint dfs1(int cur, int par, vector<string>& a, vector<vector<int>>& edge, vector<vector<mint>>& dp) {
    mint ret = 1;
    for (int i = 0; i < edge[cur].size(); i++) {
        int nex = edge[cur][i];
        if (nex == par) continue;
        mint tmp = dfs1(nex, cur, a, edge, dp);
        dp[cur][i] = tmp;
        ret += tmp;
    }
    ret *= pow_mod(10, a[cur].size(), MOD);
    return ret;
}

void dfs2(int cur, int par, vector<string>& a, vector<vector<int>>& edge, vector<vector<mint>>& dp, mint psum) {
    if (par != -1) {
        int parIdx;
        for (int i = 0; i < edge[cur].size(); i++) {
            int nex = edge[cur][i];
            if (nex == par) {
                parIdx = i;
                break;
            }
        }
        dp[cur][parIdx] = 1 + psum;
        dp[cur][parIdx] *= pow_mod(10, a[par].size(), MOD);
    }

    mint sm = 0;
    for (int i = 0; i < edge[cur].size(); i++) {
        sm += dp[cur][i];
    }
    
    for (int i = 0; i < edge[cur].size(); i++) {
        int nex = edge[cur][i];
        if (nex == par) {
            continue;
        }
        dfs2(nex, cur, a, edge, dp, sm - dp[cur][i]);
    }
}

int dfs3(int cur, int par, vector<vector<int>>& edge, vector<vector<int>>& num) {
    int ret = 1;
    int parIdx;
    for (int i = 0; i < edge[cur].size(); i++) {
        int nex = edge[cur][i];
        if (nex == par) {
            parIdx = i;
            continue;
        }
        int tmp = dfs3(nex, cur, edge, num);
        num[cur][i] = tmp;
        ret += tmp;
    }
    if (par != -1) {
        num[cur][parIdx] = edge.size() - ret;
    }
    return ret;
}

int main() {
    int n;
    cin >> n;
    vector<string> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector<vector<int>> edge(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    vector<vector<mint>> dp(n);
    for (int i = 0; i < n; i++) {
        dp[i].resize(edge[i].size());
    }

    dfs1(0, -1, a, edge, dp);
    dfs2(0, -1, a, edge, dp, 0);

    vector<vector<int>> num(n);
    for (int i = 0; i < n; i++) {
        num[i].resize(edge[i].size());
    }

    dfs3(0, -1, edge, num);

    // dp[cur][i] ... 頂点curからi番目の辺を使った時に、全部でどれくらい掛け算されるか
    // num[cur][i]... 頂点curからi番目の辺を使った時の部分木の頂点の数


    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < edge[i].size(); j++) {
    //         cout << i + 1 << ' ' << edge[i][j] + 1 << ' ' << num[i][j] << '\n';
    //     }
    // }

    // mint 
    mint ans = 0;
    for (int i = 0; i < n; i++) {
        mint nu = 0; 
        for (auto c : a[i]) {
            nu *= 10;
            nu += (c - '0');
        }
        for (int j = 0; j < edge[i].size(); j++) {
            // cerr << i + 1 << ' ' << edge[i][j] + 1 << ' ' << dp[i][j].val() << ' ' << num[i][j] << '\n';
            ans += (dp[i][j]) * nu * (n - num[i][j]);
        }
        ans += n * nu;
    }
    cout << ans.val() << '\n';
    return 0;
}
0