Problem p02794 (C++)

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

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1};
int dx[] = {
    1,
    -1,
    0,
    0,
};

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}

ll lcm(int a, ll b) { return a * b / gcd(a, b); }

// 繰り返し二乗法
ll power(ll a, ll b) {
  if (a == 1)
    return a;
  // if(a==0)re
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

const int MAX = 1000000;

ll fact[MAX], fact_inv[MAX];

void init_fact(ll n) {
  fact[0] = 1;
  // 階乗の計算
  rep(i, n) fact[i + 1] = fact[i] * (i + 1) % MOD;
  fact_inv[n] = power(fact[n], MOD - 2);
  // 逆元の計算
  for (ll i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

ll perm(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % MOD; }

struct UnionFind {
  vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  vector<ll> num;
  // vector<ll> dist; // rootまでの距離

  UnionFind(ll N) : par(N), num(N) { // 最初は全てが根であるとして初期化
    for (ll i = 0; i < N; i++)
      par[i] = i;
    for (ll i = 0; i < N; i++)
      num[i] = 1;
    // for(ll i = 0; i < N; i++) dist[i] = 0;
  }

  ll root(ll x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) { // xとyの木を併合
    ll rx = root(x);       // xの根をrx
    ll ry = root(y);       // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    num[ry] = num[rx] + num[ry];
  }

  bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
    ll rx = root(x);
    ll ry = root(y);
    return rx == ry;
  }

  ll size(ll x) { return num[root(x)]; }
};

vint divisor(ll n) { // nの約数
  vint ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  vsort(ret);
  return ret;
}

ll get_digit_in(ll n, ll i) { // i桁目の数字を得る。
  for (ll j = 0; j < i - 1; j++) {
    n /= 10;
  }
  return n % 10;
}

ll get_digit(ll n) {
  ll rtn = 0;
  while (n > 0) {
    n /= 10;
    rtn++;
  }
  return rtn;
}

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> rtn;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      rtn[i]++;
      n /= i;
    }
  }
  if (n != 1)
    rtn[n] = 1;
  return rtn;
}

// nPnの順列に対して処理を実行する
void foreach_permutation(ll n, function<void(ll *)> f) {
  ll indexes[n];
  for (ll i = 0; i < n; i++)
    indexes[i] = i;
  do {
    f(indexes);
  } while (std::next_permutation(indexes, indexes + n));
}

void recursive_comb(ll *indexes, ll s, ll rest, function<void(ll *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(ll n, ll k, function<void(ll *)> f) {
  ll indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

// nPkの順列に対して処理を実行する
void foreach_permutation(ll n, ll k, function<void(ll *)> f) {
  foreach_comb(n, k, [&](ll *c_indexes) {
    foreach_permutation(k, [&](ll *p_indexes) {
      ll indexes[k];
      for (ll i = 0; i < k; i++) {
        indexes[i] = c_indexes[p_indexes[i]];
      }
      f(indexes);
    });
  });
}

void printv(vint &V) {
  for (auto e : V)
    cout << e << " ";
  cout << endl;
}

// map< int64_t, int > prime_factor(int64_t n) {
//   map< int64_t, int > ret;
//   for(int64_t i = 2; i * i <= n; i++) {
//     while(n % i == 0) {
//       ret[i]++;
//       n /= i;
//     }
//   }
//   if(n != 1) ret[n] = 1;
//   return ret;
// }

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<(int)(1e9 + 7)>;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

vvint edges;

bool bfs(ll now, ll p, ll s, ll g, ll i, vvint &mask) {
  if (now == g)
    return true;
  for (auto nxt : edges[now]) {
    // cout<<
    if (nxt == p)
      continue;
    if (bfs(nxt, now, s, g, i, mask)) {
      // puts("hey");
      // cout<<now<<":"<<nxt<<endl;
      mask[now][nxt] += (1 << i);
      mask[nxt][now] += (1 << i);
      return true;
    }
  }
  return false;
};

int main() {

  cout << fixed << setprecision(10);

  ll N;
  cin >> N;
  vint a(N - 1), b(N - 1);
  rep(i, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  ll M;
  cin >> M;
  vint u(M), v(M);
  rep(i, M) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
  }

  edges.resize(N);
  vvint mask(N, vint(N, 0));
  // map<pint, ll> m;
  rep(i, N - 1) {
    edges[a[i]].push_back(b[i]);
    edges[b[i]].push_back(a[i]);
    // m[mp(a[i], b[i])] = i;
    // m[mp(b[i], a[i])] = i;
  }

  rep(i, M) {
    ll s, g;
    s = u[i];
    g = v[i];

    bfs(s, -1, s, g, i, mask);
  }

  // rep(i,N){
  //   rep(j,N){
  //     cout<<mask[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }

  ll dp[N][(1 << M)];
  rep(i, N) {
    // rep(j,2){
    rep(k, (1 << M)) dp[i][k] = 0;
    // }
  }
  return 0;

  dp[0][0] = 1;

  rep(i, N - 1) {
    // cout<<mask[a[i]][b[i]]<<endl;

    rep(j, (1 << M)) {
      ll tmp = j | mask[a[i]][b[i]];

      dp[i + 1][tmp] += dp[i][j]; // 黒
      dp[i + 1][j] += dp[i][j];   // white
    }
  }

  cout << dp[N - 1][(1 << M) - 1] << endl;
}

//


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1};
int dx[] = {
    1,
    -1,
    0,
    0,
};

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}

ll lcm(int a, ll b) { return a * b / gcd(a, b); }

// 繰り返し二乗法
ll power(ll a, ll b) {
  if (a == 1)
    return a;
  // if(a==0)re
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

const int MAX = 1000000;

ll fact[MAX], fact_inv[MAX];

void init_fact(ll n) {
  fact[0] = 1;
  // 階乗の計算
  rep(i, n) fact[i + 1] = fact[i] * (i + 1) % MOD;
  fact_inv[n] = power(fact[n], MOD - 2);
  // 逆元の計算
  for (ll i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

ll perm(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % MOD; }

struct UnionFind {
  vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  vector<ll> num;
  // vector<ll> dist; // rootまでの距離

  UnionFind(ll N) : par(N), num(N) { // 最初は全てが根であるとして初期化
    for (ll i = 0; i < N; i++)
      par[i] = i;
    for (ll i = 0; i < N; i++)
      num[i] = 1;
    // for(ll i = 0; i < N; i++) dist[i] = 0;
  }

  ll root(ll x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) { // xとyの木を併合
    ll rx = root(x);       // xの根をrx
    ll ry = root(y);       // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    num[ry] = num[rx] + num[ry];
  }

  bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
    ll rx = root(x);
    ll ry = root(y);
    return rx == ry;
  }

  ll size(ll x) { return num[root(x)]; }
};

vint divisor(ll n) { // nの約数
  vint ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  vsort(ret);
  return ret;
}

ll get_digit_in(ll n, ll i) { // i桁目の数字を得る。
  for (ll j = 0; j < i - 1; j++) {
    n /= 10;
  }
  return n % 10;
}

ll get_digit(ll n) {
  ll rtn = 0;
  while (n > 0) {
    n /= 10;
    rtn++;
  }
  return rtn;
}

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> rtn;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      rtn[i]++;
      n /= i;
    }
  }
  if (n != 1)
    rtn[n] = 1;
  return rtn;
}

// nPnの順列に対して処理を実行する
void foreach_permutation(ll n, function<void(ll *)> f) {
  ll indexes[n];
  for (ll i = 0; i < n; i++)
    indexes[i] = i;
  do {
    f(indexes);
  } while (std::next_permutation(indexes, indexes + n));
}

void recursive_comb(ll *indexes, ll s, ll rest, function<void(ll *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(ll n, ll k, function<void(ll *)> f) {
  ll indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

// nPkの順列に対して処理を実行する
void foreach_permutation(ll n, ll k, function<void(ll *)> f) {
  foreach_comb(n, k, [&](ll *c_indexes) {
    foreach_permutation(k, [&](ll *p_indexes) {
      ll indexes[k];
      for (ll i = 0; i < k; i++) {
        indexes[i] = c_indexes[p_indexes[i]];
      }
      f(indexes);
    });
  });
}

void printv(vint &V) {
  for (auto e : V)
    cout << e << " ";
  cout << endl;
}

// map< int64_t, int > prime_factor(int64_t n) {
//   map< int64_t, int > ret;
//   for(int64_t i = 2; i * i <= n; i++) {
//     while(n % i == 0) {
//       ret[i]++;
//       n /= i;
//     }
//   }
//   if(n != 1) ret[n] = 1;
//   return ret;
// }

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<(int)(1e9 + 7)>;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

vvint edges;

bool bfs(ll now, ll p, ll s, ll g, ll i, vvint &mask) {
  if (now == g)
    return true;
  for (auto nxt : edges[now]) {
    // cout<<
    if (nxt == p)
      continue;
    if (bfs(nxt, now, s, g, i, mask)) {
      // puts("hey");
      // cout<<now<<":"<<nxt<<endl;
      mask[now][nxt] += (1 << i);
      mask[nxt][now] += (1 << i);
      return true;
    }
  }
  return false;
};

int main() {

  cout << fixed << setprecision(10);

  ll N;
  cin >> N;
  vint a(N - 1), b(N - 1);
  rep(i, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  ll M;
  cin >> M;
  vint u(M), v(M);
  rep(i, M) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
  }

  edges.resize(N);
  vvint mask(N, vint(N, 0));
  // map<pint, ll> m;
  rep(i, N - 1) {
    edges[a[i]].push_back(b[i]);
    edges[b[i]].push_back(a[i]);
    // m[mp(a[i], b[i])] = i;
    // m[mp(b[i], a[i])] = i;
  }

  rep(i, M) {
    ll s, g;
    s = u[i];
    g = v[i];

    bfs(s, -1, s, g, i, mask);
  }

  // rep(i,N){
  //   rep(j,N){
  //     cout<<mask[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }

  //  dp[N][(1<<M)];
  vvint dp(N, vint((1 << M), 0));
  // rep(i,N){
  //   rep(k,(1<<M)) dp[i][k] = 0;
  // }

  dp[0][0] = 1;

  rep(i, N - 1) {
    // cout<<mask[a[i]][b[i]]<<endl;

    rep(j, (1 << M)) {
      ll tmp = j | mask[a[i]][b[i]];

      dp[i + 1][tmp] += dp[i][j]; // 黒
      dp[i + 1][j] += dp[i][j];   // white
    }
  }

  cout << dp[N - 1][(1 << M) - 1] << endl;
}

//