結果
問題 | No.2584 The University of Tree |
ユーザー | umimel |
提出日時 | 2023-12-12 14:44:29 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 392 ms / 3,000 ms |
コード長 | 5,015 bytes |
コンパイル時間 | 2,100 ms |
コンパイル使用メモリ | 186,520 KB |
実行使用メモリ | 56,320 KB |
最終ジャッジ日時 | 2024-09-27 04:56:58 |
合計ジャッジ時間 | 13,012 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 53 |
ソースコード
#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 mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template<long long mod> class modint{ long long x; public: modint(long long x=0) : x((x%mod+mod)%mod) {} modint operator-() const { return modint(-x); } bool operator==(const modint& a){ if(x == a) return true; else return false; } bool operator==(long long a){ if(x == a) return true; else return false; } bool operator!=(const modint& a){ if(x != a) return true; else return false; } bool operator!=(long long a){ if(x != a) return true; else return false; } modint& operator+=(const modint& a) { if ((x += a.x) >= mod) x -= mod; return *this; } modint& operator-=(const modint& a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } modint& operator*=(const modint& a) { (x *= a.x) %= mod; return *this; } modint operator+(const modint& a) const { modint res(*this); return res+=a; } modint operator-(const modint& a) const { modint res(*this); return res-=a; } modint operator*(const modint& a) const { modint res(*this); return res*=a; } modint pow(long long t) const { if (!t) return 1; modint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod modint inv() const { return pow(mod-2); } modint& operator/=(const modint& a) { return (*this) *= a.inv(); } modint operator/(const modint& a) const { modint res(*this); return res/=a; } friend std::istream& operator>>(std::istream& is, modint& m) noexcept { is >> m.x; m.x %= mod; if (m.x < 0) m.x += mod; return is; } friend ostream& operator<<(ostream& os, const modint& m){ os << m.x; return os; } }; using mint = modint<MOD998244353>; void solve(){ ll N; cin >> N; vector<vector<ll>> T(N); vector<pll> E(N-1, {-1, -1}); for(ll u=0; u<N; u++){ ll c; cin >> c; while(c--){ ll e_id; cin >> e_id; e_id--; T[u].pb(e_id); if(E[e_id].fi==-1) E[e_id].fi = u; else E[e_id].se = u; } } for(ll v=0; v<N; v++){ ll deg = (ll)T[v].size(); for(ll i=0; i<deg; i++){ ll e_id = T[v][i]; if(E[e_id].fi==v) T[v][i] = E[e_id].se; else T[v][i] = E[e_id].fi; } } vector<mint> dp1(N, mint(0)); function<void(ll, ll)> dfs1 = [&](ll v, ll p){ ll deg = (ll)T[v].size(); ll pidx = deg; for(ll i=0; i<deg; i++){ if(p==T[v][i]) pidx = i; else dfs1(T[v][i], v); } for(ll i=0; i<deg; i++) if(p!=T[v][i]){ dp1[v] += dp1[T[v][i]]*mint(v+1).pow((i-pidx+(deg+1))%(deg+1)); } if(v>0) dp1[v] += mint(v+1).pow(deg-pidx); }; dfs1(0, -1); vector<mint> ans(N, mint(0)); vector<mint> dp2(N, mint(0)); dp2[0] = 1; function<void(ll, ll)> dfs2 = [&](ll v, ll p){ ll deg = (ll)T[v].size(); // calc ans[v] for(ll i=0; i<deg; i++){ if(p==T[v][i]) ans[v] += dp2[v]*mint(v+1).pow(i+1); else ans[v] += dp1[T[v][i]]*mint(v+1).pow(i+1); } // calc dp2[ch] ll pidx = deg; for(ll i=0; i<deg; i++) if(p==T[v][i]) pidx = i; mint sum = dp2[v]*mint(v+1).pow(deg+1); for(ll i=(pidx+1)%(deg+1); i!=pidx; i=(i+1)%(deg+1)){ if(i==deg){ sum += mint(v+1).pow((i-pidx+deg+1)%(deg+1)); }else{ sum += dp1[T[v][i]]*mint(v+1).pow((i-pidx+deg+1)%(deg+1)); } } //cout << v << " : " << sum << endl; for(ll i=(pidx+1)%(deg+1); i!=pidx; i=(i+1)%(deg+1)){ if(i==deg){ sum /= mint(v+1); sum -= mint(1); sum += mint(v+1).pow(deg+1); }else{ sum /= mint(v+1); sum -= dp1[T[v][i]]; dp2[T[v][i]] = sum; sum += dp1[T[v][i]]*mint(v+1).pow(deg+1); dfs2(T[v][i], v); } } }; dfs2(0, -1); for(ll v=0; v<N; v++) cout << ans[v] << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }