結果

問題 No.2584 The University of Tree
ユーザー fdironiafdironia
提出日時 2024-01-24 08:24:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 641 ms / 3,000 ms
コード長 2,220 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 94,132 KB
実行使用メモリ 58,200 KB
最終ジャッジ日時 2024-09-28 07:01:44
合計ジャッジ時間 17,051 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 288 ms
30,848 KB
testcase_23 AC 323 ms
20,168 KB
testcase_24 AC 194 ms
12,928 KB
testcase_25 AC 224 ms
14,336 KB
testcase_26 AC 148 ms
12,416 KB
testcase_27 AC 274 ms
20,988 KB
testcase_28 AC 143 ms
15,224 KB
testcase_29 AC 81 ms
9,600 KB
testcase_30 AC 579 ms
35,592 KB
testcase_31 AC 611 ms
43,852 KB
testcase_32 AC 122 ms
15,616 KB
testcase_33 AC 484 ms
57,068 KB
testcase_34 AC 113 ms
9,728 KB
testcase_35 AC 619 ms
43,212 KB
testcase_36 AC 539 ms
30,500 KB
testcase_37 AC 541 ms
28,432 KB
testcase_38 AC 542 ms
27,904 KB
testcase_39 AC 567 ms
34,520 KB
testcase_40 AC 582 ms
44,236 KB
testcase_41 AC 588 ms
39,532 KB
testcase_42 AC 595 ms
43,392 KB
testcase_43 AC 588 ms
39,476 KB
testcase_44 AC 611 ms
49,348 KB
testcase_45 AC 641 ms
49,144 KB
testcase_46 AC 478 ms
58,200 KB
testcase_47 AC 540 ms
30,592 KB
testcase_48 AC 204 ms
12,928 KB
testcase_49 AC 512 ms
25,804 KB
testcase_50 AC 413 ms
21,856 KB
testcase_51 AC 224 ms
14,080 KB
testcase_52 AC 98 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