結果

問題 No.2584 The University of Tree
ユーザー 👑 NachiaNachia
提出日時 2023-11-22 03:54:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 305 ms / 3,000 ms
コード長 3,364 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 92,600 KB
実行使用メモリ 44,056 KB
最終ジャッジ日時 2023-12-11 23:30:24
合計ジャッジ時間 10,819 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 135 ms
21,856 KB
testcase_23 AC 144 ms
28,504 KB
testcase_24 AC 90 ms
16,972 KB
testcase_25 AC 104 ms
20,712 KB
testcase_26 AC 59 ms
14,764 KB
testcase_27 AC 122 ms
22,916 KB
testcase_28 AC 62 ms
13,808 KB
testcase_29 AC 29 ms
9,088 KB
testcase_30 AC 278 ms
42,208 KB
testcase_31 AC 268 ms
38,452 KB
testcase_32 AC 48 ms
11,684 KB
testcase_33 AC 146 ms
38,580 KB
testcase_34 AC 47 ms
13,108 KB
testcase_35 AC 276 ms
39,296 KB
testcase_36 AC 266 ms
44,056 KB
testcase_37 AC 290 ms
39,296 KB
testcase_38 AC 305 ms
42,424 KB
testcase_39 AC 264 ms
42,412 KB
testcase_40 AC 289 ms
41,232 KB
testcase_41 AC 298 ms
40,180 KB
testcase_42 AC 285 ms
39,296 KB
testcase_43 AC 285 ms
42,424 KB
testcase_44 AC 276 ms
39,296 KB
testcase_45 AC 274 ms
39,296 KB
testcase_46 AC 151 ms
39,296 KB
testcase_47 AC 252 ms
44,056 KB
testcase_48 AC 98 ms
17,596 KB
testcase_49 AC 265 ms
36,584 KB
testcase_50 AC 219 ms
30,920 KB
testcase_51 AC 119 ms
19,164 KB
testcase_52 AC 47 ms
10,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0