Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();

int main() {
  int n, m, a, b, src, dst;
  cin >> n;
  unordered_map<int, vector<int>> e;
  map<pair<int, int>, int> mm;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
    if (a > b)
      swap(a, b);
    mm[{a, b}] = i;
  }
  vector<ll> rules;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> src >> dst;
    vector<int> parent(n, 0);
    vector<bool> visit(n, false);
    visit[src] = true;
    parent[src] = src;
    vector<int> dq = {src};
    while (!dq.empty()) {
      vector<int> new_dq;
      for (int node : dq) {
        for (int neibor : e[node]) {
          if (visit[neibor] == false) {
            visit[neibor] = true;
            parent[neibor] = node;
            new_dq.push_back(neibor);
          }
        }
      }
      new_dq.swap(dq);
    }
    int cur = dst;
    ll num = 0;
    while (cur != src) {
      a = cur;
      b = parent[cur];
      if (a > b)
        swap(a, b);
      num = num | (1LL << mm[{a, b}]);
      cur = parent[cur];
    }
    rules.push_back(num);
  }
  // inclusive - exclusive
  ll ans = 0;
  for (int state = 0; state < (1 << m); state++) {
    ll x = state, bit_cnt = 0;
    while (x > 0) {
      x -= x & (-x);
      bit_cnt += 1;
    }
    ll mask = 0;
    for (int j = 0; j < m; j++) {
      if (state >> j & 1) {
        mask = mask | rules[j];
      }
    }
    x = mask;
    ll bit_cnt2 = 0;
    while (x > 0) {
      x -= x & (-x);
      bit_cnt2 += 1;
    }
    ll comb = 1LL << (n - 1 - bit_cnt2);
    if (bit_cnt % 2 == 1)
      ans -= comb;
    else
      ans += comb;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();

int main() {
  int n, m, a, b, src, dst;
  cin >> n;
  unordered_map<int, vector<int>> e;
  map<pair<int, int>, int> mm;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
    if (a > b)
      swap(a, b);
    mm[{a, b}] = i;
  }
  vector<ll> rules;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> src >> dst;
    vector<int> parent(n + 1, 0);
    vector<bool> visit(n + 1, false);
    visit[src] = true;
    parent[src] = src;
    vector<int> dq = {src};
    while (!dq.empty()) {
      vector<int> new_dq;
      for (int node : dq) {
        for (int neibor : e[node]) {
          if (visit[neibor] == false) {
            visit[neibor] = true;
            parent[neibor] = node;
            new_dq.push_back(neibor);
          }
        }
      }
      new_dq.swap(dq);
    }
    int cur = dst;
    ll num = 0;
    while (cur != src) {
      a = cur;
      b = parent[cur];
      if (a > b)
        swap(a, b);
      num = num | (1LL << mm[{a, b}]);
      cur = parent[cur];
    }
    rules.push_back(num);
  }
  // inclusive - exclusive
  ll ans = 0;
  for (int state = 0; state < (1 << m); state++) {
    ll x = state, bit_cnt = 0;
    while (x > 0) {
      x -= x & (-x);
      bit_cnt += 1;
    }
    ll mask = 0;
    for (int j = 0; j < m; j++) {
      if (state >> j & 1) {
        mask = mask | rules[j];
      }
    }
    x = mask;
    ll bit_cnt2 = 0;
    while (x > 0) {
      x -= x & (-x);
      bit_cnt2 += 1;
    }
    ll comb = 1LL << (n - 1 - bit_cnt2);
    if (bit_cnt % 2 == 1)
      ans -= comb;
    else
      ans += comb;
  }
  cout << ans << endl;
  return 0;
}