結果
問題 | No.2584 The University of Tree |
ユーザー |
👑 ![]() |
提出日時 | 2023-11-22 03:54:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 249 ms / 3,000 ms |
コード長 | 3,364 bytes |
コンパイル時間 | 968 ms |
コンパイル使用メモリ | 89,856 KB |
最終ジャッジ日時 | 2025-02-17 22:50:37 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 53 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <utility> #include <cassert> #include <atcoder/modint> using namespace std; using Modint = atcoder::modint998244353; struct MidData{ int root; Modint reach; Modint pass; }; // - - // | | // -x->-y- MidData rake(MidData x, MidData y){ MidData res; res.root = x.root; res.pass = x.pass * y.pass; res.reach = x.pass * y.reach + x.reach; return res; } MidData compress(MidData x, int newRoot){ MidData res; res.root = newRoot + 1; res.pass = Modint(newRoot + 1); res.reach = Modint(newRoot + 1) * x.reach; return res; } MidData newTree(int newRoot, bool base){ MidData res; if(base){ res.root = 1; res.pass = Modint(0); res.reach = Modint(1); } else { res.root = newRoot + 1; res.pass = Modint(1); res.reach = Modint(0); } return res; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> edges(N*2); vector<int> xoredges(N-1); for(int i=0; i<N; i++){ edges[i].push_back(i+N); edges[i+N].push_back(i); } for(int i=0; i<N; i++){ int c; cin >> c; for(int j=0; j<c; j++){ int e; cin >> e; e--; xoredges[e] ^= i; edges[i].push_back(e); } } for(int i=0; i<N; i++){ for(int j=1; j<(int)edges[i].size(); j++){ edges[i][j] = i ^ xoredges[edges[i][j]]; } } vector<int> parent(N*2, -1); vector<int> bfs = {0}; for(int i=0; i<N*2; i++){ int v = bfs[i]; if(i != 0){ int q = 0; while(edges[v][q] != parent[v]) q++; if(q != (int)edges[v].size() - 1){ rotate(edges[v].begin(), edges[v].begin() + q + 1, edges[v].end()); } } for(int w : edges[v]) if(parent[v] != w){ parent[w] = v; bfs.push_back(w); } } vector<MidData> low(N*2); for(int i=N*2-1; i>=0; i--){ int v = bfs[i]; low[v] = newTree(v, v>=N); int c = edges[v].size(); if(i != 0) c--; for(int j=0; j<c; j++){ int w = edges[v][j]; low[v] = rake(low[v], compress(low[w], v)); } } vector<MidData> high(N*2); for(int i=0; i<N*2; i++){ int v = bfs[i]; int c = edges[v].size(); if(i != 0) c--; if(c == 0) continue; vector<MidData> highL(c); vector<MidData> highR(c); highL[0] = newTree(v, v>=N); if(i != 0){ int w = parent[v]; highL[0] = rake(highL[0], compress(high[v], v)); } for(int t=0; t<c-1; t++){ int w = edges[v][t]; highL[t+1] = rake(highL[t], compress(low[w], v)); } highR[c-1] = newTree(v, v>=N); for(int t=c-1; t>=1; t--){ int w = edges[v][t]; highR[t-1] = rake(compress(low[w], v), highR[t]); } for(int t=0; t<c; t++){ int w = edges[v][t]; high[w] = rake(highR[t], highL[t]); } } vector<Modint> ans(N); for(int i=0; i<N; i++) ans[i] = high[N+i].reach; for(int i=0; i<N; i++){ cout << ans[i].val() << '\n'; } return 0; }