Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i.

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

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

Sample Output 4

62

The tree in this input is shown below:

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

//! ----------------------------------------------
//! Framework <https://github.com/vain0x/procon>
//!
//! See the bottom of file for solution.
//! ----------------------------------------------

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

/// Print values to standard error if debug mode.
#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        })*
    };
}

/// Read from standard input and parse each word.
/// - `read!(T, U, ..)` parses a line as a tuple of words.
/// - `read![[T]]` parses a line as an array of words.
/// - `read![..; N]` parses `N` lines, using `read!(..)` repeatedly.
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

/// Read a line from standard input.
#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();

    #[allow(deprecated)]
    buf.trim_right().to_owned()
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

/// s & (1 << i) != 0 <=> エッジ el + i が黒に塗られている (i < er - el)
/// 満たされる制約の集合を返す。
fn bfs(
    s: usize,
    el: usize,
    er: usize,
    G: &Vec<Vec<usize>>,
    edge_map: &Vec<Vec<usize>>,
    C: &Vec<(usize, usize)>,
) -> usize {
    let mut x = 0;
    let N = G.len();

    let mut q = VecDeque::new();
    for ci in 0..C.len() {
        let (u, _) = C[ci];
        q.push_back((u, ci, N, false));
    }

    while let Some((u, ci, p, b)) = q.pop_front() {
        if u == C[ci].1 && b {
            x |= 1 << ci;
            continue;
        }

        for ei in 0..G[u].len() {
            let v = G[u][ei];
            if v == p {
                continue;
            }

            // 黒のエッジを通る？
            let b = b || {
                let e = edge_map[u][v];
                el <= e && e < er && (s & (1 << (e - el))) != 0
            };

            q.push_back((v, ci, u, b));
        }
    }

    x
}

fn main() {
    let N = read!(usize);

    let mut G = vec![vec![]; N];
    for (mut u, mut v) in read![usize, usize; N - 1] {
        u -= 1;
        v -= 1;
        G[u].push(v);
        G[v].push(u);
    }

    let M = read!(usize);
    let mut C = read![usize, usize; M];
    for &mut (ref mut u, ref mut v) in &mut C {
        *u -= 1;
        *v -= 1;
    }

    let mut edges = vec![];
    let mut edge_map = vec![vec![N; N]; N];

    for u in 0..N {
        for ei in 0..G[u].len() {
            let v = G[u][ei];
            if u < v {
                edge_map[u][v] = edges.len();
                edge_map[v][u] = edges.len();
                edges.push((u, v));
            }
        }
    }
    debug!(edges, edge_map);

    let em = (N - 1) / 2;
    let el = 0;
    let er = em;
    let mut dp = vec![0_usize; 1 << M];

    for s in 0..(1 << (er - el)) {
        let x = bfs(s, el, er, &G, &edge_map, &C);
        debug!((s, x));
        dp[x] += 1;
    }

    debug!(dp);

    // 累積和
    for pc in (0..M + 1).rev() {
        for x in 0..1 << M {
            if (0..M).filter(|&i| (x & (1 << i)) != 0).count() == pc {
                for i in 0..M {
                    if x & (1 << i) != 0 {
                        dp[x & !(1 << i)] += dp[x];
                    }
                }
            }
        }
    }

    debug!(dp);

    let el = em;
    let er = N - 1;
    let mut count = 0;

    for s in 0..(1 << (er - el)) {
        let y = bfs(s, el, er, &G, &edge_map, &C);
        debug!((s, y));

        let x = ((1 << M) - 1) & !y;
        count += dp[x];
    }

    println!("{}", count)
}