Problem p02794 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp)                                                             \
  cerr << #mp << ":" << endl;                                                  \
  for (auto i = mp.begin(); i != mp.end(); ++i) {                              \
    cerr << i->first << ":" << i->second << endl;                              \
  }
#define dbgarr(n, m, arr)                                                      \
  rep(i, n) {                                                                  \
    rep(j, m) { cerr << arr[i][j] << " "; }                                    \
    cerr << endl;                                                              \
  }
#define dbgdp(n, arr)                                                          \
  rep(i, n) { cerr << arr[i] << " "; }                                         \
  cerr << endl;
#define dbgmint(n, arr)                                                        \
  rep(i, n) { cerr << arr[i].x << " "; }                                       \
  cerr << endl;
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}

template <typename T> istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "	";
  }
  return s;
}

// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

// LCA
template <typename G> struct DoublingLowestCommonAncestor {
  const int LOG;
  vector<int> dep;
  const G &g;
  vector<vector<int>> table;

  DoublingLowestCommonAncestor(const G &g)
      : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector<int>(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for (auto &to : g[idx]) {
      if (to != par)
        dfs(to, idx, d + 1);
    }
  }

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  ll n;
  cin >> n;
  VV g(n);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  // dbg(g);

  vector<ll> dist(n);
  auto dfs = [&](auto dfs, ll v, ll parent) -> void {
    Each(x, g[v]) {
      if (x == parent)
        continue;
      dist[x] = dist[v] + 1;
      dfs(dfs, x, v);
    }
  };
  ll m;
  cin >> m;
  Vec u(m);
  Vec v(m);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u[i] = a;
    v[i] = b;
  }
  DoublingLowestCommonAncestor<VV> LCA(g);
  LCA.build(0);
  ll ans = pow(2, n - 1);
  Vec lca(m);
  rep(i, m) { lca[i] = LCA.query(u[i], v[i]); }
  REP(bit, 1, (1LL << m)) {
    ll tmp = 0;
    Vec add(n);
    rep(i, m) {
      if (bit & (1LL << i)) {
        add[lca[i]] -= 2;
        add[u[i]]++;
        add[v[i]]++;
      }
    }
    dbg(add) auto dfs = [&](auto dfs, ll v, ll parent) -> ll {
      ll res = 0;
      Each(x, g[v]) {
        if (x == parent)
          continue;
        res += dfs(dfs, x, v);
        add[v] += add[x];
      }
      return res + (add[v] > 0);
    };
    tmp = dfs(dfs, 0, -1);
    ll sum = pow(2, n - 1 - tmp);
    if (__builtin_popcount(bit) % 2 == 1) {
      sum *= -1;
    }
    // dbg(sum);
    ans += sum;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp)                                                             \
  cerr << #mp << ":" << endl;                                                  \
  for (auto i = mp.begin(); i != mp.end(); ++i) {                              \
    cerr << i->first << ":" << i->second << endl;                              \
  }
#define dbgarr(n, m, arr)                                                      \
  rep(i, n) {                                                                  \
    rep(j, m) { cerr << arr[i][j] << " "; }                                    \
    cerr << endl;                                                              \
  }
#define dbgdp(n, arr)                                                          \
  rep(i, n) { cerr << arr[i] << " "; }                                         \
  cerr << endl;
#define dbgmint(n, arr)                                                        \
  rep(i, n) { cerr << arr[i].x << " "; }                                       \
  cerr << endl;
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}

template <typename T> istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "	";
  }
  return s;
}

// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

// LCA
template <typename G> struct DoublingLowestCommonAncestor {
  const int LOG;
  vector<int> dep;
  const G &g;
  vector<vector<int>> table;

  DoublingLowestCommonAncestor(const G &g)
      : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector<int>(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for (auto &to : g[idx]) {
      if (to != par)
        dfs(to, idx, d + 1);
    }
  }

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  ll n;
  cin >> n;
  VV g(n);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  // dbg(g);

  vector<ll> dist(n);
  auto dfs = [&](auto dfs, ll v, ll parent) -> void {
    Each(x, g[v]) {
      if (x == parent)
        continue;
      dist[x] = dist[v] + 1;
      dfs(dfs, x, v);
    }
  };
  ll m;
  cin >> m;
  Vec u(m);
  Vec v(m);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u[i] = a;
    v[i] = b;
  }
  DoublingLowestCommonAncestor<VV> LCA(g);
  LCA.build(0);
  ll ans = pow(2, n - 1);
  Vec lca(m);
  rep(i, m) { lca[i] = LCA.query(u[i], v[i]); }
  REP(bit, 1, (1LL << m)) {
    ll tmp = 0;
    Vec add(n);
    rep(i, m) {
      if (bit & (1LL << i)) {
        add[lca[i]] -= 2;
        add[u[i]]++;
        add[v[i]]++;
      }
    }
    // dbg(add)
    auto dfs = [&](auto dfs, ll v, ll parent) -> ll {
      ll res = 0;
      Each(x, g[v]) {
        if (x == parent)
          continue;
        res += dfs(dfs, x, v);
        add[v] += add[x];
      }
      return res + (add[v] > 0);
    };
    tmp = dfs(dfs, 0, -1);
    ll sum = pow(2, n - 1 - tmp);
    if (__builtin_popcount(bit) % 2 == 1) {
      sum *= -1;
    }
    // dbg(sum);
    ans += sum;
  }
  cout << ans << endl;
  return 0;
}