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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    int n, m;
    E[] g;
    int[][] uv;

    void run() {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        g = new E[n];
        for (int i = 0; i < n; i++) g[i] = new E();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt() - 1, b = sc.nextInt() - 1;
            g[a].add(new P(b, i));
            g[b].add(new P(a, i));
        }
        m = sc.nextInt();
        uv = new int[m][2];
        for (int i = 0; i < m; i++) {
            int u = sc.nextInt() - 1, v = sc.nextInt() - 1;
            uv[i][0] = u;
            uv[i][1] = v;
        }

        long[] pows = new long[n + 1];
        pows[0] = 1;
        for (int i = 1; i <= n; i++) {
            pows[i] = pows[i - 1] * 2;
        }


        LCA lca = new LCA(n, g);
        lca.build(0);
        debug(lca.ar);
        long ans = 0;
        for (int i = 0; i < (1 << m); i++) {
            long cov = 0;
            long code = (Integer.bitCount(i) & 1) == 1 ? -1 : 1;
            for (int j = 0; j < m; j++) if (((i >> j) & 1) == 1) {
//                debug(uv[j][0], uv[j][1], Long.toBinaryString(extract(lca, uv[j][0], uv[j][1])));
                cov |= extract(lca, uv[j][0], uv[j][1]);
            }
            ans += code * pows[n - 1 - Long.bitCount(cov)];
        }
        System.out.println(ans);
    }

    long extract(LCA lca, int u, int v) {
        int t = lca.query(u, v);
        long cov = lca.ar[u] ^ lca.ar[t];
        cov ^= lca.ar[v] ^ lca.ar[t];
//        debug(Long.toBinaryString(Math.max(lca.ar[u], lca.ar[t]) - Math.min(lca.ar[u], lca.ar[t])), Long.toBinaryString(Math.max(lca.ar[v], lca.ar[t]) - Math.min(lca.ar[v], lca.ar[t])));
        return cov;
    }

    class LCA {
        int n;
        int[][] table;
        int[] dep;
        E[] g;
        int[] glen;
        int LOG;
        long[] ar;
        LCA(int n, E[] g) {
            int x = n;
            this.n = n;
            LOG = 0;
            while (x > 0) {
                x /= 2;
                LOG++;
            }

            table = new int[LOG][n];
            dep = new int[n];
            this.g = g;
            glen = new int[n];
            for (int i = 0; i < n; i++) {
                glen[i] = g[i].size();
            }
            ar = new long[n];
        }

        void dfs(int idx, int par, int d) {
//            debug(idx, par, d);
            table[0][idx] = par;
            dep[idx] = d;
            for (P next : g[idx]) {
                if (next.to != par) dfs(next.to, idx, d + 1);
            }
        }

        void build(int p) {
            dfs(p, -1, 0);
            for (int k = 0; k + 1 < LOG; k++) {
                for (int i = 0; i < n; i++) {
                    if (table[k][i] == -1) table[k + 1][i] = -1;
                    else table[k + 1][i] = table[k][table[k][i]];
                }
            }
            dfsEdge(p, -1, 0, 0);
        }

        void dfsEdge(int idx, int par, long ce, int eidx) {
            debug("", idx, eidx);
            ar[eidx] = ce;
            for (P next : g[idx]) {
                if (next.to != par) {
                    dfsEdge(next.to, idx, ce  | (1L << (next.idx - 1)), next.idx);
                }
            }
        }

        int query(int u, int v) {
            if (dep[u] > dep[v]) {
                int tmp = u;
                u = v;
                v = tmp;
            }
            for (int i = LOG - 1; i >= 0; i--) {
                if ((((dep[v] - dep[u]) >> i) & 1) == 1) {
                    v = table[i][v];
                }
            }
            if (u == v) return u;
//            debug("then", u, v);
            for (int i = LOG - 1; i >= 0; i--) {
                if (table[i][u] != table[i][v]) {
                    v = table[i][v];
                    u = table[i][u];
                }
            }
            return table[0][u];
        }
    }

    class E extends ArrayList<P> {}

    class P {
        int to, idx;
        P(int to, int idx) {
            this.to = to;
            this.idx = idx;
        }
    }


    void debug(Object... os) {
        System.err.println(Arrays.deepToString(os));
    }

    public static void main(String[] args) {
        new Main().run();
    }

}