Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int n, m;
struct edge {
  int u, v, w;
  bool usd;
  edge() {}
  edge(int u, int v, int w) : u(u), v(v), w(w) {}
  bool operator<(const edge &t) const { return w < t.w; }
} es[2200];
int fa[1100];
vector<pii> g[1100];
ll X, val[1100];
int pa[12][1100], wei[12][1100], dep[1100];
void dfs(int x, int fr, int fw) {
  pa[0][x] = fr;
  wei[0][x] = fw;
  dep[x] = dep[fr] + 1;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].first;
    if (to == fr)
      continue;
    dfs(to, x, g[x][i].second);
  }
}
inline int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
pii getmax(int x, int y) {
  if (dep[x] < dep[y])
    swap(x, y);
  int ret = 0;
  for (int i = 0; i < 12; i++) {
    if ((dep[x] - dep[y]) >> i & 1) {
      ret = max(ret, wei[i][x]);
      x = pa[i][x];
    }
  }
  if (x == y)
    return make_pair(ret, x);
  for (int k = 11; k >= 0; k--) {
    if (pa[k][x] != pa[k][y]) {
      ret = max(ret, wei[k][x]);
      ret = max(ret, wei[k][y]);
      x = pa[k][x];
      y = pa[k][y];
    }
  }
  ret = max(ret, wei[0][x]);
  ret = max(ret, wei[0][y]);
  return make_pair(ret, pa[0][x]);
}
int rht[2200];
inline void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
inline int quickpow(ll base, ll ind) {
  ll ans = 1;
  while (ind > 0) {
    if (ind & 1)
      ans = ans * base % MOD;
    ind >>= 1;
    base = base * base % MOD;
  }
  return (int)ans;
}
vector<int> repl[2020];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%lld", &X);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].w);
  }
  sort(es, es + m);
  rht[m - 1] = m;
  for (int i = m - 2; i >= 0; i--) {
    if (es[i].w == es[i + 1].w)
      rht[i] = rht[i + 1];
    else
      rht[i] = i + 1;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  int ru, rv;
  ll sum = 0;
  for (int i = 0; i < m; i++) {
    if ((ru = getfa(es[i].u)) != (rv = getfa(es[i].v))) {
      fa[ru] = rv;
      es[i].usd = 1;
      sum += es[i].w;
      g[es[i].u].push_back(make_pair(es[i].v, es[i].w));
      g[es[i].v].push_back(make_pair(es[i].u, es[i].w));
    } else {
      es[i].usd = 0;
    }
  }
  dfs(1, 0, 0);
  for (int k = 1; k < 12; k++) {
    for (int i = 1; i <= n; i++) {
      pa[k][i] = pa[k - 1][pa[k - 1][i]];
      wei[k][i] = max(wei[k - 1][i], wei[k - 1][pa[k - 1][i]]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    if (es[i].usd) {
      val[i] = sum;
    } else {
      pii tmp = getmax(es[i].u, es[i].v);
      val[i] = sum + es[i].w - tmp.first;
      /*	int x=es[i].u;
              while(x!=tmp.second)
              {
                      if(wei[0][x]==tmp.first)repl[i].push_back(x);
                      x=pa[0][x];
              }
              x=es[i].v;
              while(x!=tmp.second)
              {
                      if(wei[0][x]==tmp.first)repl[i].push_back(x);
                      x=pa[0][x];
              }*/
    }
    //	cout<<i<<" "<<val[i]<<endl;
  }
  for (int i = 1; i < m; i++) {
    if (val[i] == X) {
      int res = 2;
      for (int j = 0; j < m; j++) {
        if (val[j] > X || (val[j] == X && j > i))
          res = res * 2 % MOD;
      }
      add(ans, res);
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int n, m;
struct edge {
  int u, v, w;
  bool usd;
  edge() {}
  edge(int u, int v, int w) : u(u), v(v), w(w) {}
  bool operator<(const edge &t) const { return w < t.w; }
} es[2200];
int fa[1100];
vector<pii> g[1100];
ll X, val[2200];
int pa[12][1100], wei[12][1100], dep[1100];
void dfs(int x, int fr, int fw) {
  pa[0][x] = fr;
  wei[0][x] = fw;
  dep[x] = dep[fr] + 1;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].first;
    if (to == fr)
      continue;
    dfs(to, x, g[x][i].second);
  }
}
inline int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
pii getmax(int x, int y) {
  if (dep[x] < dep[y])
    swap(x, y);
  int ret = 0;
  for (int i = 0; i < 12; i++) {
    if ((dep[x] - dep[y]) >> i & 1) {
      ret = max(ret, wei[i][x]);
      x = pa[i][x];
    }
  }
  if (x == y)
    return make_pair(ret, x);
  for (int k = 11; k >= 0; k--) {
    if (pa[k][x] != pa[k][y]) {
      ret = max(ret, wei[k][x]);
      ret = max(ret, wei[k][y]);
      x = pa[k][x];
      y = pa[k][y];
    }
  }
  ret = max(ret, wei[0][x]);
  ret = max(ret, wei[0][y]);
  return make_pair(ret, pa[0][x]);
}
int rht[2200];
inline void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
inline int quickpow(ll base, ll ind) {
  ll ans = 1;
  while (ind > 0) {
    if (ind & 1)
      ans = ans * base % MOD;
    ind >>= 1;
    base = base * base % MOD;
  }
  return (int)ans;
}
vector<int> repl[2020];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%lld", &X);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].w);
  }
  sort(es, es + m);
  rht[m - 1] = m;
  for (int i = m - 2; i >= 0; i--) {
    if (es[i].w == es[i + 1].w)
      rht[i] = rht[i + 1];
    else
      rht[i] = i + 1;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  int ru, rv;
  ll sum = 0;
  for (int i = 0; i < m; i++) {
    if ((ru = getfa(es[i].u)) != (rv = getfa(es[i].v))) {
      fa[ru] = rv;
      es[i].usd = 1;
      sum += es[i].w;
      g[es[i].u].push_back(make_pair(es[i].v, es[i].w));
      g[es[i].v].push_back(make_pair(es[i].u, es[i].w));
    } else {
      es[i].usd = 0;
    }
  }
  dfs(1, 0, 0);
  for (int k = 1; k < 12; k++) {
    for (int i = 1; i <= n; i++) {
      pa[k][i] = pa[k - 1][pa[k - 1][i]];
      wei[k][i] = max(wei[k - 1][i], wei[k - 1][pa[k - 1][i]]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    if (es[i].usd) {
      val[i] = sum;
    } else {
      pii tmp = getmax(es[i].u, es[i].v);
      val[i] = sum + es[i].w - tmp.first;
      /*	int x=es[i].u;
              while(x!=tmp.second)
              {
                      if(wei[0][x]==tmp.first)repl[i].push_back(x);
                      x=pa[0][x];
              }
              x=es[i].v;
              while(x!=tmp.second)
              {
                      if(wei[0][x]==tmp.first)repl[i].push_back(x);
                      x=pa[0][x];
              }*/
    }
    //	cout<<i<<" "<<val[i]<<endl;
  }
  for (int i = 1; i < m; i++) {
    if (val[i] == X) {
      int res = 2;
      for (int j = 0; j < m; j++) {
        if (val[j] > X || (val[j] == X && j > i))
          res = res * 2 % MOD;
      }
      add(ans, res);
    }
  }
  printf("%d\n", ans);
  return 0;
}