Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
#define pii pair<int, int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define drep(i, x, y) for (int i = (x); i >= (y); i--)
#define go(x) for (int i = head[x]; i; i = edge[i].nxt)
#define templ template <typename T>
#define sz 2222
#define mod 1000000007ll
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l, T r) { return uniform_int_distribution<T>(l, r)(rng); }
templ inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
templ inline bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
templ inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0')
    f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0')
    t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0')
      t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __zz = 0;
inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20)
    Ot();
  if (x < 0)
    __sr[++__C] = '-', x = -x;
  while (__z[++__zz] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__zz], --__zz)
    ;
  __sr[++__C] = '\n';
}
void file() {
#ifdef NTFOrz
  freopen("a.in", "r", stdin);
#endif
}
inline void chktime() {
#ifdef NTFOrz
  cout << (clock() - t) / 1000.0 << '\n';
#endif
}
#ifdef mod
ll ksm(ll x, int y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1)
      ret = ret * x % mod;
  return ret;
}
ll inv(ll x) { return ksm(x, mod - 2); }
#else
ll ksm(ll x, int y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1)
      ret = ret * x;
  return ret;
}
#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll
//ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
} // namespace my_std
using namespace my_std;

int n, m;
ll X;
struct hh {
  int u, v;
  ll w;
  const bool operator<(const hh &x) const { return w < x.w; }
} edge[sz];
int vis[sz];
ll W[sz];

namespace LCT {
int fa[sz], ch[sz][2], w[sz], mx[sz], tag[sz];
#define ls ch[x][0]
#define rs ch[x][1]
int nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
int get(int x) { return ch[fa[x]][1] == x; }
void pushup(int x) { mx[x] = max(max(mx[ls], mx[rs]), w[x]); }
void Rev(int x) {
  tag[x] ^= 1;
  swap(ls, rs);
}
void pushdown(int x) {
  if (!tag[x])
    return;
  if (ls)
    Rev(ls);
  if (rs)
    Rev(rs);
  tag[x] = 0;
}
void Pushdown(int x) {
  if (nroot(x))
    Pushdown(fa[x]);
  pushdown(x);
}
void rotate(int x) {
  int y = fa[x], z = fa[y], k = get(x), w = ch[x][!k];
  if (nroot(y))
    ch[z][get(y)] = x;
  ch[x][!k] = y;
  ch[y][k] = w;
  if (w)
    fa[w] = y;
  fa[y] = x;
  fa[x] = z;
  pushup(y), pushup(x);
}
void splay(int x) {
  Pushdown(x);
  for (int y; y = fa[x], nroot(x); rotate(x))
    if (nroot(y))
      rotate(get(x) == get(y) ? y : x);
}
void access(int x) {
  for (int y = 0; x; x = fa[y = x])
    splay(x), rs = y, pushup(x);
}
void makeroot(int x) { access(x), splay(x), Rev(x); }
void link(int x, int y) {
  makeroot(x);
  fa[x] = y;
}
int query(int x, int y) {
  makeroot(x);
  access(y), splay(y);
  return mx[y];
}
} // namespace LCT
using namespace LCT;

int f[sz];
int getfa(int x) { return x == f[x] ? x : f[x] = getfa(f[x]); }

int main() {
  file();
  read(n, m, X);
  int x, y, z;
  rep(i, 1, m) read(x, y, z), edge[i] = (hh){x, y, z};
  sort(edge + 1, edge + m + 1);
  rep(i, 1, n) f[i] = i;
  rep(i, 1, m) w[i + n] = mx[i + n] = edge[i].w;
  ll S = 0;
  rep(i, 1, m) {
    int u = edge[i].u, v = edge[i].v;
    if (getfa(u) == getfa(v))
      continue;
    link(u, i + n), link(v, i + n);
    vis[i] = 1;
    S += edge[i].w;
    f[f[u]] = f[v];
  }
  rep(i, 1, m) if (vis[i]) W[i] = S;
  else W[i] = S + edge[i].w - query(edge[i].u, edge[i].v);
  int cnt1 = 0, cnt2 = 0;
  rep(i, 1, m) cnt1 += (W[i] < X), cnt2 += (W[i] == X);
  if (!cnt2)
    return puts("0"), 0;
  ll ans;
  if (cnt1)
    ans = ksm(2, m - cnt1 - cnt2) * 2 % mod * (ksm(2, cnt2) - 1) % mod;
  else
    ans = ksm(2, m - cnt2) * (ksm(2, cnt2) - 2) % mod;
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
#define pii pair<int, int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define drep(i, x, y) for (int i = (x); i >= (y); i--)
#define go(x) for (int i = head[x]; i; i = edge[i].nxt)
#define templ template <typename T>
#define sz 5222
#define mod 1000000007ll
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l, T r) { return uniform_int_distribution<T>(l, r)(rng); }
templ inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
templ inline bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
templ inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0')
    f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0')
    t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0')
      t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __zz = 0;
inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20)
    Ot();
  if (x < 0)
    __sr[++__C] = '-', x = -x;
  while (__z[++__zz] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__zz], --__zz)
    ;
  __sr[++__C] = '\n';
}
void file() {
#ifdef NTFOrz
  freopen("a.in", "r", stdin);
#endif
}
inline void chktime() {
#ifdef NTFOrz
  cout << (clock() - t) / 1000.0 << '\n';
#endif
}
#ifdef mod
ll ksm(ll x, int y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1)
      ret = ret * x % mod;
  return ret;
}
ll inv(ll x) { return ksm(x, mod - 2); }
#else
ll ksm(ll x, int y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1)
      ret = ret * x;
  return ret;
}
#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll
//ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
} // namespace my_std
using namespace my_std;

int n, m;
ll X;
struct hh {
  int u, v;
  ll w;
  const bool operator<(const hh &x) const { return w < x.w; }
} edge[sz];
int vis[sz];
ll W[sz];

namespace LCT {
int fa[sz], ch[sz][2], w[sz], mx[sz], tag[sz];
#define ls ch[x][0]
#define rs ch[x][1]
int nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
int get(int x) { return ch[fa[x]][1] == x; }
void pushup(int x) { mx[x] = max(max(mx[ls], mx[rs]), w[x]); }
void Rev(int x) {
  tag[x] ^= 1;
  swap(ls, rs);
}
void pushdown(int x) {
  if (!tag[x])
    return;
  if (ls)
    Rev(ls);
  if (rs)
    Rev(rs);
  tag[x] = 0;
}
void Pushdown(int x) {
  if (nroot(x))
    Pushdown(fa[x]);
  pushdown(x);
}
void rotate(int x) {
  int y = fa[x], z = fa[y], k = get(x), w = ch[x][!k];
  if (nroot(y))
    ch[z][get(y)] = x;
  ch[x][!k] = y;
  ch[y][k] = w;
  if (w)
    fa[w] = y;
  fa[y] = x;
  fa[x] = z;
  pushup(y), pushup(x);
}
void splay(int x) {
  Pushdown(x);
  for (int y; y = fa[x], nroot(x); rotate(x))
    if (nroot(y))
      rotate(get(x) == get(y) ? y : x);
}
void access(int x) {
  for (int y = 0; x; x = fa[y = x])
    splay(x), rs = y, pushup(x);
}
void makeroot(int x) { access(x), splay(x), Rev(x); }
void link(int x, int y) {
  makeroot(x);
  fa[x] = y;
}
int query(int x, int y) {
  makeroot(x);
  access(y), splay(y);
  return mx[y];
}
} // namespace LCT
using namespace LCT;

int f[sz];
int getfa(int x) { return x == f[x] ? x : f[x] = getfa(f[x]); }

int main() {
  file();
  read(n, m, X);
  int x, y, z;
  rep(i, 1, m) read(x, y, z), edge[i] = (hh){x, y, z};
  sort(edge + 1, edge + m + 1);
  rep(i, 1, n) f[i] = i;
  rep(i, 1, m) w[i + n] = mx[i + n] = edge[i].w;
  ll S = 0;
  rep(i, 1, m) {
    int u = edge[i].u, v = edge[i].v;
    if (getfa(u) == getfa(v))
      continue;
    link(u, i + n), link(v, i + n);
    vis[i] = 1;
    S += edge[i].w;
    f[f[u]] = f[v];
  }
  rep(i, 1, m) if (vis[i]) W[i] = S;
  else W[i] = S + edge[i].w - query(edge[i].u, edge[i].v);
  int cnt1 = 0, cnt2 = 0;
  rep(i, 1, m) cnt1 += (W[i] < X), cnt2 += (W[i] == X);
  if (!cnt2)
    return puts("0"), 0;
  ll ans;
  if (cnt1)
    ans = ksm(2, m - cnt1 - cnt2) * 2 % mod * (ksm(2, cnt2) - 1) % mod;
  else
    ans = ksm(2, m - cnt2) * (ksm(2, cnt2) - 2) % mod;
  cout << ans;
  return 0;
}