結果

問題 No.2047 Path Factory
ユーザー umimelumimel
提出日時 2023-02-18 06:42:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 3,082 bytes
コンパイル時間 2,256 ms
コンパイル使用メモリ 178,296 KB
実行使用メモリ 21,848 KB
最終ジャッジ日時 2023-09-27 02:04:55
合計ジャッジ時間 7,181 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 36 ms
10,228 KB
testcase_08 AC 27 ms
8,840 KB
testcase_09 AC 32 ms
9,856 KB
testcase_10 AC 48 ms
12,660 KB
testcase_11 AC 27 ms
8,860 KB
testcase_12 AC 16 ms
6,484 KB
testcase_13 AC 17 ms
6,692 KB
testcase_14 AC 29 ms
9,168 KB
testcase_15 AC 37 ms
10,676 KB
testcase_16 AC 100 ms
8,600 KB
testcase_17 AC 98 ms
8,636 KB
testcase_18 AC 122 ms
9,852 KB
testcase_19 AC 145 ms
11,464 KB
testcase_20 AC 215 ms
15,076 KB
testcase_21 AC 119 ms
9,704 KB
testcase_22 AC 24 ms
4,768 KB
testcase_23 AC 32 ms
4,964 KB
testcase_24 AC 28 ms
4,564 KB
testcase_25 AC 190 ms
21,848 KB
testcase_26 AC 251 ms
19,908 KB
testcase_27 AC 169 ms
14,384 KB
testcase_28 AC 40 ms
14,956 KB
testcase_29 AC 3 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘void dfs(ll, ll)’ 内:
main.cpp:77:29: 警告: ‘a’ may be used uninitialized [-Wmaybe-uninitialized]
   77 |         dp[u][0] = (mul*dp[a][2])%mod;
      |                             ^
main.cpp:73:12: 備考: ‘a’ はここで定義されています
   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