結果

問題 No.2584 The University of Tree
ユーザー KudeKude
提出日時 2023-12-12 19:53:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 535 ms / 3,000 ms
コード長 3,436 bytes
コンパイル時間 3,766 ms
コンパイル使用メモリ 283,140 KB
実行使用メモリ 91,696 KB
最終ジャッジ日時 2023-12-12 19:53:34
合計ジャッジ時間 19,158 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 3 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 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 230 ms
48,020 KB
testcase_23 AC 260 ms
41,624 KB
testcase_24 AC 158 ms
26,752 KB
testcase_25 AC 181 ms
30,592 KB
testcase_26 AC 107 ms
22,764 KB
testcase_27 AC 218 ms
39,024 KB
testcase_28 AC 114 ms
25,088 KB
testcase_29 AC 68 ms
14,976 KB
testcase_30 AC 482 ms
70,084 KB
testcase_31 AC 456 ms
77,792 KB
testcase_32 AC 91 ms
23,296 KB
testcase_33 AC 197 ms
89,904 KB
testcase_34 AC 108 ms
18,044 KB
testcase_35 AC 466 ms
78,128 KB
testcase_36 AC 397 ms
65,096 KB
testcase_37 AC 483 ms
65,100 KB
testcase_38 AC 434 ms
64,576 KB
testcase_39 AC 470 ms
69,200 KB
testcase_40 AC 469 ms
78,416 KB
testcase_41 AC 512 ms
74,516 KB
testcase_42 AC 499 ms
77,260 KB
testcase_43 AC 488 ms
73,280 KB
testcase_44 AC 535 ms
83,760 KB
testcase_45 AC 514 ms
83,760 KB
testcase_46 AC 217 ms
91,696 KB
testcase_47 AC 400 ms
65,096 KB
testcase_48 AC 166 ms
27,136 KB
testcase_49 AC 403 ms
58,948 KB
testcase_50 AC 360 ms
49,448 KB
testcase_51 AC 178 ms
29,824 KB
testcase_52 AC 73 ms
15,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

template<class S, class E>
struct Rerooting {
    const vector<vector<E>>& g;
    const int n;
    const int root;
    vector<vector<S>> dp, sl, sr;

    Rerooting(const vector<vector<E>>& g, int root=0): g(g), n(g.size()), root(root), dp(n), sl(n), sr(n) {
        dfs1(root);
        dfs2(root);
    }

    S dfs1(int u, int p=-1) {
        const int sz = g[u].size();
        dp[u].resize(sz);
        sl[u].resize(sz + 1);
        sr[u].resize(sz + 1);

        S res[2];
        int f = 0;
        for(int i = 0; i < sz; i++) {
            const E& e = g[u][i];
            int v = dest(e);
            if (v == p) {
              f++;
              continue;
            }
            dp[u][i] = dfs1(v, u).apply(e);
            res[f] = res[f].merge(dp[u][i]);
        }
        return res[0].merge(res[1]);
    }

    void dfs2(int u, int p=-1) {
        const int sz = g[u].size();

        {
            S s;
            for(int i = 0; i < sz; i++) {
                s = s.merge(dp[u][i]);
                sl[u][i + 1] = s;
            }
        }
        {
            S s;
            for(int i = sz - 1; i >= 0; i--) {
                s = dp[u][i].merge(s);
                sr[u][i] = s;
            }
        }

        for(int i = 0; i < sz; i++) {
            int v = dest(g[u][i]);
            if (v == p) continue;
            const int sz_v = g[v].size();
            for(int j = 0; j < sz_v; j++) {
                const E& e = g[v][j];
                int w = dest(e);
                if (w != u) continue;
                dp[v][j] = sl[u][i].merge(sr[u][i + 1]).apply(e);
                break;
            }
            dfs2(v, u);
        }
    }

    S get_acc(int v) { return sr[v][0]; }
    S get_res(int v, E e) { return sr[v][0].apply(e); }

    private:
    int dest(const E& e) {
        if constexpr (is_same<E, int>::value) return e;
        else return e.to;
    };
};

struct E {
  int from, to;
};

struct S {
  mint vl, vr, kl = 1, kr = 1;
  S apply(E e) const {
    e.from++, e.to++;
    return S{((vl + 1) * e.to * kr + vr) * e.from, 0, e.from, 1};
  }
  S merge(const S& rhs) const {
    return S{vl + vr * kl, rhs.vl + rhs.vr * rhs.kl, kl * kr, rhs.kl * rhs.kr};
  }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<E>> g(n);
  VI edge(n - 1);
  rep(u, n) {
    int c;
    cin >> c;
    rep(_, c) {
      int e;
      cin >> e;
      e--;
      edge[e] ^= u;
      g[u].push_back({u, e});
    }
  }
  rep(u, n) for (auto& [from, to] : g[u]) to = u ^ edge[to];
  Rerooting<S, E> rt(g);
  rep(u, n) {
    mint ans = S{}.merge(rt.get_acc(u)).vr;
    cout << ans.val() << '\n';
  }
}
0