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: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::io::{self, BufRead};

#[allow(unused)]
macro_rules! input_at_once {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner_for_input_at_once!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner_for_input_at_once!{iter, $($r)*}
    };
}

#[allow(unused)]
macro_rules! input_inner_for_input_at_once {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value_for_input_at_once!($iter, $t);
        input_inner_for_input_at_once!{$iter $($r)*}
    };
}

#[allow(unused)]
macro_rules! read_value_for_input_at_once {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value_for_input_at_once!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value_for_input_at_once!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value_for_input_at_once!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value_for_input_at_once!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

const P: usize = 1_000_000_000 + 7;

//ノードiを根としてサイズjの木でmod2がmのときの場合の数
fn dfs_solve_(
  g: &Vec<Vec<usize>>,
  dp: &mut Vec<Vec<Vec<usize>>>,
  used: &mut Vec<bool>,
  size: &Vec<usize>,
  i: usize,
  pair_count: &Vec<usize>,
) {
  used[i] = true;
  dp[i][1][0] = 1;
  let mut current_size = 1;
  let mut tmp = vec![vec![0; 2]; g.len() + 1];
  for v in g[i].iter() {
    if used[*v] {
      continue;
    }
    for ite in tmp.iter_mut() {
      ite[0] = 0;
      ite[1] = 0;
    }
    dfs_solve_(g, dp, used, size, *v, pair_count);
    for x in 1..current_size + 1 {
      for y in 1..size[*v] + 1 {
        for m1 in 0..2 {
          for m2 in 0..2 {
            tmp[x + y][(m1 + m2) % 2] += (dp[i][x][m1] * dp[*v][y][m2]) % P;
            tmp[x + y][(m1 + m2) % 2] %= P;
            tmp[x][(m1 + m2 + 1) % 2] +=
              (((dp[i][x][m1] * pair_count[y]) % P) * dp[*v][y][m2]) % P;
            tmp[x][(m1 + m2 + 1) % 2] %= P;
          }
        }
      }
    }
    for s in 1..current_size + size[*v] + 1 {
      dp[i][s][0] = tmp[s][0];
      dp[i][s][1] = tmp[s][1];
    }
    current_size += size[*v];
  }
}

fn dfs_size(
  g: &Vec<Vec<usize>>,
  size: &mut Vec<usize>,
  i: usize,
  used: &mut Vec<bool>,
) {
  used[i] = true;
  size[i] = 1;
  for v in g[i].iter() {
    if used[*v] {
      continue;
    }
    dfs_size(g, size, *v, used);
    size[i] += size[*v];
  }
}

fn main() {
  // pray for AC
  input!{
    n: usize,
    xy: [[usize; 2]; n-1],
  };
  let xy: Vec<_> = xy
    .into_iter()
    .map(|xyi| vec![xyi[0] - 1, xyi[1] - 1])
    .collect();
  let mut g = vec![vec![]; n];
  for xyi in xy.iter() {
    let x = xyi[0];
    let y = xyi[1];
    g[x].push(y);
    g[y].push(x);
  }

  let mut size = vec![0; n];
  dfs_size(&g, &mut size, 0, &mut vec![false; n]);

  let mut dp = vec![vec![vec![0; 2]; n + 1]; n];
  let mut pair_count = vec![0; 5001];
  pair_count[0] = 1;
  for i in 2..5001 {
    if i % 2 == 1 {
      continue;
    }
    pair_count[i] = (pair_count[i - 2] * (i - 1)) % P;
  }
  dfs_solve_(&g, &mut dp, &mut vec![false; n], &size, 0, &pair_count);

  let mut ans = 0;
  for s in 1..size[0] + 1 {
    ans += ((dp[0][s][0] * pair_count[s]) % P
      + (P - ((dp[0][s][1] * pair_count[s]) % P)))
      % P;
    ans %= P;
  }
  println!("{}", ans);
}