Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;

int n, m;
vector<ii> al[100];
ii path[100];

bool onpath[100][100];

int depth[100];
int parent[100];
int edge[100];

void dfs(int i, int p) {
  for (auto &it : al[i]) {
    if (it.first == p)
      continue;
    depth[it.first] = depth[i] + 1;
    parent[it.first] = i;
    edge[it.first] = it.second;
    dfs(it.first, i);
  }
}

void __path(int i, int j, int k) {
  if (depth[i] < depth[j])
    swap(i, j);

  while (depth[i] != depth[j]) {
    onpath[k][edge[i]] = true;
    i = parent[i];
  }

  while (i != j) {
    onpath[k][edge[i]] = true;
    i = parent[i];
    onpath[k][edge[j]] = true;
    j = parent[j];
  }
}

long long memo[2][150000];

int main() {
  cin >> n;

  int a, b;
  for (int x = 0; x < n - 1; x++) {
    cin >> a >> b;
    al[a].push_back(ii(b, x));
    al[b].push_back(ii(a, x));
  }

  cin >> m;
  for (int x = 0; x < m; x++) {
    cin >> path[x].first >> path[x].second;
  }

  dfs(1, -1);

  for (int x = 0; x < m; x++) {
    __path(path[x].first, path[x].second, x);
  }

  memo[0][0] = 1;
  a = 0, b = 1;
  int temp;
  for (int x = 0; x < n - 1; x++) {
    memset(memo[b], 0, sizeof(memo[b]));

    for (int bm = 0; bm < (1 << m); bm++) {
      memo[b][bm] += memo[a][bm];

      temp = bm;
      for (int y = 0; y < m; y++) {
        if (onpath[y][x])
          temp |= (1 << y);
      }

      memo[b][temp] += memo[a][bm];
    }
    swap(a, b);
  }

  printf("%lld\n", memo[a][(1 << m) - 1]);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;

int n, m;
vector<ii> al[100];
ii path[100];

bool onpath[100][100];

int depth[100];
int parent[100];
int edge[100];

void dfs(int i, int p) {
  for (auto &it : al[i]) {
    if (it.first == p)
      continue;
    depth[it.first] = depth[i] + 1;
    parent[it.first] = i;
    edge[it.first] = it.second;
    dfs(it.first, i);
  }
}

void __path(int i, int j, int k) {
  if (depth[i] < depth[j])
    swap(i, j);

  while (depth[i] != depth[j]) {
    onpath[k][edge[i]] = true;
    i = parent[i];
  }

  while (i != j) {
    onpath[k][edge[i]] = true;
    i = parent[i];
    onpath[k][edge[j]] = true;
    j = parent[j];
  }
}

long long memo[2][1500000];

int main() {
  cin >> n;

  int a, b;
  for (int x = 0; x < n - 1; x++) {
    cin >> a >> b;
    al[a].push_back(ii(b, x));
    al[b].push_back(ii(a, x));
  }

  cin >> m;
  for (int x = 0; x < m; x++) {
    cin >> path[x].first >> path[x].second;
  }

  dfs(1, -1);

  for (int x = 0; x < m; x++) {
    __path(path[x].first, path[x].second, x);
  }

  memo[0][0] = 1;
  a = 0, b = 1;
  int temp;
  for (int x = 0; x < n - 1; x++) {
    memset(memo[b], 0, sizeof(memo[b]));

    for (int bm = 0; bm < (1 << m); bm++) {
      memo[b][bm] += memo[a][bm];

      temp = bm;
      for (int y = 0; y < m; y++) {
        if (onpath[y][x])
          temp |= (1 << y);
      }

      memo[b][temp] += memo[a][bm];
    }
    swap(a, b);
  }

  printf("%lld\n", memo[a][(1 << m) - 1]);
}