Submission #61233227


Source Code Expand

// answer = size[0] * (-n-1)^[# other components - 1]

#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;

int n, p[100005];
pair<int, int> dp[100005][2];
vector<int> G[100005];

int power(int x, int t) {
    int ret = 1;
    while(t > 0) {
        if(t & 1) ret = 1LL * ret * x % MOD;
        x = 1LL * x * x % MOD;
        t >>= 1;
    }
    return ret;
}

pair<int, int> operator+(const pair<int, int>& A, const pair<int, int>& B) {
    return make_pair((A.first + B.first) % MOD, (A.second + B.second) % MOD);
}
pair<int, int> operator*(const pair<int, int>& A, const pair<int, int>& B) {
    return make_pair(1LL * A.first * B.first % MOD, (1LL * A.first * B.second + 1LL * A.second * B.first) % MOD);
}

int main() {
    scanf("%d", &n);
    for(int i = 2; i <= n; i++) scanf("%d", &p[i]);

    for(int i = 2; i <= n; i++) G[p[i]].push_back(i);
    for(int i = n; i >= 1; i--) {
        dp[i][0] = make_pair(1, 1);
        dp[i][1] = make_pair(1, 0);
        for(auto ni : G[i]) {
            dp[i][0] = dp[i][0] * (dp[ni][0] * make_pair(n * 2, 0) + dp[ni][1] * make_pair(MOD - n - 1, 0));
            dp[i][1] = dp[i][1] * (dp[ni][0] * make_pair(n * 2 - 1, 0) + dp[ni][1] * make_pair(MOD - n, 0));
        }
    }
    pair<int, int> ans = dp[1][0] * make_pair(n * 2 + 1, 0) + dp[1][1] * make_pair(MOD - n - 1, 0);
    printf("%lld\n", 1LL * (ans.first + ans.second) * power(n + 1, MOD - 2) % MOD);
    return 0;
}

Submission Info

Submission Time
Task B - Odd Namori
User tour1st
Language C++ 20 (gcc 12.2)
Score 1000
Code Size 1485 Byte
Status AC
Exec Time 19 ms
Memory 11128 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:29:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:30:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |     for(int i = 2; i <= n; i++) scanf("%d", &p[i]);
      |                                 ~~~~~^~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 4
AC × 28
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_n_small_00.txt, 01_n_small_01.txt, 01_n_small_02.txt, 01_n_small_03.txt, 01_n_small_04.txt, 01_n_small_05.txt, 01_n_small_06.txt, 01_n_small_07.txt, 01_n_small_08.txt, 01_n_small_09.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 03_path_00.txt, 03_path_01.txt, 04_star_00.txt, 04_star_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3692 KiB
00_sample_01.txt AC 1 ms 3900 KiB
00_sample_02.txt AC 1 ms 3796 KiB
00_sample_03.txt AC 1 ms 3676 KiB
01_n_small_00.txt AC 1 ms 3812 KiB
01_n_small_01.txt AC 1 ms 3812 KiB
01_n_small_02.txt AC 1 ms 3852 KiB
01_n_small_03.txt AC 1 ms 3728 KiB
01_n_small_04.txt AC 1 ms 3904 KiB
01_n_small_05.txt AC 1 ms 3860 KiB
01_n_small_06.txt AC 1 ms 3856 KiB
01_n_small_07.txt AC 1 ms 3928 KiB
01_n_small_08.txt AC 1 ms 3672 KiB
01_n_small_09.txt AC 1 ms 3924 KiB
02_random_00.txt AC 15 ms 8640 KiB
02_random_01.txt AC 18 ms 9740 KiB
02_random_02.txt AC 4 ms 4972 KiB
02_random_03.txt AC 19 ms 9556 KiB
02_random_04.txt AC 5 ms 5192 KiB
02_random_05.txt AC 19 ms 9800 KiB
02_random_06.txt AC 3 ms 4320 KiB
02_random_07.txt AC 19 ms 9676 KiB
02_random_08.txt AC 6 ms 5636 KiB
02_random_09.txt AC 19 ms 9692 KiB
03_path_00.txt AC 7 ms 6916 KiB
03_path_01.txt AC 14 ms 11128 KiB
04_star_00.txt AC 3 ms 4420 KiB
04_star_01.txt AC 8 ms 5920 KiB