Submission #2617626


Source Code Expand

#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

#define int ll
const int mod = 1e9+7;
const int N = 2007;
const int inv2 = (mod+1)/2;

int n;

VI G[N];

int sub[N];
int dp[2][N][N][3][2]; //[korzen][ile skladowych][stopien korzenia][parzystosc liczby krawedzi]
int c[N];

void dfs(int v)
{
	sub[v] = 1;

	dp[c[v]][v][1][0][0] = 1;

	for(auto u:G[v])
	{
		dfs(u);

		for(int degv = 2; degv >= 0; --degv)
		{
			for(int sv = sub[v]; sv >= 1; --sv)
			{
				for(int pv = 0; pv < 2; ++pv)
				{
					for(int degu = 2; degu >= 0; --degu)
					{
						for(int su = sub[u]; su >= 1; --su)
						{
							for(int pu = 0; pu < 2; ++pu)
							{
								if(degv<=1 && degu<=1) //uzywam krawedzi
								{
									int ndeg = degv+1;
									int np = (pv+pu+1)%2;
									int ns = sv+su-1;
									int mult = 1;

									if(degv==0 && degu==0)
										mult *= 2;
									else if(degv==1 && degu==1)
										mult *= inv2;

									dp[c[v]^1][v][ns][ndeg][np] += mult*dp[c[v]][v][sv][degv][pv]%mod*dp[c[u]][u][su][degu][pu]%mod;
									dp[c[v]^1][v][ns][ndeg][np] %= mod;
								}

								//nie uzywam krawedzi

								int ndeg = degv;
								int np = (pv+pu)%2;
								int ns = sv+su;

								dp[c[v]^1][v][ns][ndeg][np] += dp[c[v]][v][sv][degv][pv]*dp[c[u]][u][su][degu][pu]%mod;
								dp[c[v]^1][v][ns][ndeg][np] %= mod;
							}
						}
					}

					dp[c[v]][v][sv][degv][pv] = 0;
				}	
			}
		}

		c[v] ^= 1;
		sub[v] += sub[u];
	}
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin >> n;

	for(int i = 2; i <= n; ++i)
	{
		int x;
		cin >> x;
		G[x].pb(i);
	}

	dfs(1);

	int fact = 1;
	int ans = 0;

	for(int i = 1; i <= n; ++i)
	{
		fact = fact*i%mod;

		for(int deg = 0; deg < 3; ++deg)
		{
			ans -= fact*dp[c[1]][1][i][deg][1]%mod;
			ans += fact*dp[c[1]][1][i][deg][0]%mod;
			ans %= mod;
		}
	}

	ans += mod;
	ans %= mod;

	cout << ans << endl;
}

Submission Info

Submission Time
Task A - Awkward
User shadowatyy
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 2404 Byte
Status AC
Exec Time 752 ms
Memory 376320 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 2 ms 2432 KB
a02 AC 2 ms 2432 KB
a03 AC 2 ms 2432 KB
a04 AC 2 ms 2432 KB
b05 AC 2 ms 2432 KB
b06 AC 740 ms 188928 KB
b07 AC 657 ms 375808 KB
b08 AC 637 ms 283520 KB
b09 AC 629 ms 250496 KB
b10 AC 618 ms 236288 KB
b11 AC 615 ms 207488 KB
b12 AC 617 ms 193024 KB
b13 AC 644 ms 189056 KB
b14 AC 740 ms 189056 KB
b15 AC 637 ms 283520 KB
b16 AC 166 ms 109056 KB
b17 AC 5 ms 10624 KB
b18 AC 2 ms 2432 KB
b19 AC 639 ms 283392 KB
b20 AC 623 ms 250624 KB
b21 AC 627 ms 236160 KB
b22 AC 632 ms 283392 KB
b23 AC 635 ms 254592 KB
b24 AC 626 ms 232064 KB
b25 AC 627 ms 229888 KB
b26 AC 741 ms 188928 KB
b27 AC 630 ms 250368 KB
b28 AC 740 ms 195072 KB
b29 AC 647 ms 330496 KB
b30 AC 647 ms 330496 KB
b31 AC 643 ms 318080 KB
b32 AC 658 ms 375680 KB
b33 AC 651 ms 369664 KB
b34 AC 654 ms 375552 KB
b35 AC 651 ms 375808 KB
b36 AC 649 ms 365312 KB
b37 AC 648 ms 365312 KB
b38 AC 653 ms 371584 KB
b39 AC 647 ms 373504 KB
b40 AC 653 ms 371456 KB
b41 AC 661 ms 371456 KB
b42 AC 649 ms 371456 KB
b43 AC 655 ms 367360 KB
b44 AC 650 ms 363520 KB
b45 AC 660 ms 375936 KB
b46 AC 658 ms 375680 KB
b47 AC 657 ms 375680 KB
b48 AC 657 ms 375808 KB
b49 AC 657 ms 375808 KB
b50 AC 752 ms 283136 KB
b51 AC 751 ms 256512 KB
b52 AC 747 ms 225792 KB
b53 AC 743 ms 203264 KB
b54 AC 741 ms 195072 KB
b55 AC 666 ms 376320 KB