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++ (GCC 9.2.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
using P = pair<int,int>;

//bit全探索改
vector<string> bit_all_search(int n){
  vector<string> use(0);
  queue<string> Q;
  Q.push("");
  while(Q.size()){
    string S = Q.front();
    Q.pop();
    string T = S;
    T += '0';
    if(T.size() == n){
      use.push_back(T);
    }
    else{
      Q.push(T);
    }
    T = S;
    T += '1';
    if(T.size() == n){
      use.push_back(T);
    }
    else{
      Q.push(T);
    }
  }
  return use;
}

int main(){
  int N;
  cin >> N;
  vector<vector<int>> G(N,vector<int>(0));
  map<P,int> M;
  for(int i=0; i<N-1; i++){
    int a, b;
    cin >> a >> b;
    if(a > b){
      swap(a,b);
    }
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
    P add = {a,b};
    M[add] = i;
  }
  
  
  //条件が満たされないのは、M個の制約について、使う辺の集合全てが白であることをMiとすると、
  //M1 || M2 || ... || Mmとなる
  //よってこれに包除原理を用いるとかつの式に変換できる
  
  int m;
  cin >> m;
  
  vector<set<int>> S(m);
  
  for(int i=0; i<m; i++){
    int x, y;
    cin >> x >> y;
    x--; y--;
    vector<int> par(N);
    vector<bool> seen(N,false);
    queue<int> Q;
    Q.push(x);
    seen[x] = true;
    while(Q.size()){
      int x = Q.front();
      Q.pop();
      for(int i=0; i<G[x].size(); i++){
        if(!seen[G[x][i]]){
          seen[G[x][i]] = true;
          par[G[x][i]] = x;
          Q.push(G[x][i]);
        }
      }
    }
    while(y != x){
      int x1, y1;
      x1 = par[y];
      y1 = y;
      if(x1 > y1){
        swap(x1,y1);
      }
      P check = {x1,y1};
      int z = M[check];
      S[i].insert(z);
      y = par[y];
    }
  }
  
  /*for(int i=0; i<m; i++){
    for(auto itr = S[i].begin(); itr != S[i].end(); itr++){
      cout << *itr << " ";
    }
    cout << endl;
  }*/
  
  int64_t ans = pow(2,N-1);
  int64_t no = 0;
  
  vector<string> X = bit_all_search(m);
  
  for(int i=0; i<X.size(); i++){
    string T = X[i];
    int cnt = 0;
    set<int> same;
    for(int j=0; j<m; j++){
      if(T[j] == '1'){
        cnt++;
        for(auto itr = S[j].begin(); itr != S[j].end(); itr++){
          same.insert(*itr);
        }
      }
    }
    int SIZE = same.size();
    if(cnt %2 == 1){
      no += pow(2,N-1-SIZE);
    }
    else if(cnt != 0){
      no -= pow(2,N-1-SIZE);
    }  
  }
  cout << ans - no << endl;
}