Problem p02794 (C++)

Buggy submission (status: Runtime Error):
// Link : https://atcoder.jp/contests/abc152/tasks/abc152_f

#include <bits/stdc++.h>
using namespace std;
#define ll long long

#define N 51
#define M 8
vector<int> edge[N];
int a[N], b[N];

int anc[N];
int parEdge[N], depth[N];

void dfs(int node, int par) {
  for (int i = 0; i < edge[node].size(); ++i) {
    int eid = edge[node][i];
    int ch = a[eid] ^ b[eid] ^ node;
    if (ch == par) {
      continue;
    }
    anc[ch] = node;
    depth[ch] = depth[node] + 1;
    parEdge[ch] = eid;
    dfs(ch, node);
  }
}

#define EDGE 20
int x[EDGE], y[EDGE];
bool isColored[EDGE];
void paint(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  int diff = depth[u] - depth[v];
  for (int i = 0; i < diff; ++i) {
    isColored[parEdge[u]] = true;
    u = anc[u];
  }
  while (u != v) {
    isColored[parEdge[u]] = true;
    isColored[parEdge[v]] = true;
    u = anc[u];
    v = anc[v];
  }
}
ll color(int bit, int n, int m) {
  ll ret = 1;
  memset(isColored, 0, sizeof(isColored));
  for (int i = 0; i < m; ++i) {
    if (bit & (1 << i)) {
      paint(x[i], y[i]);
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    if (!isColored[i]) {
      ret = ret * 2;
    }
  }
  return ret;
}
int bitCount(int x) {
  int ret = 0;
  while (x > 0) {
    ++ret;
    x = x & (x - 1);
  }
  return ret;
}
void solve() {
  int n;
  scanf("%d ", &n);
  ll ret = 1;
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d %d ", &a[i], &b[i]);
    edge[a[i]].push_back(i);
    edge[b[i]].push_back(i);
    ret *= 2;
  }
  dfs(1, 0);
  int m;
  scanf("%d ", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d ", &x[i], &y[i]);
    paint(x[i], y[i]);
  }
  for (int i = 1; i < (1 << m); ++i) {
    int bc = bitCount(i);
    if (bc & 1) {
      ret -= color(i, n, m);
    } else {
      ret += color(i, n, m);
    }
  }
  printf("%lld\n", ret);
}
int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}


Fixed submission:
// Link : https://atcoder.jp/contests/abc152/tasks/abc152_f

#include <bits/stdc++.h>
using namespace std;
#define ll long long

#define N 51
#define M 8
vector<int> edge[N];
int a[N], b[N];

int anc[N];
int parEdge[N], depth[N];

void dfs(int node, int par) {
  for (int i = 0; i < edge[node].size(); ++i) {
    int eid = edge[node][i];
    int ch = a[eid] ^ b[eid] ^ node;
    if (ch == par) {
      continue;
    }
    anc[ch] = node;
    depth[ch] = depth[node] + 1;
    parEdge[ch] = eid;
    dfs(ch, node);
  }
}

#define EDGE 20
int x[EDGE], y[EDGE];
bool isColored[N];
void paint(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  int diff = depth[u] - depth[v];
  for (int i = 0; i < diff; ++i) {
    isColored[parEdge[u]] = true;
    u = anc[u];
  }
  while (u != v) {
    isColored[parEdge[u]] = true;
    isColored[parEdge[v]] = true;
    u = anc[u];
    v = anc[v];
  }
}
ll color(int bit, int n, int m) {
  ll ret = 1;
  memset(isColored, 0, sizeof(isColored));
  for (int i = 0; i < m; ++i) {
    if (bit & (1 << i)) {
      paint(x[i], y[i]);
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    if (!isColored[i]) {
      ret = ret * 2;
    }
  }
  return ret;
}
int bitCount(int x) {
  int ret = 0;
  while (x > 0) {
    ++ret;
    x = x & (x - 1);
  }
  return ret;
}
void solve() {
  int n;
  scanf("%d ", &n);
  ll ret = 1;
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d %d ", &a[i], &b[i]);
    edge[a[i]].push_back(i);
    edge[b[i]].push_back(i);
    ret *= 2;
  }
  dfs(1, 0);
  int m;
  scanf("%d ", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d ", &x[i], &y[i]);
    paint(x[i], y[i]);
  }
  for (int i = 1; i < (1 << m); ++i) {
    int bc = bitCount(i);
    if (bc & 1) {
      ret -= color(i, n, m);
    } else {
      ret += color(i, n, m);
    }
  }
  printf("%lld\n", ret);
}
int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}