Problem p03405 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

// #define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (2006)

ll n, m, x, p[MAXN];
vector<pi> v[MAXN];
vector<spi> e;
void myassert(bool b) {
  if (!b) {
    cout << 945693 << '\n';
    exit(0);
  }
}
struct _ufds {
  int p[MAXN];
  _ufds() { FOR(i, 0, MAXN - 1) p[i] = i; }
  void merge(ll x, ll y) {
    x = find(x), y = find(y);
    myassert(x >= 0 && x < MAXN), myassert(y >= 0 && y < MAXN);
    p[x] = y;
  }
  ll find(ll x) {
    return 0;
    return (p[x] == x) ? x : p[x] = find(p[x]);
  }
  bool same(ll a, ll b) { return find(a) == find(b); }
} ufds;
ll depth[MAXN], up[MAXN];
void dfs(ll x, ll par) {
  p[x] = par;
  for (auto i : v[x])
    if (i.f ^ par) {
      depth[i.f] = depth[x] + 1, up[i.f] = i.s, dfs(i.f, x);
    }
}
ll mod = 1e9 + 7;
ll qexp(ll x, ll e) {
  if (e == 0)
    return 1;
  ll half = qexp(x, e / 2);
  half *= half, half %= mod;
  if (e & 1)
    half *= x, half %= mod;
  return half;
}
int main() {
  FAST cin >> n >> m >> x;
  FOR(i, 0, m - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    e.eb(c, pi(a, b));
  }
  sort(all(e));
  vector<bool> in(5000, 0);
  vector<ll> diff(5000, 0);
  ll mst = 0;
  for (int i = 0; i < (int)e.size(); ++i) {
    ll a = e[i].s.f, b = e[i].s.s;
    if (ufds.same(a, b))
      continue;
    ufds.merge(a, b);
    in[i] = 1;
    v[a].eb(b, e[i].f);
    v[b].eb(a, e[i].f);
    mst += e[i].f;
  }
  dfs(1, 1);
  function<ll(ll, ll)> go_up = [&](ll x, ll y) {
    ll ans = 0;
    while (x ^ y) {
      if (depth[x] < depth[y]) {
        swap(x, y);
      }
      ans = max(ans, up[x]);
      x = p[x];
    }
    return ans;
  };
  ll D = x - mst, equal = 0, upper = 0;
  FOR(i, 0, siz(e) - 1) if (!in[i]) {
    diff[i] = e[i].f - go_up(e[i].s.f, e[i].s.s), assert(diff[i] >= 0);
    if (D == diff[i]) {
      ++equal;
    } else if (D < diff[i]) {
      ++upper;
    }
  }
  if (D < 0) {
    cout << 0 << '\n';
    return 0;
  } else if (D == 0) {
    cout << (qexp(2, n - 1 + equal) - 2 + mod) % mod * qexp(2, upper) % mod
         << '\n';
  } else {
    cout << (qexp(2, equal) - 1 + mod) % mod * qexp(2, upper) % mod * 2 % mod
         << '\n';
  }
}
/*
lower, equal, upper
D = X - S
if(D < 0) cout<<0;
else if(D==0) {
        (qexp(2, n-1 + equal) - 1) * qexp(2, upper)
} else  if(D > 0) {
        (qexp(2, equal) - 1) * qexp(2, upper) * 2
}
*/


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

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

// #define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (2006)

ll n, m, x, p[MAXN];
vector<pi> v[MAXN];
vector<spi> e;
void myassert(bool b) {
  if (!b) {
    cout << 945693 << '\n';
    exit(0);
  }
}
struct _ufds {
  int p[MAXN];
  _ufds() { FOR(i, 0, MAXN - 1) p[i] = i; }
  void merge(ll x, ll y) {
    x = find(x), y = find(y);
    myassert(x >= 0 && x < MAXN), myassert(y >= 0 && y < MAXN);
    p[x] = y;
  }
  ll find(ll x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
  bool same(ll a, ll b) { return find(a) == find(b); }
} ufds;
ll depth[MAXN], up[MAXN];
void dfs(ll x, ll par) {
  p[x] = par;
  for (auto i : v[x])
    if (i.f ^ par) {
      depth[i.f] = depth[x] + 1, up[i.f] = i.s, dfs(i.f, x);
    }
}
ll mod = 1e9 + 7;
ll qexp(ll x, ll e) {
  if (e == 0)
    return 1;
  ll half = qexp(x, e / 2);
  half *= half, half %= mod;
  if (e & 1)
    half *= x, half %= mod;
  return half;
}
int main() {
  FAST cin >> n >> m >> x;
  FOR(i, 0, m - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    e.eb(c, pi(a, b));
  }
  sort(all(e));
  vector<bool> in(5000, 0);
  vector<ll> diff(5000, 0);
  ll mst = 0;
  for (int i = 0; i < (int)e.size(); ++i) {
    ll a = e[i].s.f, b = e[i].s.s;
    if (ufds.same(a, b))
      continue;
    ufds.merge(a, b);
    in[i] = 1;
    v[a].eb(b, e[i].f);
    v[b].eb(a, e[i].f);
    mst += e[i].f;
  }
  dfs(1, 1);
  function<ll(ll, ll)> go_up = [&](ll x, ll y) {
    ll ans = 0;
    while (x ^ y) {
      if (depth[x] < depth[y]) {
        swap(x, y);
      }
      ans = max(ans, up[x]);
      x = p[x];
    }
    return ans;
  };
  ll D = x - mst, equal = 0, upper = 0;
  FOR(i, 0, siz(e) - 1) if (!in[i]) {
    diff[i] = e[i].f - go_up(e[i].s.f, e[i].s.s), assert(diff[i] >= 0);
    if (D == diff[i]) {
      ++equal;
    } else if (D < diff[i]) {
      ++upper;
    }
  }
  if (D < 0) {
    cout << 0 << '\n';
    return 0;
  } else if (D == 0) {
    cout << (qexp(2, n - 1 + equal) - 2 + mod) % mod * qexp(2, upper) % mod
         << '\n';
  } else {
    cout << (qexp(2, equal) - 1 + mod) % mod * qexp(2, upper) % mod * 2 % mod
         << '\n';
  }
}
/*
lower, equal, upper
D = X - S
if(D < 0) cout<<0;
else if(D==0) {
        (qexp(2, n-1 + equal) - 1) * qexp(2, upper)
} else  if(D > 0) {
        (qexp(2, equal) - 1) * qexp(2, upper) * 2
}
*/