Submission #2762708


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

const int Mod = 1e9 + 7;
const int Nmax = 2005;
typedef long long ll;

int n, i, j, x;
vector<int> v[Nmax];
int dp[Nmax][Nmax][3], fact[Nmax], D[Nmax][3], w[Nmax];

void add_to(int &x, int y) { x+=y; if(x>=Mod) x-=Mod; }
int inm(int x, int y) { return (ll)x*y%Mod; }

void Merge(int node, int son)
{
    int i, j, dg, dgr;
    for(i=0; i <= w[node] + w[son]; ++i)
        for(j=0; j<3; ++j)
            D[i][j] = 0;

    for(i=0; i<=w[node]; ++i)
        for(j=0; j<=w[son]; ++j)
            for(dg=0; dg<3; ++dg)
                for(dgr=0; dgr<3; ++dgr)
                    if(dp[node][i][dg] && dp[son][j][dgr])
                    {
                        add_to( D[i+j][dg], inm(dp[node][i][dg], dp[son][j][dgr] * (dgr ? 2 : 1)) );

                        if(dg < 2 && dgr < 2)
                            add_to( D[i+j-1][dg+1], inm(dp[node][i][dg], dp[son][j][dgr]) );
                    }

    w[node] += w[son];
    for(i=0; i<=w[node]; ++i)
        for(j=0; j<3; ++j)
            dp[node][i][j] = D[i][j];
}

void solve(int node)
{
    w[node] = 1; dp[node][1][0] = 1;
    for(auto son : v[node])
    {
        solve(son);
        Merge(node, son);
    }
}

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

    cin >> n;
    for(i=2; i<=n; ++i)
    {
        cin >> x;
        v[x].push_back(i);
    }

    fact[0] = 1;
    for(i=1; i<=n; ++i) fact[i] = inm(fact[i-1], i);

    solve(1);

    int ans = 0;
    for(i=0; i<=n; ++i)
        for(j=0; j<3; ++j)
            if( (n-i) % 2 == 0 ) add_to(ans, inm( dp[1][i][j] * (j ? 2 : 1), fact[i] ) );
                else add_to(ans, Mod - inm( dp[1][i][j] * (j ? 2 : 1), fact[i] ) );

    cout << ans << '\n';
    return 0;
}

Submission Info

Submission Time
Task A - Awkward
User Alexa2001
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 1881 Byte
Status AC
Exec Time 165 ms
Memory 47488 KB

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 384 KB
a02 AC 1 ms 384 KB
a03 AC 1 ms 384 KB
a04 AC 1 ms 384 KB
b05 AC 1 ms 384 KB
b06 AC 80 ms 45824 KB
b07 AC 114 ms 47488 KB
b08 AC 99 ms 45952 KB
b09 AC 82 ms 45824 KB
b10 AC 70 ms 45824 KB
b11 AC 49 ms 45824 KB
b12 AC 45 ms 45824 KB
b13 AC 53 ms 45824 KB
b14 AC 80 ms 45824 KB
b15 AC 98 ms 45952 KB
b16 AC 18 ms 23296 KB
b17 AC 2 ms 2688 KB
b18 AC 1 ms 384 KB
b19 AC 110 ms 45952 KB
b20 AC 83 ms 45824 KB
b21 AC 71 ms 45824 KB
b22 AC 100 ms 45952 KB
b23 AC 89 ms 45824 KB
b24 AC 68 ms 45824 KB
b25 AC 61 ms 47232 KB
b26 AC 80 ms 45824 KB
b27 AC 72 ms 47488 KB
b28 AC 83 ms 45824 KB
b29 AC 105 ms 47488 KB
b30 AC 105 ms 47488 KB
b31 AC 100 ms 47488 KB
b32 AC 112 ms 47488 KB
b33 AC 120 ms 45952 KB
b34 AC 165 ms 45952 KB
b35 AC 163 ms 45952 KB
b36 AC 122 ms 45952 KB
b37 AC 117 ms 45952 KB
b38 AC 121 ms 45952 KB
b39 AC 128 ms 45952 KB
b40 AC 128 ms 45952 KB
b41 AC 112 ms 45952 KB
b42 AC 119 ms 45952 KB
b43 AC 118 ms 45824 KB
b44 AC 110 ms 45952 KB
b45 AC 110 ms 47360 KB
b46 AC 113 ms 47360 KB
b47 AC 114 ms 47488 KB
b48 AC 114 ms 47488 KB
b49 AC 115 ms 47488 KB
b50 AC 84 ms 45824 KB
b51 AC 81 ms 45824 KB
b52 AC 80 ms 45824 KB
b53 AC 80 ms 45824 KB
b54 AC 80 ms 45824 KB
b55 AC 98 ms 46720 KB