結果

問題 No.2047 Path Factory
ユーザー umimelumimel
提出日時 2023-02-18 06:42:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 3,082 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 178,648 KB
実行使用メモリ 22,016 KB
最終ジャッジ日時 2024-07-19 19:37:21
合計ジャッジ時間 5,003 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
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 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 27 ms
10,496 KB
testcase_08 AC 21 ms
8,960 KB
testcase_09 AC 24 ms
10,112 KB
testcase_10 AC 34 ms
12,672 KB
testcase_11 AC 21 ms
8,832 KB
testcase_12 AC 13 ms
6,784 KB
testcase_13 AC 13 ms
6,912 KB
testcase_14 AC 22 ms
9,216 KB
testcase_15 AC 26 ms
10,368 KB
testcase_16 AC 83 ms
8,832 KB
testcase_17 AC 80 ms
8,576 KB
testcase_18 AC 95 ms
9,856 KB
testcase_19 AC 120 ms
11,264 KB
testcase_20 AC 169 ms
15,104 KB
testcase_21 AC 94 ms
9,728 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 28 ms
5,376 KB
testcase_24 AC 24 ms
5,376 KB
testcase_25 AC 172 ms
22,016 KB
testcase_26 AC 193 ms
20,096 KB
testcase_27 AC 125 ms
14,428 KB
testcase_28 AC 34 ms
14,952 KB
testcase_29 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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=v;
            }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