結果

問題 No.2047 Path Factory
ユーザー umimelumimel
提出日時 2023-02-17 21:19:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,082 bytes
コンパイル時間 1,907 ms
コンパイル使用メモリ 180,000 KB
実行使用メモリ 22,016 KB
最終ジャッジ日時 2024-07-19 12:20:24
合計ジャッジ時間 7,475 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 RE -
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 31 ms
10,496 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 43 ms
12,544 KB
testcase_11 AC 24 ms
8,960 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 93 ms
8,960 KB
testcase_17 AC 91 ms
8,704 KB
testcase_18 AC 113 ms
9,984 KB
testcase_19 AC 134 ms
11,264 KB
testcase_20 AC 201 ms
14,976 KB
testcase_21 AC 108 ms
9,728 KB
testcase_22 AC 22 ms
5,376 KB
testcase_23 AC 31 ms
5,376 KB
testcase_24 AC 27 ms
5,376 KB
testcase_25 AC 187 ms
22,016 KB
testcase_26 AC 229 ms
19,968 KB
testcase_27 AC 147 ms
14,552 KB
testcase_28 AC 38 ms
14,828 KB
testcase_29 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void dfs(ll, ll)':
main.cpp:77:29: warning: 'a' may be used uninitialized [-Wmaybe-uninitialized]
   77 |         dp[u][0] = (mul*dp[a][2])%mod;
      |                             ^
main.cpp:73:12: note: 'a' was declared here
   73 |         ll a;
      |            ^

ソースコード

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

ll mod = MOD2;
vector<vector<ll>> T;
vector<vector<ll>> dp;
void dfs(ll u, ll p){
    for(ll v : T[u]) if(v != p) dfs(v, u);

    ll zero_cnt = 0;
    for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])==0) zero_cnt++;

    if(zero_cnt>=3){
        cout << 0 << endl;
        exit(0);
    }

    if(zero_cnt == 0){
        ll mul = 1;
        for(ll v : T[u]) if(v != p){
            mul = (mul * (dp[v][0]+dp[v][1]))%mod;
        }

        //dp[u][0]を計算
        ll sum0 = 0;
        for(ll v : T[u]) if(v != p){
            ll tmp = ((dp[v][0]+dp[v][2])%mod*pow_mod((dp[v][0]+dp[v][1])%mod, mod-2, mod))%mod;
            sum0 = (sum0 + tmp)%mod;
        }
        dp[u][0] = (mul*sum0)%mod;

        //dp[u][1]を計算
        ll sum1 = 0;
        ll lsum = 0;
        for(ll v : T[u]) if(v != p){
            ll tmp = ((dp[v][0]+dp[v][2])%mod*pow_mod((dp[v][0]+dp[v][1])%mod, mod-2, mod))%mod;
            sum1 = (sum1 + (tmp*lsum)%mod)%mod;
            lsum = (lsum + tmp)%mod;
        }
        dp[u][1] = (mul*sum1)%mod;

        //dp[u][2]を計算
        dp[u][2] = mul;
    }else if(zero_cnt == 1){
        ll mul = 1;
        for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])!= 0){
            mul = (mul * (dp[v][0]+dp[v][1]))%mod;
        }

        ll a;
        for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])==0) a=v;

        //dp[u][0]を計算
        dp[u][0] = (mul*dp[a][2])%mod;

        //dp[u][1]を計算
        for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])!=0){
            ll tmp = (dp[a][2]*(dp[v][0]+dp[v][2]))%mod;
            tmp = (tmp * pow_mod((dp[v][0]+dp[v][1])%mod, mod-2, mod))%mod;
            dp[u][1] = (dp[u][1]+tmp)%mod;
        }
        dp[u][1] = (dp[u][1]*mul)%mod;
    }else if(zero_cnt == 2){
        ll mul = 1;
        for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])!= 0){
            mul = (mul * (dp[v][0]+dp[v][1]))%mod;
        }

        ll a=-1, b=-1;
        for(ll v : T[u]) if(v != p) if((dp[v][0]+dp[v][1])==0){
            if(a==-1){
                a=b;
            }else{
                b=v;
            }
        }

        //dp[u][1]を計算
        dp[u][1] = ((dp[a][2]*dp[b][2])%mod * mul)%mod;
    }

    return;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    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);
    cout << (dp[0][0]+dp[0][1])%mod << endl;
}
0