Submission #2173512


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

const int N = 2005;
const int mod = 1e9 + 7;

int n;
vector <int> G[N];
int dp[N][N][3];
int f[N][3];
int nchild[N];

void add(int &x, int y) {
	x += y; while(x >= y) x -= mod; while(x < 0) x += mod;
}

void dfs(int u) {
	dp[u][0][0] = 1;

	for (int &v : G[u]) {
		dfs(v);
		memset(f, 0, sizeof f);

		for (int ncut_u = 0; ncut_u <= nchild[u]; ++ncut_u) {
			for (int deg_u = 0; deg_u <= 2; ++deg_u) {
				for (int ncut_v = 0; ncut_v <= nchild[v]; ++ncut_v) {
					for (int deg_v = 0; deg_v <= 2; ++deg_v) {
						if (!dp[u][ncut_u][deg_u] || !dp[v][ncut_v][deg_v]) continue;

						int mul = 1LL * dp[u][ncut_u][deg_u] * dp[v][ncut_v][deg_v] % mod;

						// cut
						add(f[ncut_u + ncut_v + 1][deg_u], (deg_v == 1 ? 2LL : 1LL) * mul % mod);

						// concatenate
						if (deg_u <= 1 && deg_v <= 1) {
							add(f[ncut_u + ncut_v][deg_u + 1], mul);
						}
					}
				}
			}
		}

		nchild[u] += nchild[v];
		for (int ncut_u = 0; ncut_u <= nchild[u]; ++ncut_u) {
			for (int deg_u = 0; deg_u <= 2; ++deg_u) {
				dp[u][ncut_u][deg_u] = f[ncut_u][deg_u]; // update the values of dp[]
			}
		}
	}

	++nchild[u];
	for (int ncut_u = 0; ncut_u < nchild[u]; ++ncut_u) {
		for (int deg_u = 0; deg_u <= 2; ++deg_u) {
			if (deg_u == 2) {
				add(dp[u][ncut_u][deg_u], dp[u][ncut_u][deg_u]); // x 2
			}
			if (u == 1 && deg_u == 1) {
				add(dp[u][ncut_u][deg_u], dp[u][ncut_u][deg_u]); // x 2	
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 2; i <= n; ++i) {
		int p; cin >> p; G[p].push_back(i);
	}

	dfs(1);

	int fact = 1;
	int res = 0;
	for (int ncut = 0; ncut <= n - 1; ++ncut) {
		int npair = n - 1 - ncut;
		int cur = 0;
		for (int deg = 0; deg <= 2; ++deg) add(cur, dp[1][ncut][deg]);

		fact = 1LL * fact * (ncut + 1) % mod; // (ncut + 1)!
		cur = 1LL * cur * fact % mod;

		if (npair % 2 == 0) add(res, cur); else add(res, -cur);
	}

	cout << res << endl;
}

Submission Info

Submission Time
Task A - Awkward
User cheater2k
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 2049 Byte
Status AC
Exec Time 210 ms
Memory 47744 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 91 ms 45824 KB
b07 AC 92 ms 47744 KB
b08 AC 114 ms 45952 KB
b09 AC 90 ms 45824 KB
b10 AC 72 ms 45824 KB
b11 AC 41 ms 45824 KB
b12 AC 33 ms 45824 KB
b13 AC 44 ms 45824 KB
b14 AC 91 ms 45824 KB
b15 AC 114 ms 45952 KB
b16 AC 17 ms 23296 KB
b17 AC 2 ms 2688 KB
b18 AC 1 ms 384 KB
b19 AC 130 ms 45952 KB
b20 AC 91 ms 45824 KB
b21 AC 75 ms 45824 KB
b22 AC 115 ms 45952 KB
b23 AC 105 ms 45824 KB
b24 AC 71 ms 45952 KB
b25 AC 55 ms 47232 KB
b26 AC 91 ms 45824 KB
b27 AC 62 ms 47488 KB
b28 AC 96 ms 45824 KB
b29 AC 90 ms 47616 KB
b30 AC 90 ms 47616 KB
b31 AC 93 ms 47488 KB
b32 AC 92 ms 47744 KB
b33 AC 146 ms 45952 KB
b34 AC 210 ms 45952 KB
b35 AC 205 ms 45952 KB
b36 AC 148 ms 45952 KB
b37 AC 141 ms 45952 KB
b38 AC 146 ms 46080 KB
b39 AC 158 ms 45952 KB
b40 AC 156 ms 45952 KB
b41 AC 134 ms 45952 KB
b42 AC 144 ms 45952 KB
b43 AC 143 ms 45824 KB
b44 AC 131 ms 45952 KB
b45 AC 91 ms 47616 KB
b46 AC 92 ms 47616 KB
b47 AC 93 ms 47744 KB
b48 AC 93 ms 47744 KB
b49 AC 93 ms 47744 KB
b50 AC 98 ms 45824 KB
b51 AC 93 ms 45824 KB
b52 AC 92 ms 45824 KB
b53 AC 91 ms 45824 KB
b54 AC 91 ms 45824 KB
b55 AC 97 ms 46720 KB