Problem p02794 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
vector<int> g[maxn];
int f[maxn], u[maxn], v[maxn], d[maxn];
ll p[100];
bool vis[maxn];
void dfs(int x, int fa) {
  f[x] = fa;
  for (int y : g[x]) {
    if (y == fa)
      continue;
    d[y] = d[x] + 1;
    dfs(y, x);
  }
}
ll lca(int x, int y) {
  ll s = 0;
  if (d[x] < d[y])
    swap(x, y);
  //	cout<<"!!!!!!!!"<<x<<" "<<y<<endl;
  while (d[x] > d[y]) {
    vis[x] = 1;
    s ^= 1LL << x;
    x = f[x];
  }
  if (x == y)
    return s;
  while (x != y) {
    vis[x] = 1;
    s ^= 1LL << x;
    x = f[x];

    vis[y] = 1;
    s ^= 1LL << y;
    y = f[y];
  }
  return s;
}
ll mask[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].pb(y);
    g[y].pb(x);
  }
  d[1] = 1;
  dfs(1, 0);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &u[i], &v[i]);
    mask[i] = lca(u[i], v[i]);
    //		cout<<"->"<<i<<" "<<mask[i]<<endl;
  }
  p[0] = 1;
  for (int i = 1; i <= 50; i++)
    p[i] = p[i - 1] * 2;
  ll ans = 0;
  for (int i = 0; i < (1 << m); i++) {
    memset(vis, 0, sizeof(vis));
    int op = 1;
    ll cur = 0;
    for (int j = 0; j < m; j++)
      if (i & (1 << j)) {
        op = -op;
        cur |= mask[j];
      }
    int cnt = 0;
    for (int j = 2; j <= n; j++)
      if ((cur & (1LL << j)) == 0)
        cnt++;
    ans += op * p[cnt];
    //		cout<<"!"<<i<<" "<<cnt<<" "<<cur<<endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
vector<int> g[maxn];
int f[maxn], u[maxn], v[maxn], d[maxn];
ll p[100];
bool vis[100];
void dfs(int x, int fa) {
  f[x] = fa;
  for (int y : g[x]) {
    if (y == fa)
      continue;
    d[y] = d[x] + 1;
    dfs(y, x);
  }
}
ll lca(int x, int y) {
  ll s = 0;
  if (d[x] < d[y])
    swap(x, y);
  //	cout<<"!!!!!!!!"<<x<<" "<<y<<endl;
  while (d[x] > d[y]) {
    vis[x] = 1;
    s ^= 1LL << x;
    x = f[x];
  }
  if (x == y)
    return s;
  while (x != y) {
    vis[x] = 1;
    s ^= 1LL << x;
    x = f[x];

    vis[y] = 1;
    s ^= 1LL << y;
    y = f[y];
  }
  return s;
}
ll mask[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].pb(y);
    g[y].pb(x);
  }
  d[1] = 1;
  dfs(1, 0);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &u[i], &v[i]);
    mask[i] = lca(u[i], v[i]);
    //		cout<<"->"<<i<<" "<<mask[i]<<endl;
  }
  p[0] = 1;
  for (int i = 1; i <= 50; i++)
    p[i] = p[i - 1] * 2;
  ll ans = 0;
  for (int i = 0; i < (1 << m); i++) {
    memset(vis, 0, sizeof(vis));
    int op = 1;
    ll cur = 0;
    for (int j = 0; j < m; j++)
      if (i & (1 << j)) {
        op = -op;
        cur |= mask[j];
      }
    int cnt = 0;
    for (int j = 2; j <= n; j++)
      if ((cur & (1LL << j)) == 0)
        cnt++;
    ans += op * p[cnt];
    //		cout<<"!"<<i<<" "<<cnt<<" "<<cur<<endl;
  }
  cout << ans << endl;
  return 0;
}