結果

問題 No.2584 The University of Tree
ユーザー fdironiafdironia
提出日時 2024-01-24 08:24:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 597 ms / 3,000 ms
コード長 2,220 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 93,336 KB
実行使用メモリ 58,172 KB
最終ジャッジ日時 2024-01-24 08:25:19
合計ジャッジ時間 17,165 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 3 ms
6,548 KB
testcase_20 AC 2 ms
6,548 KB
testcase_21 AC 2 ms
6,548 KB
testcase_22 AC 310 ms
30,848 KB
testcase_23 AC 301 ms
20,208 KB
testcase_24 AC 184 ms
12,928 KB
testcase_25 AC 214 ms
14,336 KB
testcase_26 AC 143 ms
12,544 KB
testcase_27 AC 283 ms
21,120 KB
testcase_28 AC 142 ms
15,232 KB
testcase_29 AC 79 ms
9,600 KB
testcase_30 AC 524 ms
35,636 KB
testcase_31 AC 558 ms
43,824 KB
testcase_32 AC 116 ms
15,616 KB
testcase_33 AC 483 ms
57,116 KB
testcase_34 AC 121 ms
9,856 KB
testcase_35 AC 575 ms
43,196 KB
testcase_36 AC 510 ms
30,680 KB
testcase_37 AC 546 ms
28,504 KB
testcase_38 AC 501 ms
27,980 KB
testcase_39 AC 538 ms
34,564 KB
testcase_40 AC 528 ms
44,364 KB
testcase_41 AC 551 ms
39,700 KB
testcase_42 AC 544 ms
43,352 KB
testcase_43 AC 550 ms
39,500 KB
testcase_44 AC 557 ms
49,340 KB
testcase_45 AC 597 ms
49,340 KB
testcase_46 AC 490 ms
58,172 KB
testcase_47 AC 502 ms
30,680 KB
testcase_48 AC 212 ms
12,928 KB
testcase_49 AC 475 ms
25,704 KB
testcase_50 AC 382 ms
21,888 KB
testcase_51 AC 230 ms
14,080 KB
testcase_52 AC 97 ms
8,448 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void dfs1(vp*, int, int, vi*, int*)':
main.cpp:51:19: warning: '*pos[1]' may be used uninitialized [-Wmaybe-uninitialized]
   51 |     ans[u] = pos[1];
      |              ~~~~~^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:50,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
                 from main.cpp:1:
In function 'constexpr typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int*; _Tp = int]',
    inlined from 'constexpr void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = int*; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:968:21,
    inlined from 'constexpr void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int*; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:998:20,
    inlined from 'int main()' at main.cpp:68:9:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:922:18: warning: 'void* __builtin_memset(void*, int, long unsigned int)' specified bound between 18446744065119617024 and 18446744073709551612 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
  922 |         *__first = __tmp;
      |         ~~~~~~~~~^~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

constexpr int Z = 998244353;

using pii = pair<int, int>;
using vp = vector<pii>;
using vi = vector<int>;

void dfs0(vp e[], int u, int p, int pord, vi dp[]) {
    dp[u].resize(e[u].size());
    dp[u][0] = 1;

    for (int i = 1; i < (int)e[u].size(); i++) {
        int v = e[u][i].second;

        if (v == p) {
            e[p][pord].first = i;
            e[u][i].first = pord;
        } else {
            dfs0(e, v, u, i, dp);
            int ord = e[u][i].first, dv = e[v].size();
            for (int j = 0; j < (int)e[v].size() - 1; j++) {
                dp[u][i] = 1ll * (dp[u][i] + dp[v][(ord-1-j+dv)%dv]) * (v + 1) % Z;
            }
        }
    }
}

void dfs1(vp e[], int u, int p, vi dp[], int ans[]) {
    int pre[e[u].size()];
    int pu = 1;
    for (int i = 0; i < (int)e[u].size(); i++) {
        pre[i] = ((i == 0 ? 0 : pre[i-1]) + 1ll * pu * dp[u][i]) % Z;
        pu = 1ll * pu * (u + 1) % Z;
    }
    pu = 1;
    for (int i = (int)e[u].size() - 1; i >= 0; i--) {
        pre[i] = 1ll * pre[i] * pu % Z;
        pu = 1ll * pu * (u + 1) % Z;
    }

    int pos[e[u].size()+1];
    for (int i = (int)e[u].size(); i >= 0; i--) {
        pos[i] = i == (int)e[u].size() ? 0 : 1ll * (pos[i+1] + dp[u][i]) * (u + 1) % Z;
    }

    ans[u] = pos[1];

    for (int i = (int)e[u].size() - 1; i >= 1; i--) {
        auto [ord, v] = e[u][i];

        if (v != p) {
            dp[v][ord] = (pos[i+1] + pre[i-1]) % Z;
            dfs1(e, v, u, dp, ans);
        }
    }
}

int main() {
    int n;
    cin >> n;
    vp e[n];
    int e2[n-1];
    fill(e2, e2 + n - 1, 0);
    for (int i = 0; i < n; i++) {
        int d;
        cin >> d;
        e[i].resize(d + 1);
        for (int j = 1; j <= d; j++) {
            cin >> e[i][j].first;
            e2[--e[i][j].first] ^= i;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (int)e[i].size(); j++) {
            e[i][j].second = e2[e[i][j].first] ^ i;
        }
    }

    vi dp[n];
    dfs0(e, 0, -1, -1, dp);

    int ans[n];
    dfs1(e, 0, -1, dp, ans);

    for (int i = 0; i < n; i++) {
        cout << ans[i] << endl;
    }

    return 0;
}
0