Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)

const int MaxN = 5e5 + 10;
const ll mod = 1ll << 62;

struct edge {
  int next, to;
};

edge e[MaxN << 1];
int n, m, cnt;
int head[MaxN], f[MaxN], u[MaxN], v[MaxN];
std::bitset<510> q[25], t[1 << 16];

ll fast_pow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod, b >>= 1;
  }
  return res;
}

void add_edge(int u, int v) {
  ++cnt;
  e[cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}

void dfs(int u, int fa) {
  f[u] = fa;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dfs(v, u);
  }
}

void make(int x, int y, int p) {
  for (int i = x; i != 1; i = f[i])
    q[p].set(i, 1);
  int cur = y;
  for (; cur != 1 && !q[p][cur]; cur = f[cur])
    q[p].set(cur, 1);
  for (; cur != 1; cur = f[cur])
    q[p].set(cur, 0);
}

inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}

int main() {
  ll ans = 0;
  n = read();
  for (int i = 1; i <= n - 1; i++) {
    int u = read(), v = read();
    add_edge(u, v), add_edge(v, u);
  }
  dfs(1, 0), m = read();
  for (int i = 1; i <= m; i++) {
    u[i] = read(), v[i] = read();
    make(u[i], v[i], i);
  }
  for (int i = 0; i < (1 << m); i++) {
    int cnt = 0, cur = 0;
    for (int j = 0; j < m; j++)
      if (i & (1 << j))
        cnt++, cur = j + 1;
    if (cnt)
      t[i] = t[i ^ (1 << (cur - 1))] | q[cur];
    if (cnt & 1)
      ans = sum(ans, mod - fast_pow(2, n - 1 - t[i].count()), mod);
    else
      ans = sum(ans, fast_pow(2, n - 1 - t[i].count()), mod);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)

const int MaxN = 5e5 + 10;
const ll mod = 1ll << 62;

struct edge {
  int next, to;
};

edge e[MaxN << 1];
int n, m, cnt;
int head[MaxN], f[MaxN], u[MaxN], v[MaxN];
std::bitset<510> q[25], t[1 << 20];

ll fast_pow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod, b >>= 1;
  }
  return res;
}

void add_edge(int u, int v) {
  ++cnt;
  e[cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}

void dfs(int u, int fa) {
  f[u] = fa;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dfs(v, u);
  }
}

void make(int x, int y, int p) {
  for (int i = x; i != 1; i = f[i])
    q[p].set(i, 1);
  int cur = y;
  for (; cur != 1 && !q[p][cur]; cur = f[cur])
    q[p].set(cur, 1);
  for (; cur != 1; cur = f[cur])
    q[p].set(cur, 0);
}

inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}

int main() {
  ll ans = 0;
  n = read();
  for (int i = 1; i <= n - 1; i++) {
    int u = read(), v = read();
    add_edge(u, v), add_edge(v, u);
  }
  dfs(1, 0), m = read();
  for (int i = 1; i <= m; i++) {
    u[i] = read(), v[i] = read();
    make(u[i], v[i], i);
  }
  for (int i = 0; i < (1 << m); i++) {
    int cnt = 0, cur = 0;
    for (int j = 0; j < m; j++)
      if (i & (1 << j))
        cnt++, cur = j + 1;
    if (cnt)
      t[i] = t[i ^ (1 << (cur - 1))] | q[cur];
    if (cnt & 1)
      ans = sum(ans, mod - fast_pow(2, n - 1 - t[i].count()), mod);
    else
      ans = sum(ans, fast_pow(2, n - 1 - t[i].count()), mod);
  }
  printf("%lld", ans);
  return 0;
}