Submission #1854812


Source Code Expand

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

const int N = 2005;
const int mod = 1e9 + 7;
const int half = 5e8 + 4;

int n;
int sz[N]; // number of edges
int prd[N];
int f[N][N][3]; // (i, j, k) subtree i, number of edges: j, number of edges connect to i: k
int g[N][N][3]; // knapsack
vector<int> G[N];

// a fixed set of edges has number of edges: i and number of components: j
// number of ways *= (2 ^ j) * (n - i)! * ((-1) ^ i)

void add(int &x, int y) {
	x = (x + y) >= mod ? (x + y - mod) : (x + y);
}

void dfs(int u) {
	for (auto v : G[u]) dfs(v);
	int id = 0;
	g[0][0][0] = 1;
	for (auto v : G[u]) {
		++id;
		// reset
		for (int i = 0; i <= sz[u] + sz[v] + 1; ++i) {
			for (int j = 0; j <= 2; ++j) g[id][i][j] = 0;
		}
		for (int i = 0; i <= sz[u]; ++i) {
			for (int j = 0; j <= sz[v]; ++j) {
				for (int k = 0; k <= 2; ++k) {
					// do not use u-v edge
					for (int l = 0; l <= 2; ++l) {
						add(g[id][i + j][k], 1LL * g[id - 1][i][k] * f[v][j][l] % mod); 
					}
					// use u-v edge
					if (k == 0) {
						add(g[id][i + j + 1][k + 1], 2LL * g[id - 1][i][k] * f[v][j][0] % mod);
						add(g[id][i + j + 1][k + 1], 1LL * g[id - 1][i][k] * f[v][j][1] % mod);
					}
					if (k == 1) {
						add(g[id][i + j + 1][k + 1], 1LL * g[id - 1][i][k] * f[v][j][0] % mod);
						add(g[id][i + j + 1][k + 1], 1LL * g[id - 1][i][k] * f[v][j][1] % mod * half % mod);
					}
				}
			}
		}
		sz[u] += sz[v] + 1;
	}
	// cout << "At " << u << '\n';
	for (int i = 0; i <= sz[u]; ++i) {
		for (int j = 0; j <= 2; ++j) {
			f[u][i][j] = g[id][i][j];
			// cout << f[u][i][j] << ' ' << i << ' ' << j << '\n';
 		}
 	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 2; i <= n; ++i) {
		int p; cin >> p, G[p].push_back(i);
	}
	dfs(1);
	prd[0] = 1; 
	for (int i = 1; i <= n; ++i) prd[i] = 1LL * prd[i - 1] * i % mod;
	int res = 0;
	for (int i = 0; i < n; ++i) {
		int tmp = 0;
		for (int j = 0; j <= 2; ++j) add(tmp, f[1][i][j]);
		// inclusion-exclusion
		if (i & 1) tmp = mod - tmp;
		add(res, 1LL * tmp * prd[n - i] % mod);
	}
	cout << res;
}

Submission Info

Submission Time
Task A - Awkward
User aome
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 2147 Byte
Status AC
Exec Time 111 ms
Memory 93056 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 2304 KB
a03 AC 2 ms 2432 KB
a04 AC 2 ms 2432 KB
b05 AC 2 ms 2432 KB
b06 AC 111 ms 92928 KB
b07 AC 97 ms 47872 KB
b08 AC 93 ms 47488 KB
b09 AC 93 ms 47488 KB
b10 AC 93 ms 47488 KB
b11 AC 93 ms 47616 KB
b12 AC 94 ms 48128 KB
b13 AC 97 ms 53376 KB
b14 AC 107 ms 71296 KB
b15 AC 93 ms 47488 KB
b16 AC 27 ms 24960 KB
b17 AC 2 ms 4480 KB
b18 AC 2 ms 2432 KB
b19 AC 93 ms 47488 KB
b20 AC 93 ms 47616 KB
b21 AC 93 ms 47616 KB
b22 AC 93 ms 47616 KB
b23 AC 95 ms 50944 KB
b24 AC 94 ms 47744 KB
b25 AC 94 ms 47616 KB
b26 AC 111 ms 92928 KB
b27 AC 94 ms 47744 KB
b28 AC 110 ms 90880 KB
b29 AC 96 ms 47744 KB
b30 AC 96 ms 47744 KB
b31 AC 95 ms 47616 KB
b32 AC 97 ms 47872 KB
b33 AC 93 ms 47488 KB
b34 AC 93 ms 47616 KB
b35 AC 93 ms 47616 KB
b36 AC 93 ms 47616 KB
b37 AC 93 ms 47616 KB
b38 AC 93 ms 47488 KB
b39 AC 93 ms 47616 KB
b40 AC 93 ms 47616 KB
b41 AC 93 ms 47616 KB
b42 AC 93 ms 47744 KB
b43 AC 93 ms 47616 KB
b44 AC 93 ms 47616 KB
b45 AC 97 ms 47872 KB
b46 AC 98 ms 47872 KB
b47 AC 97 ms 48000 KB
b48 AC 97 ms 48000 KB
b49 AC 97 ms 48000 KB
b50 AC 110 ms 88832 KB
b51 AC 110 ms 93056 KB
b52 AC 110 ms 92928 KB
b53 AC 111 ms 92928 KB
b54 AC 111 ms 92928 KB
b55 AC 95 ms 47744 KB