Language: C++

/**
 *    author:  otera    
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int long long

int n;
vector<int> g[55];

vector<int> pre;

int _pow(int x, int n) {
    int res = 1LL;
    while(n > 0) {
        if(n & 1) res = res * x;
        x = x * x;
        n >>= 1;
    }
    return res;
}

void dfs(int v, int p) {
    pre[v] = p;
    for(auto nv: g[v]) {
        if(nv == p) continue;
        dfs(nv, v);
    }
}

void solve() {
	cin >> n;
    map<pair<int, int>, int> mp;
    rep(i, n - 1) {
        int a, b; cin >> a >> b;
        --a; --b;
        mp[make_pair(a, b)] = i;
        mp[make_pair(b, a)] = i;
        g[a].pb(b);
        g[b].pb(a);
    }
    int m; cin >> m;
    set<int> se[55];
    vector<ll> b(m, 0);
    rep(i, m) {
        int u, v; cin >> u >> v;
        --u; --v;
        pre.resize(n);
        pre[u] = -1;
        dfs(u, -1);
        while(pre[v] != -1) {
            se[i].insert(mp[make_pair(v, pre[v])]);
            v = pre[v];
        }
        for(auto k: se[i]) {
            b[i] |= (1LL<<k);
        }
    }

    int ans = 0;
    //cerr << _pow(2, 10) << endl;

    //vector<int> dp(1<<22, 0);

    for(int bit = 0; bit < (1<<m); ++bit) {
        int cnt = __builtin_popcount(bit);
        set<int> s;
        ll dp = 0;
        rep(i, m) {
            if(bit & (1<<i)) {
                dp |= b[i];
            }
        }
        int cnt = 0;
        rep(i, n - 1) {
            if(dp & (1LL<<i)) ++cnt;
        }
        if(cnt % 2 == 0) {
            ans += _pow(2LL, n - 1 - cnt);
        } else {
            ans -= _pow(2LL, n - 1 - cnt);
        }
    }

    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:121:13: error: redeclaration of 'long long int cnt'
  121 |         int cnt = 0;
      |             ^~~
a.cc:113:13: note: 'long long int cnt' previously declared here
  113 |         int cnt = __builtin_popcount(bit);
      |             ^~~