Problem:
Score : 900 points

Problem Statement

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 1

4
1 2
2 3
3 4

Sample Output 1

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.

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

3

There are three possible ways to divide the vertices into pairs, as shown below, and all of them satisfy the condition.

Sample Input 3

6
1 2
1 3
3 4
1 5
5 6

Sample Output 3

10

Sample Input 4

10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

672

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#define Endl endl
#define mp make_pair
#define ll long long 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define ceil(a,b) ((a-1)/b+1)
#define srand() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l,r) uniform_int_distribution<int>(l,r)(rng)
typedef unsigned long long ull;
const int inf=1039074182;
//const int mod=1e9+7;
using namespace std;
int dp[5005][2][5005];
vector <int> vec[5005];
int n;
int par[5005];
int son[5005];
vector <int> sumson[5005];
int f[5005];

namespace combinatorics
{
	int *fac;
	int *ifac;
	int mod;
	inline int add(int a,int b)
	{
		return (a+b)%mod;
	}
	inline int minus(int a,int b)
	{
		return (a-b+mod)%mod;
	}
	inline int multiply(int a,int b)
	{
		return (1LL*a*b)%mod;
	}
	inline int fastpow(int basic,int x)
	{
		int res=1;
		while(x)
		{
			if(x&1)	res=multiply(res,basic);
			basic=multiply(basic,basic);
			x>>=1;
		}
		return res;
	}
	inline int inv(int x)
	{
		return fastpow(x,mod-2);
	}
	void init(int n,int tmod)
	{
		mod=tmod;
		fac=new int[n+5];
		ifac=new int[n+5];
		fac[0]=1;
		for(int i=1;i<=n;i++)
		{
			fac[i]=multiply(fac[i-1],i);
		}
		ifac[n]=inv(fac[n]);
		for(int i=n-1;i>=0;i--)
		{
			ifac[i]=multiply(ifac[i+1],i+1);
		}
	}
	inline int C(int n,int m)
	{
		return multiply(multiply(fac[n],ifac[m]),ifac[n-m]);
	}
	inline int Cat(int x)
	{
		return multiply(C(x*2,x),inv(x+1));
	}
};
using namespace combinatorics;

void go(int x,int p=-1)
{
	son[x]=1;
	par[x]=p;
	for(int i=0;i<vec[x].size();i++)
	{
		if(vec[x][i]==p) continue;
		go(vec[x][i],x);
		son[x]+=son[vec[x][i]];
	}
}

void dfs(int x)
{
//	cout<<x<<endl;
	for(int i=0;i<vec[x].size();i++)
	{
		dfs(vec[x][i]);
	}
	int n=vec[x].size();
	int dp2[n+1][2][son[x]+1];
	memset(dp2,0,sizeof(dp2));
	dp2[0][0][1]=1;
	for(int i=0;i<n;i++)
	{
		for(int parity=0;parity<2;parity++)
		{
			for(int remain=0;remain<=sumson[x][i]-son[vec[x][i]]+1;remain++)
			{
				for(int sz=0;sz<=son[vec[x][i]];sz++)
				{
					for(int ncut=0;ncut<2;ncut++)
					{
						dp2[i+1][parity^1^ncut][remain]=(1LL*dp2[i+1][parity^1^ncut][remain]+(1LL*dp2[i][parity][remain]*dp[vec[x][i]][ncut][sz])%mod*f[sz])%mod;
						dp2[i+1][parity^ncut][remain+sz]=(1LL*dp2[i+1][parity^ncut][remain+sz]+1LL*dp2[i][parity][remain]*dp[vec[x][i]][ncut][sz])%mod;
					}
				}
			}
		}
	}
	for(int parity=0;parity<2;parity++)
	{
		for(int remain=0;remain<=son[x];remain++)
		{
			dp[x][parity][remain]=dp2[n][parity][remain];
		}
	}
}

int main()
{
	freopen("input.txt","r",stdin);
	memset(dp,0,sizeof(dp));
	cin>>n;
	init(n+5,1e9+7);
	f[0]=1;
	for(int i=2;i<=n;i+=2)
	{
		f[i]=multiply(multiply(fac[i],ifac[i/2]),inv(fastpow(2,i/2)));
	}
//	cout<<f[2000]<<endl;
	int x,y;
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&x,&y);
		x--;
		y--;
		vec[x].push_back(y);
		vec[y].push_back(x);
	}
	go(0);
	for(int i=1;i<n;i++)
	{
		bool flag=false;
		for(int j=0;j<(int)vec[i].size()-1;j++)
		{
			if(vec[i][j]==par[i])
			{
				flag=true;
			}
			if(flag) vec[i][j]=vec[i][j+1];
		}
		vec[i].resize((int)vec[i].size()-1);
	}
	for(int i=0;i<n;i++)
	{
		sumson[i].resize(vec[i].size());
		for(int j=0;j<vec[i].size();j++)
		{
			sumson[i][j]=(j?sumson[i][j-1]:0)+son[vec[i][j]];
		}
	}
	dfs(0);
	ll res=0;
//	for(int j=0;j<2;j++)
//	{
//		for(int i=0;i<=son[1];i++)
//		{
//			cout<<dp[1][j][i]<<' ';
//		}
//		cout<<endl;
//	}
	for(int i=0;i<=son[0];i++)
	{
		res=(res+1LL*dp[0][0][i]*f[i]-1LL*dp[0][1][i]*f[i])%mod;
//		res+=dp[0][0][i]*f[i];
//		res-=dp[0][1][i]*f[i];
	}
	cout<<res<<endl;
	return 0;
}