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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;

int n, m;
vector<vector<int>> paths;
vector<pair<int, int>> edges;
vector<bool> used;
vector<vector<bool>> edge_used;
vector<vector<int>> g;
vector<vector<vector<bool>>> path_edge;

bool dfs(int cur, int end, int num) {
    if (cur == end) {
        paths[num].push_back(cur);
        return true;
    }
    used[cur] = true;
    for (const auto &to:g[cur]) {
        if (!used[to]) {
            if (dfs(to, end, num)) {
                paths[num].push_back(cur);
                used[cur] = false;
                return true;
            }
        }
    }
    used[cur] = false;
    return false;
}

int main() {
    ll x = 1 << 49;
    cout << x;
    cin >> n;
    g.resize(n);
    used.resize(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
        edges.emplace_back(min(u, v), max(u, v));
    }
    cin >> m;
    paths.resize(m);
    path_edge.resize(n, vector<vector<bool>>(n, vector<bool>(m, false)));
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        dfs(u, v, i);
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j + 1 < paths[i].size(); ++j) {
            path_edge[min(paths[i][j], paths[i][j + 1])][max(paths[i][j], paths[i][j + 1])][i] = true;
        }
    }
    ll ans = 1ull << (n - 1);
    for (int i = 1; i < 1ull << (paths.size()); ++i) {
        ll edge_num = 0, bits = 0, cur = 1, j = 0;
        vector<int> ind;
        while (cur <= i) {
            if (cur & i) {
                bits++;
                ind.push_back(j);
            }
            cur *= 2;
            j++;
        }
        for (auto &edge:edges) {
            for (auto k:ind) {
                if (path_edge[edge.first][edge.second][k]) {
                    edge_num++;
                    break;
                }
            }
        }
        if (bits & 1) {
            ans -= 1ull << (n - 1 - edge_num);
        } else {
            ans += 1ull << (n - 1 - edge_num);
        }
    }
    cout << ans;
    return 0;
}