Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

vector<bitset<100>> p;
vector<vector<pair<i64, i64>>> G;

bool dfs(i64 v, i64 f, i64 g, i64 i) {
  if (v == g) {
    return true;
  }
  for (auto e : G[v]) {
    i64 t = e.first;
    i64 ei = e.second;
    if (t == f)
      continue;
    if (dfs(t, v, g, i)) {
      p[i] |= (1 << ei);
      return true;
    }
  }
  return false;
}

int main() {
  i64 N;
  cin >> N;
  G.resize(N);
  rep(i, 0, N - 1) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({b, i});
    G[b].push_back({a, i});
  }
  i64 M;
  cin >> M;
  p.resize(M);
  vector<i64> U(M), V(M);
  rep(i, 0, M) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    dfs(U[i], -1, V[i], i);
  }

  i64 ans = 0;
  vector<i64> cnt(N);
  for (auto s = 1; s < (1 << M); s++) {
    bitset<100> bit;
    rep(i, 0, M) {
      if (s & (1 << i)) {
        bit |= p[i];
      }
    }
    if (__builtin_popcountll(s) & 1)
      cnt[N - 1 - bit.count()]++;
    else
      cnt[N - 1 - bit.count()]--;
  }
  for (i64 i = 0; i < N; i++) {
    ans += (1ll << i) * cnt[i];
  }
  cout << (1ll << (N - 1)) - ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

vector<bitset<100>> p;
vector<vector<pair<i64, i64>>> G;

bool dfs(i64 v, i64 f, i64 g, i64 i) {
  if (v == g) {
    return true;
  }
  for (auto e : G[v]) {
    i64 t = e.first;
    i64 ei = e.second;
    if (t == f)
      continue;
    if (dfs(t, v, g, i)) {
      p[i] |= (1ll << ei);
      return true;
    }
  }
  return false;
}

int main() {
  i64 N;
  cin >> N;
  G.resize(N);
  rep(i, 0, N - 1) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({b, i});
    G[b].push_back({a, i});
  }
  i64 M;
  cin >> M;
  p.resize(M);
  vector<i64> U(M), V(M);
  rep(i, 0, M) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    dfs(U[i], -1, V[i], i);
  }

  i64 ans = 0;
  vector<i64> cnt(N);
  for (auto s = 1; s < (1 << M); s++) {
    bitset<100> bit;
    rep(i, 0, M) {
      if (s & (1 << i)) {
        bit |= p[i];
      }
    }
    if (__builtin_popcountll(s) & 1)
      cnt[N - 1 - bit.count()]++;
    else
      cnt[N - 1 - bit.count()]--;
  }
  for (i64 i = 0; i < N; i++) {
    ans += (1ll << i) * cnt[i];
  }
  cout << (1ll << (N - 1)) - ans << endl;
}