結果

問題 No.2047 Path Factory
ユーザー umimelumimel
提出日時 2022-08-20 15:46:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,666 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 177,024 KB
実行使用メモリ 26,732 KB
最終ジャッジ日時 2024-04-23 13:33:06
合計ジャッジ時間 3,971 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 49 ms
10,496 KB
testcase_08 AC 38 ms
8,960 KB
testcase_09 AC 45 ms
9,984 KB
testcase_10 AC 63 ms
12,800 KB
testcase_11 AC 39 ms
8,960 KB
testcase_12 AC 24 ms
6,944 KB
testcase_13 AC 24 ms
6,944 KB
testcase_14 AC 42 ms
9,344 KB
testcase_15 AC 49 ms
10,624 KB
testcase_16 AC 47 ms
9,216 KB
testcase_17 AC 45 ms
9,088 KB
testcase_18 AC 53 ms
10,624 KB
testcase_19 AC 68 ms
12,416 KB
testcase_20 AC 99 ms
16,384 KB
testcase_21 AC 54 ms
10,240 KB
testcase_22 AC 12 ms
6,944 KB
testcase_23 AC 16 ms
6,940 KB
testcase_24 AC 14 ms
6,944 KB
testcase_25 AC 95 ms
26,732 KB
testcase_26 AC 110 ms
23,596 KB
testcase_27 AC 123 ms
14,908 KB
testcase_28 AC 60 ms
14,928 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

ll pow_mod(ll x, ll n, ll mod){
    ll ret = 1;
    while(n > 0){
        if(n & 1) ret = (ret*x)%mod;
        x = x*x%mod;
        n >>=1;
    }
    return ret;
}

vector<vector<ll>> T;
vector<vector<ll>> dp;
void dfs(ll u, ll p){
    if((ll)T[u].size()==1 && u != 0){
        dp[u][2] = 1;
        return;
    }

    ll l_cnt = 0;
    for(ll v : T[u]){
        if(v == p) continue;
        dfs(v, u);
        if((ll)T[v].size() == 1) l_cnt++;
    }

    if(l_cnt >= 3){
        cout << 0 << endl;
        exit(0);
    }else if(l_cnt == 2){
        dp[u][0] = 1;
        for(ll v : T[u]){
            if(v == p) continue;
            if((ll)T[v].size()==1) continue;

            dp[u][0]*=(dp[v][0]+dp[v][1])%MOD2;
            dp[u][0]%=MOD2;
        }
    }else if(l_cnt == 1){
        ll all_cut = 1;
        for(ll v : T[u]){
            if(v == p) continue;
            if((ll)T[v].size()==1) continue;

            all_cut*=(dp[v][0]+dp[v][1])%MOD2;
            all_cut%=MOD2;
        }

        dp[u][1] = all_cut;

        ll cut1 = 0;
        for(ll v : T[u]){
            if(v == p) continue;
            if((ll)T[v].size()==1) continue;

            ll tmp = pow_mod((dp[v][0]+dp[v][1])%MOD2, MOD2-2, MOD2);
            tmp*=all_cut;
            tmp%=MOD2;
            tmp*=(dp[v][1]+dp[v][2])%MOD2;
            cut1+=tmp%MOD2;
            cut1%=MOD2;
        }

        dp[u][0]=cut1;
    }else{
        ll all_cut = 1;
        for(ll v : T[u]){
            if(v == p) continue;
            all_cut*=(dp[v][0]+dp[v][1])%MOD2;
            all_cut%=MOD2;
        }

        dp[u][2] = all_cut;

        ll cut1 = 0;
        for(ll v : T[u]){
            if(v == p) continue;
            ll tmp = pow_mod((dp[v][0]+dp[v][1])%MOD2, MOD2-2, MOD2);
            tmp*=all_cut;
            tmp%=MOD2;
            tmp*=(dp[v][1]+dp[v][2])%MOD2;
            cut1+=tmp%MOD2;
            cut1%=MOD2;
        }

        dp[u][1]=cut1;

        ll cut2 = 0;
        ll child = 0;
        for(ll v : T[u]){
            if(v == p) continue;
            child++;
        }

        if(child == 1) return;

        vector<ll> sum(child+1, 0);
        ll now = 1;
        for(ll v : T[u]){
            if(v == p) continue;
            sum[now] = pow_mod((dp[v][0]+dp[v][1])%MOD2, MOD2-2, MOD2);
            sum[now] *= (dp[v][1]+dp[v][2])%MOD2;
            sum[now] += sum[now-1];
            sum[now] %= MOD2;
            now++;
        }

        now = 1;
        for(ll v : T[u]){
            if(v == p) continue;
            ll sum2 = (sum.back() - sum[now] + MOD2)%MOD2;
            now++;

            sum2 *= (dp[v][1]+dp[v][2])%MOD2;
            sum2%=MOD2;
            sum2 *= pow_mod((dp[v][0]+dp[v][1])%MOD2, MOD2-2, MOD2);
            sum2 %= MOD2;

            cut2+=sum2;
            cut2%=MOD2;
        }

        cut2 *= all_cut;
        cut2 %= MOD2;

        dp[u][0] = cut2;
    }

    return;
}

int main(){
    ll n;
    cin >> n;

    T.resize(n);
    dp.resize(n, vector<ll>(3, 0));
    rep(i, n-1){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        T[u].pb(v);
        T[v].pb(u);
    }

    dfs(0, -1);

    //rep(i, n) cout << i+1 << " : " << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl;

    cout << (dp[0][0]+dp[0][1])%MOD2 << endl;
}
0