Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
using ll = long long;
using Graph = vector<vector<int>>;

vector<pair<int, int>> ab;
map<pair<int, int>, int> m;

bool dfs(Graph &G, int v, int p, int g, ll &c) {
  for (auto nv : G.at(v)) {
    if (nv == p) {
      continue;
    }
    if (nv == g) {
      ll k = m[make_pair(min(v, nv), max(v, nv))];
      c = 1ull << k;
      return true;
    }
    if (dfs(G, nv, v, g, c)) {
      ll k = m[make_pair(min(v, nv), max(v, nv))];
      c += (1ull << k);
      return true;
    }
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  Graph G(N);
  ab.resize(N - 1);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ab.at(i) = make_pair(min(x, y), max(x, y));
    m[ab.at(i)] = i;
    G.at(x).push_back(y);
    G.at(y).push_back(x);
  }
  int M;
  cin >> M;
  vector<int> u(M), v(M);
  for (int i = 0; i < M; i++) {
    cin >> u.at(i) >> v.at(i);
    u.at(i)--;
    v.at(i)--;
  }

  vector<ll> constrain(M);
  for (int i = 0; i < M; i++) {
    dfs(G, u.at(i), -1, v.at(i), constrain.at(i));
  }

  ll ans = 1ull << ((ll)N - 1);
  for (int j = 1; j < 1 << M; j++) {
    ll bit = 0;
    ll count = 0;
    for (int i = 0; i < N; i++) {
      if (((j >> i) & 1) == 1) {
        count++;
        bit |= constrain.at(i);
      }
    }
    ll C = 0;
    for (int i = 0; i < 64; i++) {
      C += bit % 2;
      bit >>= 1;
    }
    if (count % 2 == 0) {
      ans += 1ull << (N - 1 - C);
    } else {
      ans -= 1ull << (N - 1 - C);
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
using ll = long long;
using Graph = vector<vector<int>>;

vector<pair<int, int>> ab;
map<pair<int, int>, int> m;

bool dfs(Graph &G, int v, int p, int g, ll &c) {
  for (auto nv : G.at(v)) {
    if (nv == p) {
      continue;
    }
    if (nv == g) {
      ll k = m[make_pair(min(v, nv), max(v, nv))];
      c = 1ull << k;
      return true;
    }
    if (dfs(G, nv, v, g, c)) {
      ll k = m[make_pair(min(v, nv), max(v, nv))];
      c += (1ull << k);
      return true;
    }
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  Graph G(N);
  ab.resize(N - 1);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ab.at(i) = make_pair(min(x, y), max(x, y));
    m[ab.at(i)] = i;
    G.at(x).push_back(y);
    G.at(y).push_back(x);
  }
  int M;
  cin >> M;
  vector<int> u(M), v(M);
  for (int i = 0; i < M; i++) {
    cin >> u.at(i) >> v.at(i);
    u.at(i)--;
    v.at(i)--;
  }

  vector<ll> constrain(M);
  for (int i = 0; i < M; i++) {
    dfs(G, u.at(i), -1, v.at(i), constrain.at(i));
  }

  ll ans = 1ull << ((ll)N - 1);
  for (int j = 1; j < 1 << M; j++) {
    ll bit = 0;
    ll count = 0;
    for (int i = 0; i < M; i++) {
      if (((j >> i) & 1) == 1) {
        count++;
        bit |= constrain.at(i);
      }
    }
    ll C = 0;
    for (int i = 0; i < 64; i++) {
      C += bit % 2;
      bit >>= 1;
    }
    if (count % 2 == 0) {
      ans += 1ull << (N - 1 - C);
    } else {
      ans -= 1ull << (N - 1 - C);
    }
  }

  cout << ans << endl;
}