Problem p02794 (C++)

Buggy submission (status: Runtime Error):
// QWsin
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define out(i, u) for (int i = first[u]; i != -1; i = nxt[i])
#define repvc(i, vc) for (int i = 0, Sz = vc.size(); i < Sz; ++i)
using namespace std;
inline int read() {
  char ch = getchar();
  int ret = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ret = ret * 10 + ch - '0';
  return ret * f;
}

typedef long long ll;
const int INF = 1 << 30;

const int maxn = 50 + 10;
const int maxm = 100 + 10;

int first[maxn], nxt[maxn], ecnt;
struct Edge {
  int u, v;
  Edge(int u = 0, int v = 0) : u(u), v(v) {}
} e[maxm];
void add_edge(int u, int v) {
  nxt[ecnt] = first[u];
  first[u] = ecnt;
  e[ecnt++] = Edge(u, v);
  nxt[ecnt] = first[v];
  first[v] = ecnt;
  e[ecnt++] = Edge(v, u);
}

int d[maxn];
int bit(int x) { return 1 << x; }

int dep[maxn], fa[maxn];
void dfs(int u, int pre) {
  dep[u] = dep[pre] + 1;
  fa[u] = pre;
  for (int i = first[u]; i != -1; i = nxt[i]) {
    int v = e[i].v;
    if (v == pre)
      continue;
    dfs(v, u);
  }
}

int LCA(int a, int b) {
  if (dep[a] > dep[b])
    swap(a, b);
  while (dep[a] < dep[b])
    b = fa[b];
  while (a != b)
    a = fa[a], b = fa[b];
  return a;
}

struct LIM {
  int a, b, lca;
  void input() {
    cin >> a >> b;
    lca = LCA(a, b);
  }
} lim[maxn];
int tag = 0;
int dfs2(int u) {
  int ret = d[u];
  for (int i = first[u]; i != -1; i = nxt[i]) {
    int v = e[i].v;
    if (v == fa[u])
      continue;
    int t = dfs2(v);
    if (t > 0)
      ++tag;
    ret += t;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  rep(i, 1, n) first[i] = -1;
  rep(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    add_edge(u, v);
  }
  int m;
  cin >> m;
  dfs(1, 1);
  rep(i, 1, m) lim[i].input();

  ll ans = 1ll << (n - 1);
  for (int S = 1; S < (1 << m); ++S) {
    int v = 1;
    rep(i, 1, n) d[i] = 0;
    for (int i = 1; i <= m; ++i)
      if (bit(i - 1) & S) {
        v *= -1;
        ++d[lim[i].a];
        ++d[lim[i].b];
        d[lim[i].lca] -= 2;
      }
    tag = 0;
    dfs2(1);
    ans += v * (1ll << (n - 1 - tag));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// QWsin
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define out(i, u) for (int i = first[u]; i != -1; i = nxt[i])
#define repvc(i, vc) for (int i = 0, Sz = vc.size(); i < Sz; ++i)
using namespace std;
inline int read() {
  char ch = getchar();
  int ret = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ret = ret * 10 + ch - '0';
  return ret * f;
}

typedef long long ll;
const int INF = 1 << 30;

const int maxn = 50 + 10;
const int maxm = 100 + 10;

int first[maxn], nxt[maxm], ecnt;
struct Edge {
  int u, v;
  Edge(int u = 0, int v = 0) : u(u), v(v) {}
} e[maxm];
void add_edge(int u, int v) {
  nxt[ecnt] = first[u];
  first[u] = ecnt;
  e[ecnt++] = Edge(u, v);
  nxt[ecnt] = first[v];
  first[v] = ecnt;
  e[ecnt++] = Edge(v, u);
}

int d[maxn];
int bit(int x) { return 1 << x; }

int dep[maxn], fa[maxn];
void dfs(int u, int pre) {
  dep[u] = dep[pre] + 1;
  fa[u] = pre;
  for (int i = first[u]; i != -1; i = nxt[i]) {
    int v = e[i].v;
    if (v == pre)
      continue;
    dfs(v, u);
  }
}

int LCA(int a, int b) {
  if (dep[a] > dep[b])
    swap(a, b);
  while (dep[a] < dep[b])
    b = fa[b];
  while (a != b)
    a = fa[a], b = fa[b];
  return a;
}

struct LIM {
  int a, b, lca;
  void input() {
    cin >> a >> b;
    lca = LCA(a, b);
  }
} lim[maxn];
int tag = 0;
int dfs2(int u) {
  int ret = d[u];
  for (int i = first[u]; i != -1; i = nxt[i]) {
    int v = e[i].v;
    if (v == fa[u])
      continue;
    int t = dfs2(v);
    if (t > 0)
      ++tag;
    ret += t;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  rep(i, 1, n) first[i] = -1;
  rep(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    add_edge(u, v);
  }
  int m;
  cin >> m;
  dfs(1, 1);
  rep(i, 1, m) lim[i].input();

  ll ans = 1ll << (n - 1);
  for (int S = 1; S < (1 << m); ++S) {
    int v = 1;
    rep(i, 1, n) d[i] = 0;
    for (int i = 1; i <= m; ++i)
      if (bit(i - 1) & S) {
        v *= -1;
        ++d[lim[i].a];
        ++d[lim[i].b];
        d[lim[i].lca] -= 2;
      }
    tag = 0;
    dfs2(1);
    ans += v * (1ll << (n - 1 - tag));
  }
  cout << ans << endl;
  return 0;
}