結果

問題 No.2360 Path to Integer
ユーザー au7777au7777
提出日時 2023-07-06 00:32:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,391 bytes
コンパイル時間 4,408 ms
コンパイル使用メモリ 246,948 KB
実行使用メモリ 23,472 KB
最終ジャッジ日時 2024-07-19 18:47:04
合計ジャッジ時間 9,298 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 15 ms
5,376 KB
testcase_09 AC 170 ms
22,784 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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> > >&)':
main.cpp:121:23: warning: 'parIdx' may be used uninitialized [-Wmaybe-uninitialized]
  121 |         dp[cur][parIdx] = 1;
      |                       ^
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) {
    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;
        for (int j = 0; j < edge[par].size(); j++) {
            if (edge[par][j] == cur) continue;
            // cerr << cur + 1 << ' ' << par + 1 << ' ' << edge[par][j] + 1 << ' ' << dp[par][j].val() << '\n';
            dp[cur][parIdx] += dp[par][j];
        }
        dp[cur][parIdx] *= pow_mod(10, a[par].size(), MOD);
    }
    
    for (int i = 0; i < edge[cur].size(); i++) {
        int nex = edge[cur][i];
        if (nex == par) {
            continue;
        }
        dfs2(nex, cur, a, edge, dp);
    }
}

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);

    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