結果

問題 No.2047 Path Factory
ユーザー au7777au7777
提出日時 2023-09-21 02:09:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 6,080 bytes
コンパイル時間 4,679 ms
コンパイル使用メモリ 259,284 KB
実行使用メモリ 58,752 KB
最終ジャッジ日時 2024-07-06 20:53:36
合計ジャッジ時間 9,616 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 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 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 192 ms
14,592 KB
testcase_08 AC 148 ms
12,288 KB
testcase_09 AC 174 ms
13,952 KB
testcase_10 AC 256 ms
18,048 KB
testcase_11 AC 145 ms
12,288 KB
testcase_12 AC 84 ms
8,832 KB
testcase_13 AC 86 ms
9,088 KB
testcase_14 AC 150 ms
12,672 KB
testcase_15 AC 185 ms
14,720 KB
testcase_16 AC 175 ms
14,848 KB
testcase_17 AC 168 ms
14,336 KB
testcase_18 AC 206 ms
17,920 KB
testcase_19 AC 249 ms
21,120 KB
testcase_20 AC 382 ms
29,568 KB
testcase_21 AC 201 ms
16,768 KB
testcase_22 AC 41 ms
6,796 KB
testcase_23 AC 54 ms
7,552 KB
testcase_24 AC 45 ms
6,656 KB
testcase_25 AC 398 ms
58,752 KB
testcase_26 AC 438 ms
49,408 KB
testcase_27 AC 325 ms
23,360 KB
testcase_28 AC 172 ms
23,964 KB
testcase_29 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'mint g(std::vector<atcoder::static_modint<998244353> >, std::vector<atcoder::static_modint<998244353> >)':
main.cpp:135:13: warning: 'id' may be used uninitialized [-Wmaybe-uninitialized]
  135 |             if (i == id) {
      |             ^~
main.cpp:123:9: note: 'id' was declared here
  123 |     int id;
      |         ^~

ソースコード

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.1415926535897932;
using mint = modint998244353;
#else
const ll MOD = 1000000007;
using mint = modint1000000007;
#endif
const int MAX = 2000001;
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;
}

vector<ll> Eratosthenes( const ll N )
{
    vector<bool> is_prime( N + 1 );
    for( ll i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;
    }
    vector<ll> P;
    for( ll i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( ll j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            P.emplace_back( i );
        }
    }
    return P;
}

// return a[0]b[1]b[2]...b[n - 1] + b[0]a[1]b[2]...b[n - 1]
mint g(vector<mint> a, vector<mint> b) {
    int n = b.size();
    int id;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (b[i].val() == 0) {
            id = i;
            cnt++;
        }
    }
    if (cnt >= 2) return 0;
    if (cnt == 1) {
        mint ret = 1;
        for (int i = 0; i < n; i++) {
            if (i == id) {
                ret *= a[i];
            }
            else {
                ret *= b[i];
            }
        }
        return ret;
    }
    mint product_b = 1;
    mint ratio_sum = 0;
    for (int i = 0; i < n; i++) {
        product_b *= b[i];
        ratio_sum += a[i] * b[i].inv();
    }
    return product_b * ratio_sum;
}

mint sub_f(vector<mint> c) {
    mint sum = 0;
    mint sumsq = 0;
    for (auto x : c) {
        sum += x;
        sumsq += x * x;
    }
    mint two = 2;
    return (sum * sum - sumsq) * two.inv();
}

// return a[0]a[1]b[2]b[3]...b[n - 1] + a[0]b[1]a[2]...b[n - 1]
mint f(vector<mint> a, vector<mint> b) {
    int n = b.size();
    int id1, id2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (b[i].val() == 0) {
            if (cnt == 0) {
                id1 = i;
            }
            else {
                id2 = i;
            }
            cnt++;
        }
    }
    if (cnt >= 3) return 0;
    if (cnt == 2) {
        mint ret = 1;
        for (int i = 0; i < n; i++) {
            if ((i == id1) || (i == id2)) {
                ret *= a[i];
            }
            else {
                ret *= b[i];
            }
        }
        return ret;
    }
    if (cnt == 1) {
        vector<mint> new_a;
        vector<mint> new_b;
        for (int i = 0; i < n; i++) {
            if (i == id1) {
                ;
            }
            else {
                new_a.push_back(a[i]);
                new_b.push_back(b[i]);
            }
        }
        return g(new_a, new_b) * a[id1];
    }
    mint product_b = 1;
    vector<mint> c;
    for (int i = 0; i < n; i++) {
        product_b *= b[i];
        c.push_back(a[i] * b[i].inv());
    }
    mint ratio_dual_sum = sub_f(c);
    return product_b * ratio_dual_sum;

}

// return b[0]b[1]b[2]b[3]...b[n - 1]
mint h(vector<mint> a, vector<mint> b) {
    mint ret = 1;
    for (auto x : b) ret *= x;
    return ret;
}

void test() {
    vector<mint> a1 = {2, 3, 4};
    vector<mint> b1 = {0, 2, 4};
    cout << f(a1, b1).val() << '\n';
    cout << g(a1, b1).val() << '\n';
}

map<tuple<int, int, bool>, mint> memo;

mint solve(int cur, int par, bool have_par, vector<vector<int>>& edge) {
    if (memo.find({cur, par, have_par}) != memo.end()) {
        return memo[{cur, par, have_par}];
    }
    vector<mint> a;
    vector<mint> b;
    for (auto nex : edge[cur]) {
        if (nex == par) continue;
        a.push_back(solve(nex, cur, true, edge));
        b.push_back(solve(nex, cur, false, edge));
    }
    // 子の数が2以上だから成り立つこと
    if (have_par) {
        memo[{cur, par, have_par}] = g(a, b) + h(a, b);
        return g(a, b) + h(a, b);
    }
    else {
        memo[{cur, par, have_par}] = f(a, b) + g(a, b);
        return f(a, b) + g(a, b);
    }
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> edge(n);
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    cout << solve(0, -1, false, edge).val() << '\n';
    return 0;
}
0