結果
問題 | No.2047 Path Factory |
ユーザー | umimel |
提出日時 | 2022-08-20 15:26:20 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,542 bytes |
コンパイル時間 | 2,086 ms |
コンパイル使用メモリ | 179,772 KB |
実行使用メモリ | 26,624 KB |
最終ジャッジ日時 | 2024-10-09 08:07:48 |
合計ジャッジ時間 | 5,034 ms |
ジャッジサーバーID (参考情報) |
judge1 / 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 | RE | - |
testcase_07 | AC | 56 ms
10,368 KB |
testcase_08 | AC | 44 ms
8,960 KB |
testcase_09 | AC | 53 ms
10,112 KB |
testcase_10 | AC | 75 ms
12,672 KB |
testcase_11 | AC | 44 ms
8,960 KB |
testcase_12 | AC | 27 ms
6,816 KB |
testcase_13 | AC | 28 ms
7,040 KB |
testcase_14 | AC | 47 ms
9,216 KB |
testcase_15 | AC | 57 ms
10,496 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | AC | 143 ms
26,624 KB |
testcase_26 | WA | - |
testcase_27 | AC | 146 ms
14,916 KB |
testcase_28 | AC | 65 ms
14,932 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; vector<ll> sum((ll)T[u].size(), 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; if(now >= 3) 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; }