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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{min,max};
#[allow(unused_imports)]
use std::collections::{BTreeMap,BTreeSet};
#[allow(unused_imports)]
use std::ops::*;
#[allow(unused_imports)]
use std::collections::BinaryHeap;

#[allow(unused_macros)]
macro_rules! tf {
    ($c:expr, $t:expr, $f:expr) => {{
        if $c { $t } else { $f }
    }};
}

fn dfs(graph: &Vec<Vec<usize>>, gs: &Vec<Mod>, prev: Option<usize>, v: usize) -> Vec<Mod> {
    let mut xs = vec![Mod(0), Mod(1)];
    for &w in &graph[v] {
        if prev == Some(w) {
            continue;
        }
        let ys = dfs(graph, gs, Some(v), w);
        let mut zs = vec![Mod(0); xs.len() + ys.len() - 1];
        for (i,&x) in xs.iter().enumerate() {
            for (k,&y) in ys.iter().enumerate() {
                // 繋げる場合
                // 構築中の連結成分のサイズはi+k, 連結成分の数は変わらないので(+)
                zs[i+k] += Mod(1) * x * y;
                // 繋げない場合
                // 構築中の連結成分のサイズはi, サイズkの連結成分が増える，連結成分の数が1つ増えるので(-)
                zs[i] += -Mod(1) * x * y * gs[k];
            }
        }
        xs = zs;
    }
    xs
}

fn main() {
    let n = read_i64() as usize;
    let graph = {
        let mut graph = vec![vec![]; n];
        for _ in 0..n-1 {
            let (a,b) = {
                let xs = read_vec_i64();
                ((xs[0]-1) as usize, (xs[1]-1) as usize)
            };
            graph[a].push(b);
            graph[b].push(a);
        }
        graph
    };
    let gs = {
        let mut gs = vec![Mod(0); n+1];
        gs[2] = Mod(1);
        for i in 4..n+1 {
            if i % 2 == 0 {
                gs[i] = gs[i-2] * Mod((i-1) as u64);
            }
        }
        gs
    };
    let xs = dfs(&graph, &gs, None, 0);
    let mut res = Mod(0);
    for (i,&x) in xs.iter().enumerate() {
        // 構築中のサイズiの連結成分の分を乗算
        res += x * gs[i];
    }
    println!("{}", res.0);
}

const MOD : u64 = 1_000_000_007;
#[derive(Clone,Copy,PartialEq,Debug)]
struct Mod (u64);

impl Mod {
    fn pow(self, n: u64) -> Mod {
        if n == 0 {
            return Mod(1);
        }
        let t = self.pow(n/2);
        if n % 2 == 0 {
            t * t
        }
        else {
            t * t * self
        }
    }
}

impl Add for Mod {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Mod((self.0 + rhs.0) % MOD)
    }
}

impl AddAssign for Mod {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl Sub for Mod {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Mod((self.0 + MOD - rhs.0 % MOD) % MOD)
    }
}

impl SubAssign for Mod {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl Neg for Mod {
    type Output = Mod;
    fn neg(self) -> Mod {
        Mod(MOD - self.0)
    }
}

impl Mul for Mod {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Mod(self.0 % MOD * rhs.0 % MOD)
    }
}

impl Div for Mod {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.pow(MOD-2)
    }
}

#[allow(dead_code)]
fn read_line() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    ret.pop();
    //ret.pop();
    return ret;
}

#[allow(dead_code)]
fn read_i64() -> i64 {
    let ss = read_line();
    return ss.parse::<i64>().unwrap();
}

#[allow(dead_code)]
fn read_vec_i64() -> Vec<i64> {
    let mut res = vec![];
    let ss = read_line();
    for ts in ss.split_whitespace() {
        let x = ts.parse::<i64>().unwrap();
        res.push(x);
    }
    return res;
}

use std::fmt::Display;
#[allow(dead_code)]
fn write_vec<T: Display>(xs: &Vec<T>) {
    if xs.len() == 0 {
        println!("");
        return;
    }
    print!("{}", xs[0]);
    for i in 1..xs.len() {
        print!(" {}", xs[i]);
    }
    println!("");
}