Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const ll MOD = 1e9 + 7;
const ll INV = 5e8 + 4;

class UFDS {
private:
  vector<int> p, rank;

public:
  UFDS(int N) {
    p.assign(N, 0);
    for (int i = 0; i < N; i++)
      p[i] = i;
    rank.assign(N, 0);
  }
  int find_set(int i) { return (p[i] == i) ? i : (p[i] = find_set(p[i])); }
  bool same_set(int i, int j) { return find_set(i) == find_set(j); }
  void union_set(int i, int j) {
    if (same_set(i, j))
      return;
    int x = find_set(i), y = find_set(j);
    rank[x] > rank[y] ? p[y] = x : p[x] = y;
    if (rank[x] == rank[y])
      rank[y]++;
  }
};

vector<pair<int, ll>> G[1001], H[1001];
vector<pair<ll, ii>> E, A, B;
bitset<1001> vis;

ll dfs(int u, int d) {
  vis[u] = 1;
  for (auto &v : H[u]) {
    if (!vis[v.f]) {
      if (v.f == d)
        return v.s;
      ll x = dfs(v.f, d);
      if (x)
        return max(x, v.s);
    }
  }
  return 0;
}

int main() {
  ll N, M, X;
  cin >> N >> M >> X;
  while (M--) {
    int u, v, w;
    cin >> u >> v >> w;
    E.emplace_back(w, ii(u, v));
    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }
  sort(E.begin(), E.end());

  UFDS U(N);
  ll cost = 0, num = 1;
  for (auto &e : E) {
    if (!U.same_set(e.s.f, e.s.s)) {
      U.union_set(e.s.f, e.s.s);
      H[e.s.f].emplace_back(e.s.s, e.f);
      H[e.s.s].emplace_back(e.s.f, e.f);
      cost += e.f;
      A.push_back(e);
      num = 2 * num % MOD;
    } else {
      B.push_back(e);
    }
  }

  if (cost > X) {
    cout << 0 << '\n';
  } else {
    ll ans = 1, cnt = (cost == X ? num * INV % MOD : 1);
    for (auto &e : B) {
      vis.reset();
      ll x = e.f - dfs(e.s.f, e.s.s) + cost;
      if (x > X)
        ans = 2 * ans % MOD;
      if (x == X)
        cnt = 2 * cnt % MOD;
    }
    cout << 2 * (cnt - 1 + MOD) * ans % MOD << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const ll MOD = 1e9 + 7;
const ll INV = 5e8 + 4;

class UFDS {
private:
  vector<int> p, rank;

public:
  UFDS(int N) {
    p.assign(N, 0);
    for (int i = 0; i < N; i++)
      p[i] = i;
    rank.assign(N, 0);
  }
  int find_set(int i) { return (p[i] == i) ? i : (p[i] = find_set(p[i])); }
  bool same_set(int i, int j) { return find_set(i) == find_set(j); }
  void union_set(int i, int j) {
    if (same_set(i, j))
      return;
    int x = find_set(i), y = find_set(j);
    rank[x] > rank[y] ? p[y] = x : p[x] = y;
    if (rank[x] == rank[y])
      rank[y]++;
  }
};

vector<pair<int, ll>> G[1001], H[1001];
vector<pair<ll, ii>> E, A, B;
bitset<1001> vis;

ll dfs(int u, int d) {
  vis[u] = 1;
  for (auto &v : H[u]) {
    if (!vis[v.f]) {
      if (v.f == d)
        return v.s;
      ll x = dfs(v.f, d);
      if (x)
        return max(x, v.s);
    }
  }
  return 0;
}

int main() {
  ll N, M, X;
  cin >> N >> M >> X;
  while (M--) {
    int u, v, w;
    cin >> u >> v >> w;
    E.emplace_back(w, ii(u, v));
    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }
  sort(E.begin(), E.end());

  UFDS U(N + 1);
  ll cost = 0, num = 1;
  for (auto &e : E) {
    if (!U.same_set(e.s.f, e.s.s)) {
      U.union_set(e.s.f, e.s.s);
      H[e.s.f].emplace_back(e.s.s, e.f);
      H[e.s.s].emplace_back(e.s.f, e.f);
      cost += e.f;
      A.push_back(e);
      num = 2 * num % MOD;
    } else {
      B.push_back(e);
    }
  }

  if (cost > X) {
    cout << 0 << '\n';
  } else {
    ll ans = 1, cnt = (cost == X ? num * INV % MOD : 1);
    for (auto &e : B) {
      vis.reset();
      ll x = e.f - dfs(e.s.f, e.s.s) + cost;
      if (x > X)
        ans = 2 * ans % MOD;
      if (x == X)
        cnt = 2 * cnt % MOD;
    }
    cout << 2 * (cnt - 1 + MOD) * ans % MOD << '\n';
  }
}