Submission #2622193


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

int N, fac[2018], vol[2018], dp[2018][2018][2][3], old[2018][2][3];
const int mod = 1e9 + 7;
vector < int > v[2018];

int add (int x, int y) {int ans = x + y; if (ans >= mod) ans -= mod; return ans;}
int subtract (int x, int y) {if (x >= y) return x - y; return x - y + mod;}
int mul (int x, int y) {return 1LL * x * y % mod;}
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

/*
dp[i][j][parity][0/1/2]

parity of the number of edges chosen to be part of a path
[0/1/2] the number of edges adjacent to i (the root) already chosen
j is the number of different paths (including 1-length ones)?
multiplied by 2^the number of paths of non-zero length that are present already
*/
const int inv2 = (mod + 1) / 2;
void dfs (int nod)
{
    vol[nod] = 1, dp[nod][1][0][0] = 1;
    for (auto it : v[nod])
    {
        dfs (it);
        for (int i=0; i<=vol[nod]; i++)
            for (int j=0; j<2; j++)
                for (int k=0; k<3; k++)
                    old[i][j][k] = dp[nod][i][j][k], dp[nod][i][j][k] = 0;
        for (int i=0; i<=vol[nod]; i++)
            for (int p1=0; p1<2; p1++)
                for (int k1=0; k1<3; k1++)
                    if (old[i][p1][k1])
                        for (int j=0; j<=vol[it]; j++)
                            for (int p2=0; p2<2; p2++)
                                for (int k2=0; k2<3; k2++)
                                    if (dp[it][j][p2][k2])
                                    {
                                        int curr = mul (old[i][p1][k1], dp[it][j][p2][k2]);
                                        for (int e=0; e<2 && e + k2 < 3 && e + k1 < 3; e++)
                                        {
                                            ///e=0/1 whether I chose or not the edge between it and nod
                                            int newSize = i + j - e;
                                            if (k1 > 0 && k2 > 0 && e == 1)
                                                curr = mul (curr, inv2);
                                            if (k1 == 0 && k2 == 0 && e == 1)
                                                curr = add (curr, curr);
                                            adto (dp[nod][newSize][p1 ^ p2 ^ e][e + k1], curr);
                                        }
                                    }
        vol[nod] += vol[it];
    }
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d", &N);
for (int i=2; i<=N; i++)
{
    int t;
    scanf ("%d", &t);
    v[t].push_back (i);
}
dfs (1);
fac[0] = 1;
for (int i=1; i<=N; i++)
    fac[i] = mul (fac[i - 1], i);
int ans = 0;
for (int p=0; p<2; p++)
{
    int curr = 0;
    for (int i=1; i<=N; i++)
        for (int j=0; j<3; j++)
            adto (curr, mul (dp[1][i][p][j], fac[i]));
    if (p == 0) adto (ans, curr);
    else ans = subtract (ans, curr);
}
printf ("%d\n", ans);
return 0;
}

Submission Info

Submission Time
Task A - Awkward
User geniucos
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 3043 Byte
Status AC
Exec Time 228 ms
Memory 94848 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:63:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d", &N);
                 ^
./Main.cpp:67:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d", &t);
                     ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 4
AC × 55
Set Name Test Cases
Sample a01, a02, a03, a04
All a01, a02, a03, a04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 384 KB
a04 AC 1 ms 384 KB
b05 AC 1 ms 256 KB
b06 AC 49 ms 92800 KB
b07 AC 92 ms 94848 KB
b08 AC 124 ms 92928 KB
b09 AC 99 ms 92928 KB
b10 AC 79 ms 92928 KB
b11 AC 41 ms 92800 KB
b12 AC 25 ms 92800 KB
b13 AC 29 ms 92800 KB
b14 AC 49 ms 92800 KB
b15 AC 124 ms 92928 KB
b16 AC 20 ms 47616 KB
b17 AC 2 ms 4608 KB
b18 AC 1 ms 384 KB
b19 AC 142 ms 92928 KB
b20 AC 100 ms 92928 KB
b21 AC 82 ms 93056 KB
b22 AC 126 ms 93056 KB
b23 AC 113 ms 92800 KB
b24 AC 77 ms 92928 KB
b25 AC 58 ms 93440 KB
b26 AC 49 ms 92800 KB
b27 AC 64 ms 93696 KB
b28 AC 66 ms 92800 KB
b29 AC 92 ms 94464 KB
b30 AC 92 ms 94464 KB
b31 AC 99 ms 94336 KB
b32 AC 92 ms 94848 KB
b33 AC 156 ms 93056 KB
b34 AC 228 ms 93056 KB
b35 AC 220 ms 93184 KB
b36 AC 162 ms 92928 KB
b37 AC 154 ms 92928 KB
b38 AC 157 ms 93056 KB
b39 AC 175 ms 93056 KB
b40 AC 171 ms 92928 KB
b41 AC 147 ms 92928 KB
b42 AC 158 ms 92928 KB
b43 AC 157 ms 92928 KB
b44 AC 143 ms 93184 KB
b45 AC 91 ms 94720 KB
b46 AC 91 ms 94848 KB
b47 AC 92 ms 94848 KB
b48 AC 92 ms 94848 KB
b49 AC 92 ms 94848 KB
b50 AC 77 ms 92800 KB
b51 AC 58 ms 92800 KB
b52 AC 53 ms 92800 KB
b53 AC 51 ms 92800 KB
b54 AC 50 ms 92800 KB
b55 AC 96 ms 93696 KB