Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
using llong = long long;

template< typename G >
struct HeavyLightDecomposition {
    G &g;
    vector< int > sz, in, out, head, rev, par;

    HeavyLightDecomposition(G &g) :
        g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}

    void dfs_sz(int idx, int p) {
        par[idx] = p;
        sz[idx] = 1;
        if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
        for(auto &to : g[idx]) {
            if(to == p) continue;
            dfs_sz(to, idx);
            sz[idx] += sz[to];
            if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
        }
    }

    void dfs_hld(int idx, int par, int &times) {
        in[idx] = times++;
        rev[in[idx]] = idx;
        for(auto &to : g[idx]) {
            if(to == par) continue;
            head[to] = (g[idx][0] == to ? head[idx] : to);
            dfs_hld(to, idx, times);
        }
        out[idx] = times;
    }

    void build() {
        dfs_sz(0, -1);
        int t = 0;
        dfs_hld(0, -1, t);
    }

    int la(int v, int k) {
        while(1) {
            int u = head[v];
            if(in[v] - k >= in[u]) return rev[in[v] - k];
            k -= in[v] - in[u] + 1;
            v = par[u];
        }
    }

    int lca(int u, int v) {
        for(;; v = par[head[v]]) {
            if(in[u] > in[v]) swap(u, v);
            if(head[u] == head[v]) return u;
        }
    }

    template< typename T, typename Q, typename F >
    T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {
        T l = ti, r = ti;
        for(;; v = par[head[v]]) {
            if(in[u] > in[v]) swap(u, v), swap(l, r);
            if(head[u] == head[v]) break;
            l = f(q(in[head[v]], in[v] + 1), l);
        }
        return f(f(q(in[u] + edge, in[v] + 1), l), r);
        //  return {f(q(in[u] + edge, in[v] + 1), l), r};
    }

    template< typename Q >
    void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }
};
//===
template<class Monoid, class Laz>
struct LazySegmentTree {

    const function<Monoid(Monoid, Monoid)> mergeMonoid;
    const function<Monoid(Monoid, Laz, int)> applyLaz;
    const function<Laz(Laz, Laz)> mergeLaz;
    
    const Monoid e; // neutral element
    
    vector<Monoid> seg;
    vector<Laz> lazy;
    vector<bool> isUpdated;
    
    int size;
                                           
    LazySegmentTree(int nmemb, const Monoid &e,
                    function<Monoid(Monoid, Monoid)> f,
                    function<Monoid(Monoid, Laz, int)> g,
                    function<Laz(Laz, Laz)> h):
        e(e), mergeMonoid(f), applyLaz(g), mergeLaz(h)
    {
        size = 1;
        while (size < nmemb) {
            size *= 2;
        }

        seg.assign(2 * size - 1, e);
        isUpdated.assign(2 * size - 1, true);
        lazy.resize(2 * size - 1); 
    };

    inline void propagation(int k, int len) {
        if (!isUpdated[k]) {
            seg[k] = applyLaz(seg[k], lazy[k], len);
            if (len > 1) {
                if (isUpdated[2 * k + 1])
                    lazy[2 * k + 1] = lazy[k], isUpdated[2 * k + 1] = false;
                else
                    lazy[2 * k + 1] = mergeLaz(lazy[2 * k + 1], lazy[k]);
                
                if (isUpdated[2 * k + 2])
                    lazy[2 * k + 2] = lazy[k], isUpdated[2 * k + 2] = false;
                else 
                    lazy[2 * k + 2] = mergeLaz(lazy[2 * k + 2], lazy[k]);
            }
            isUpdated[k] = true;
        }
    };

    // [l, r) <= dat
    void update(int l, int r, Laz dat) {
        update(0, 0, size, l, r, dat);
    };
    Monoid update(int k, int nl, int nr, int ql, int qr, Laz dat) {
        propagation(k, nr - nl);

        if (nr <= ql || qr <= nl) return seg[k];

        if (ql <= nl && nr <= qr) {
            lazy[k] = dat;
            isUpdated[k] = false;
            propagation(k, nr - nl);
            return seg[k];
        }
        else {
            seg[k] = mergeMonoid(update(2 * k + 1, nl, (nl + nr) / 2, ql, qr, dat),
                                 update(2 * k + 2, (nl + nr) / 2, nr, ql, qr, dat));
            return seg[k];
        }
    };

    // [l, r)
    Monoid fold(int l, int r) {
        return fold(0, 0, size, l, r);
    };
    Monoid fold(int k, int nl, int nr, int ql, int qr) {

        propagation(k, nr - nl);
        
        if (nr <= ql || qr <= nl) return e;

        if (ql <= nl && nr <= qr) return seg[k];
        else return mergeMonoid(fold(2 * k + 1, nl, (nl + nr) / 2, ql, qr),
                                fold(2 * k + 2, (nl + nr) / 2, nr, ql, qr));
    };

    Monoid operator [](const int &k) {
        return fold(k, k + 1);
    };
};
//===

int main() {
    cin >> n;
    tree.resize(n + 1);
    for (int i = 0; i < n - 1; i++) {
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    HeavyLightDecomposition<UnWeightedGraph> hld(tree);
    LazySegmentTree<llong, llong> seg(n + 1, 0,
                                      [](auto l, auto r){return l + r;}
                                      [](auto l, auto r, int len){return r;}
                                      [](auto l, auto r){return l + r;});

    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> u[i] >> v[i];
    }

    llong ans = 0;
    for (int i = 1; i < (1 << m); i++) {
        seg.update(0, n, 0);
        
        int bit = i;
        int cnt = 0;
        int pop = 0;
        while (bit) {
            if (bit & 1) {
                pop;
                hld.add(u, v, [&](int a, int b){seg.update(a, b, 1);});
            }
            
            bit >>= 1;
            cnt++;
        }

        if (pop & 1) {
            ans += 1 << (n - seg.fold(0, n) - 1);
        }
        else {
            ans -= 1 << (n - seg.fold(0, n) - 1);
        }
    }
    
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:186:12: error: 'n' was not declared in this scope; did you mean 'yn'?
  186 |     cin >> n;
      |            ^
      |            yn
a.cc:187:5: error: 'tree' was not declared in this scope; did you mean 'free'?
  187 |     tree.resize(n + 1);
      |     ^~~~
      |     free
a.cc:189:16: error: 'a' was not declared in this scope
  189 |         cin >> a >> b;
      |                ^
a.cc:189:21: error: 'b' was not declared in this scope
  189 |         cin >> a >> b;
      |                     ^
a.cc:194:29: error: 'UnWeightedGraph' was not declared in this scope
  194 |     HeavyLightDecomposition<UnWeightedGraph> hld(tree);
      |                             ^~~~~~~~~~~~~~~
a.cc:194:44: error: template argument 1 is invalid
  194 |     HeavyLightDecomposition<UnWeightedGraph> hld(tree);
      |                                            ^
a.cc:197:40: error: expected primary-expression before ']' token
  197 |                                       [](auto l, auto r, int len){return r;}
      |                                        ^
a.cc:197:42: error: expected primary-expression before 'auto'
  197 |                                       [](auto l, auto r, int len){return r;}
      |                                          ^~~~
a.cc:197:50: error: expected primary-expression before 'auto'
  197 |                                       [](auto l, auto r, int len){return r;}
      |                                                  ^~~~
a.cc:197:58: error: expected primary-expression before 'int'
  197 |                                       [](auto l, auto r, int len){return r;}
      |                                                          ^~~
a.cc:200:12: error: 'm' was not declared in this scope; did you mean 'tm'?
  200 |     cin >> m;
      |            ^
      |            tm
a.cc:202:16: error: 'u' was not declared in this scope
  202 |         cin >> u[i] >> v[i];
      |                ^
a.cc:202:24: error: 'v' was not declared in this scope
  202 |         cin >> u[i] >> v[i];
      |                        ^
a.cc:215:21: error: request for member 'add' in 'hld', which is of non-class type 'int'
  215 |                 hld.add(u, v, [&](int a, int b){seg.update(a, b, 1);});
      |                     ^~~
a.cc:215:25: error: 'u' was not declared in this scope
  215 |                 hld.add(u, v, [&](int a, int b){seg.update(a, b, 1);});
      |                         ^
a.cc:215:28: error: 'v' was not declared in this scope
  215 |                 hld.add(u, v, [&](int a, int b){seg.update(a, b, 1);});
      |                            ^