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)

// https://atcoder.jp/contests/arc101/tasks/arc101_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{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!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

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

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

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

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

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

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn dfs_count(now: usize, par: usize, graph: &Vec<Vec<usize>>, children: &mut Vec<usize>) -> usize {
    let mut count = 1;
    for &to in &graph[now] {
        if to == par {
            continue;
        }
        count += dfs_count(to, now, graph, children);
    }
    children[now] = count;
    count
}

type DpTable = Vec<Vec<Vec<i64>>>;

fn solve(now: usize, par: usize, graph: &Vec<Vec<usize>>, children: &Vec<usize>, dp: &mut DpTable) {
    if children[now] == 1 {
        dp[now][0][0] = 0; // without parent edge
        dp[now][1][0] = 1; // has parent edge
        return;
    }

    let mut cids = vec![];
    for &to in &graph[now] {
        if to == par {
            continue;
        }
        cids.push((children[to], to));
        solve(to, now, graph, children, dp);
    }


    let mut wdp = dvec!(0; 2, 2);
    wdp[1][0] = 1;
    let mut ctotal = 1;
    for (count, cid) in cids {
        let wl = wdp.len();
        let cl = dp[cid].len();
        let mut tdp = dvec!(0; wl+cl+1, 2);

        for f in 0..wl {
            for c in 0..cl {
                let zero = dp[cid][c][0] * wdp[f][0] % MOD + dp[cid][c][1] * wdp[f][1] % MOD;
                let one  = dp[cid][c][1] * wdp[f][0] % MOD + dp[cid][c][0] * wdp[f][1] % MOD;
                tdp[f+c][0] += zero;
                tdp[f+c][1] += one;
                tdp[f+c][0] %= MOD;
                tdp[f+c][1] %= MOD;
            }
        }
        wdp = tdp;
        ctotal += count;
    }


    let fcn = dp[now].len();
    dp[now][1][0] = 1;
    for f in 1..fcn {
        dp[now][f][0] = wdp[f][0];
        dp[now][f][1] = wdp[f][1];
    }

    let mut zero0 = 0;
    let mut zero1 = 0;
    let mut mul = 1;
    for f in 2..fcn {
        if f % 2 == 1 {
            continue;
        }
        mul *= (f-1) as i64;
        mul %= MOD;

        zero0 += mul * dp[now][f][0] % MOD;
        if zero0 >= MOD {
            zero0 -= MOD;
        }
        zero1 += mul * dp[now][f][1] % MOD;
        if zero1 >= MOD {
            zero1 -= MOD;
        }
    }
    dp[now][0][0] = zero1;
    dp[now][0][1] = zero0;
}

const MOD: i64 = 1e9 as i64 + 7;

fn main() {
    input! {
        n: usize,
        edges: [(usize1, usize1); n-1]
    };

    let mut dp = vec![];
    let mut graph = vec![vec![]; n];
    for (u, v) in edges {
        graph[u].push(v);
        graph[v].push(u);
    }
    let mut children = vec![0; n];

    dfs_count(0, n, &graph, &mut children);

    let mut best_root = 0;
    for i in 1..n {
        let mut max_child = n - children[i];
        for &to in &graph[i] {
            if children[i] > children[to] {
                max_child = max(max_child, children[to]);
            }
        }
        if max_child < n/2 {
            best_root = i;
            break;
        }
    }

    dfs_count(best_root, n, &graph, &mut children);

    for i in 0..n {
        let cnt = children[i];
        let wx = dvec!(0; cnt+1, 2);
        dp.push(wx);
    }

    solve(best_root, n, &graph, &children, &mut dp);

    // debug!(dp);

    println!("{}", (MOD+dp[best_root][0][1]-dp[best_root][0][0])%MOD);
}