AtCoder Regular Contest 101 - Ribbons on Tree

Let N be an even number.
There is a tree with N vertices.
The vertices are numbered 1, 2, ..., N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Snuke would like to decorate the tree with ribbons, as follows.
First, he will divide the N vertices into N / 2 pairs.
Here, each vertex must belong to exactly one pair.
Then, for each pair (u, v), put a ribbon through all the edges contained in the shortest path between u and v.
Snuke is trying to divide the vertices into pairs so that the following condition is satisfied: "for every edge, there is at least one ribbon going through it."
How many ways are there to divide the vertices into pairs, satisfying this condition?
Find the count modulo 10^9 + 7.
Here, two ways to divide the vertices into pairs are considered different when there is a pair that is contained in one of the two ways but not in the other.

-----Constraints-----
 - N is an even number.
 - 2 \leq N \leq 5000
 - 1 \leq x_i, y_i \leq N
 - The given graph is a tree.

-----Input-----
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

-----Output-----
Print the number of the ways to divide the vertices into pairs, satisfying the condition, modulo 10^9 + 7.

-----Sample Input-----
4
1 2
2 3
3 4

-----Sample Output-----
2

There are three possible ways to divide the vertices into pairs, as shown below, and two satisfy the condition: the middle one and the right one.

Source: atcoder | Difficulty: VERY_HARD


Solutions:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
import numpy as np
MOD = 10 ** 9 + 7
N = int(input())
graph = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(x, y) = list(map(int, input().split()))
	graph[x].append(y)
	graph[y].append(x)

def dp_merge(data1, data2):
	N1 = len(data1) - 1
	N2 = len(data2) - 1
	if N1 > N2:
		(N1, N2) = (N2, N1)
		(data1, data2) = (data2, data1)
	data = np.zeros(N1 + N2, dtype=np.int64)
	for n in range(1, N1 + 1):
		data[n:n + N2] += data1[n] * data2[1:] % MOD
	data %= MOD
	return data
fact_2 = [1, 0, 1]
for n in range(3, N + 10):
	fact_2.append(fact_2[n - 2] * (n - 1) % MOD)
fact_2 = np.array(fact_2, dtype=np.int64)

def dp_add_edge(data):
	N = len(data) - 1
	data1 = np.zeros(N + 2, dtype=np.int64)
	data1[1:] = data
	data1[1] = -(data * fact_2[:N + 1] % MOD).sum() % MOD
	return data1

def dfs(v, parent=None):
	data = None
	for y in graph[v]:
		if y == parent:
			continue
		data1 = dfs(y, v)
		data1 = dp_add_edge(data1)
		if data is None:
			data = data1
		else:
			data = dp_merge(data, data1)
	if data is None:
		return np.array([0, 1], dtype=np.int64)
	return data
data = dfs(1)
answer = (data * fact_2[:N + 1] % MOD).sum() % MOD
print(answer)


import sys
sys.setrecursionlimit(5001)
MOD = 10 ** 9 + 7
n = int(input())
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
	(x, y) = list(map(int, line.split()))
	x -= 1
	y -= 1
	links[x].add(y)
	links[y].add(x)
double_factorial_odd = [0] * (n // 2)
prev = 1
for i in range(n // 2):
	prev = double_factorial_odd[i] = (2 * i + 1) * prev % MOD

def dfs(v, p):
	ret = [0, 1]
	for u in links[v]:
		if u == p:
			continue
		res = dfs(u, v)
		(lt, ls) = (len(ret), len(res))
		mrg = [0] * (lt + ls - 1)
		for i in range(1 - lt % 2, lt, 2):
			c = ret[i]
			for j in range(1 - ls % 2, ls, 2):
				mrg[i + j] = (mrg[i + j] + c * res[j]) % MOD
		ret = mrg
	if len(ret) % 2 == 1:
		ret[0] = -sum((pattern * df % MOD for (pattern, df) in zip(ret[2::2], double_factorial_odd))) % MOD
	return ret
print(MOD - dfs(0, -1)[0])


mod = 10 ** 9 + 7
N = 2 * 10 ** 5
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, N + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
import sys
sys.setrecursionlimit(10 ** 4)
N = int(input())
edge = [[] for i in range(N)]
parent = [0] * N
deg = [1] * N
deg[0] = 0
for i in range(N - 1):
	(x, y) = map(int, input().split())
	edge[x - 1].append(y - 1)
	edge[y - 1].append(x - 1)

def prv(v, pv):
	for nv in edge[v]:
		if nv != pv:
			parent[nv] = v
			prv(nv, v)
prv(0, -1)
for i in range(N):
	new = []
	for v in edge[i]:
		if v != parent[i]:
			new.append(v)
	edge[i] = new
from collections import deque
ans = list((v for v in range(N) if deg[v] == 0))
deq = deque(ans)
used = [0] * N
while deq:
	v = deq.popleft()
	for t in edge[v]:
		deg[t] -= 1
		if deg[t] == 0:
			deq.append(t)
			ans.append(t)
dp = [[] for i in range(N)]
sz = [0 for i in range(N)]
for v in ans[::-1]:
	sz[v] = 1
	dp[v] = [0] * (sz[v] + 1)
	dp[v][1] = 1
	for nv in edge[v]:
		merged = [0] * (sz[v] + sz[nv] + 1)
		for i in range(sz[v] + 1):
			for j in range(sz[nv] + 1):
				merged[i + j] = (merged[i + j] + dp[v][i] * dp[nv][j]) % mod
		sz[v] += sz[nv]
		dp[v] = merged
	for k in range(1, sz[v] + 1):
		dp[v][0] = (dp[v][0] - g1[k] * g2[k // 2] % mod * ((k + 1) % 2) * dp[v][k]) % mod
print(-dp[0][0] * pow(inverse[2], N // 2, mod) % mod)