結果
問題 | No.2047 Path Factory |
ユーザー | umimel |
提出日時 | 2022-08-20 15:46:30 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,666 bytes |
コンパイル時間 | 1,782 ms |
コンパイル使用メモリ | 179,796 KB |
実行使用メモリ | 26,624 KB |
最終ジャッジ日時 | 2024-10-15 13:33:08 |
合計ジャッジ時間 | 4,141 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 52 ms
10,496 KB |
testcase_08 | AC | 41 ms
8,960 KB |
testcase_09 | AC | 48 ms
9,984 KB |
testcase_10 | AC | 70 ms
12,544 KB |
testcase_11 | AC | 42 ms
8,960 KB |
testcase_12 | AC | 24 ms
6,656 KB |
testcase_13 | AC | 26 ms
6,912 KB |
testcase_14 | AC | 41 ms
9,216 KB |
testcase_15 | AC | 51 ms
10,496 KB |
testcase_16 | AC | 50 ms
9,088 KB |
testcase_17 | AC | 47 ms
9,088 KB |
testcase_18 | AC | 56 ms
10,624 KB |
testcase_19 | AC | 70 ms
12,160 KB |
testcase_20 | AC | 111 ms
16,384 KB |
testcase_21 | AC | 60 ms
10,240 KB |
testcase_22 | AC | 12 ms
5,248 KB |
testcase_23 | AC | 16 ms
5,248 KB |
testcase_24 | AC | 15 ms
5,248 KB |
testcase_25 | AC | 104 ms
26,624 KB |
testcase_26 | AC | 147 ms
23,552 KB |
testcase_27 | AC | 131 ms
14,912 KB |
testcase_28 | AC | 62 ms
14,948 KB |
testcase_29 | WA | - |
ソースコード
#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; }