結果

問題 No.2047 Path Factory
ユーザー 👑 potato167potato167
提出日時 2022-08-19 21:57:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,223 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 204,340 KB
実行使用メモリ 17,076 KB
最終ジャッジ日時 2024-04-23 13:29:06
合計ジャッジ時間 4,684 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 71 ms
11,904 KB
testcase_08 AC 55 ms
10,112 KB
testcase_09 AC 67 ms
11,136 KB
testcase_10 AC 104 ms
14,200 KB
testcase_11 AC 53 ms
9,856 KB
testcase_12 AC 31 ms
7,296 KB
testcase_13 AC 31 ms
7,552 KB
testcase_14 AC 57 ms
10,240 KB
testcase_15 AC 69 ms
11,776 KB
testcase_16 AC 46 ms
9,216 KB
testcase_17 AC 48 ms
8,960 KB
testcase_18 AC 55 ms
10,112 KB
testcase_19 AC 64 ms
11,392 KB
testcase_20 AC 108 ms
15,012 KB
testcase_21 AC 55 ms
10,240 KB
testcase_22 AC 11 ms
6,944 KB
testcase_23 AC 16 ms
6,940 KB
testcase_24 AC 12 ms
6,940 KB
testcase_25 AC 83 ms
16,560 KB
testcase_26 AC 128 ms
16,488 KB
testcase_27 AC 134 ms
17,012 KB
testcase_28 AC 69 ms
17,076 KB
testcase_29 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)

void solve();
// oddloop
int main() {
    int t=1;
    //cin>>t;
    rep(i,t) solve();
}

void solve(){
    int N;
    cin>>N;
    vector<vector<int>> G(N);
    rep(i,N-1){
        int a,b;
        cin>>a>>b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<int> pare(N,-1),order={0};
    pare[0]=-2;
    for(int i=0;i<N;i++){
        int a=order[i];
        for(auto x:G[a]){
            if(pare[x]==-1){
                pare[x]=a;
                order.push_back(x);
            }
        }
    }

    vector<vector<ll>> dp(N,vector<ll>(4));
    for(int i=N-1;i>=0;i--){
        int a=order[i];
        dp[a][1]=1;
        //ll X=1,Y=1,Z=1,W=0;
        for(auto x:G[a]){
            if(pare[x]!=a) continue;
            ll tmp1=(dp[x][1]+dp[x][2])%mod;
			ll tmp2=(dp[x][0]+dp[x][3])%mod;
			dp[a][3]=((dp[a][3]*tmp2)%mod+(dp[a][0]*tmp1)%mod)%mod;
			dp[a][0]=((dp[a][0]*tmp2)%mod+(dp[a][1]*tmp1)%mod)%mod;
			dp[a][1]=(dp[a][1]*tmp2)%mod;
        }
        dp[a][2]=dp[a][0];
        //vec_out(dp[a]);
    }
    cout<<(dp[0][0]+dp[0][3])%mod<<"\n";
}

0