Problem p03405 (C++)

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

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) ((x) & (-(x)))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ld inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 100020;
const ll mod = 1e9 + 7;

struct node {
  int next, to, w;
} e[maxn * 2];
struct node2 {
  int x, y, w;
  bool operator<(node2 a) const { return w < a.w; }
} dt[maxn], sub[maxn];
int head[maxn], cnt, fa[maxn], dth[maxn], faw[maxn];
int n, m, tot, delta[maxn], num;
ll X, totw, ans;
ll pow2[maxn];

inline void adde(int x, int y, int w) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  e[cnt].w = w;
  head[x] = cnt;
}
void dfs(int x) {
  fore(i, x) {
    if (e[i].to == fa[x])
      continue;
    dth[e[i].to] = dth[x] + 1, fa[e[i].to] = x, faw[e[i].to] = e[i].w;
    dfs(e[i].to);
  }
}
int getmx(int x, int y) {
  int res = 0;
  while (x != y) {
    if (dth[x] < dth[y])
      swap(x, y);
    res = max(res, faw[x]);
    x = fa[x];
  }
  return res;
}
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void kruskal() {
  rep(i, 1, n) fa[i] = i;
  sort(dt + 1, dt + m + 1);
  rep(i, 1, m) {
    int x = dt[i].x, y = dt[i].y, w = dt[i].w;
    int p = getfa(x), q = getfa(y);
    if (p != q) {
      fa[p] = q;
      totw += w;
      adde(x, y, w), adde(y, x, w);
      num++;
    } else
      sub[++tot] = dt[i];
  }
  if (num < n - 1)
    return;
  dfs(1);
  rep(i, 1, tot) {
    int x = sub[i].x, y = sub[i].y, w = sub[i].w;
    int mxw = getmx(x, y);
    delta[i] = w - mxw;
    //	cout<<delta[i]<<endl;
  }
}
void solve() {
  pow2[0] = 1;
  rep(i, 1, m) pow2[i] = pow2[i - 1] * 2 % mod;
  if (totw > X)
    return;
  if (totw == X) {
    int a = n - 1;
    rep(i, 1, tot) if (!delta[i]) a++;
    ans = (pow2[a] - 2 + mod) % mod * pow2[m - a] % mod;
  } else {
    int a = n - 1, c = 0, b = 0;
    rep(i, 1, tot) {
      if (delta[i] == X - totw)
        c++;
      else if (delta[i] < X - totw)
        a++;
      else
        b++;
    }
    ans = (pow2[c] - 1) * 2 * pow2[b] % mod;
  }
}
int main() {
  scanf("%d %d %lld", &n, &m, &X);
  if (n <= 2)
    return printf("0"), 0;
  rep(i, 1, m) {
    int x, y, w;
    scanf("%d %d %d", &x, &y, &w);
    dt[i] = (node2){x, y, w};
  }
  kruskal();
  if (num < n - 1)
    return printf("0"), 0;
  solve();
  cout << ans << endl;
}


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

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) ((x) & (-(x)))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ld inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 100020;
const ll mod = 1e9 + 7;

struct node {
  int next, to, w;
} e[maxn * 2];
struct node2 {
  int x, y, w;
  bool operator<(node2 a) const { return w < a.w; }
} dt[maxn], sub[maxn];
int head[maxn], cnt, fa[maxn], dth[maxn], faw[maxn];
int n, m, tot, delta[maxn], num;
ll X, totw, ans;
ll pow2[maxn];

inline void adde(int x, int y, int w) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  e[cnt].w = w;
  head[x] = cnt;
}
void dfs(int x) {
  fore(i, x) {
    if (e[i].to == fa[x])
      continue;
    dth[e[i].to] = dth[x] + 1, fa[e[i].to] = x, faw[e[i].to] = e[i].w;
    dfs(e[i].to);
  }
}
int getmx(int x, int y) {
  int res = 0;
  while (x != y) {
    if (dth[x] < dth[y])
      swap(x, y);
    res = max(res, faw[x]);
    x = fa[x];
  }
  return res;
}
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void kruskal() {
  rep(i, 1, n) fa[i] = i;
  sort(dt + 1, dt + m + 1);
  rep(i, 1, m) {
    int x = dt[i].x, y = dt[i].y, w = dt[i].w;
    int p = getfa(x), q = getfa(y);
    if (p != q) {
      fa[p] = q;
      totw += w;
      adde(x, y, w), adde(y, x, w);
      num++;
    } else
      sub[++tot] = dt[i];
  }
  if (num < n - 1)
    return;
  rep(i, 1, n) fa[i] = 0;
  dfs(1);
  rep(i, 1, tot) {
    int x = sub[i].x, y = sub[i].y, w = sub[i].w;
    int mxw = getmx(x, y);
    delta[i] = w - mxw;
    //	cout<<delta[i]<<endl;
  }
}
void solve() {
  pow2[0] = 1;
  rep(i, 1, m) pow2[i] = pow2[i - 1] * 2 % mod;
  if (totw > X)
    return;
  if (totw == X) {
    int a = n - 1;
    rep(i, 1, tot) if (!delta[i]) a++;
    ans = (pow2[a] - 2 + mod) % mod * pow2[m - a] % mod;
  } else {
    int a = n - 1, c = 0, b = 0;
    rep(i, 1, tot) {
      if (delta[i] == X - totw)
        c++;
      else if (delta[i] < X - totw)
        a++;
      else
        b++;
    }
    ans = (pow2[c] - 1) * 2 * pow2[b] % mod;
  }
}
int main() {
  scanf("%d %d %lld", &n, &m, &X);
  if (n <= 2)
    return printf("0"), 0;
  rep(i, 1, m) {
    int x, y, w;
    scanf("%d %d %d", &x, &y, &w);
    dt[i] = (node2){x, y, w};
  }
  kruskal();
  if (num < n - 1)
    return printf("0"), 0;
  solve();
  cout << ans << endl;
}