結果

問題 No.2584 The University of Tree
ユーザー umimelumimel
提出日時 2023-12-12 14:44:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 3,000 ms
コード長 5,015 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 186,356 KB
実行使用メモリ 56,524 KB
最終ジャッジ日時 2023-12-12 14:44:46
合計ジャッジ時間 16,008 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 189 ms
29,952 KB
testcase_23 AC 281 ms
15,980 KB
testcase_24 AC 119 ms
11,136 KB
testcase_25 AC 137 ms
12,160 KB
testcase_26 AC 109 ms
10,904 KB
testcase_27 AC 201 ms
18,600 KB
testcase_28 AC 98 ms
14,336 KB
testcase_29 AC 46 ms
8,832 KB
testcase_30 AC 393 ms
30,208 KB
testcase_31 AC 389 ms
40,992 KB
testcase_32 AC 73 ms
15,104 KB
testcase_33 AC 257 ms
55,520 KB
testcase_34 AC 117 ms
8,236 KB
testcase_35 AC 412 ms
40,012 KB
testcase_36 AC 439 ms
23,748 KB
testcase_37 AC 393 ms
23,628 KB
testcase_38 AC 341 ms
23,116 KB
testcase_39 AC 437 ms
28,880 KB
testcase_40 AC 446 ms
40,100 KB
testcase_41 AC 418 ms
35,512 KB
testcase_42 AC 412 ms
38,604 KB
testcase_43 AC 355 ms
33,740 KB
testcase_44 AC 437 ms
46,796 KB
testcase_45 AC 418 ms
46,796 KB
testcase_46 AC 261 ms
56,524 KB
testcase_47 AC 413 ms
23,656 KB
testcase_48 AC 142 ms
11,136 KB
testcase_49 AC 320 ms
21,392 KB
testcase_50 AC 274 ms
18,364 KB
testcase_51 AC 135 ms
12,032 KB
testcase_52 AC 58 ms
7,424 KB
権限があれば一括ダウンロードができます

ソースコード

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
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();
}
0