Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;

const int maxn = 1111;
const int maxm = 2111;
const int mod = 1e9 + 7;
int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = (LL)ret * x % mod;
    x = (LL)x * x % mod;
    y >>= 1;
  }
  return ret;
}

int n, m, eu[maxm], ev[maxm], ew[maxm], ord[maxm], f[maxn], w[maxn][maxn];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
bool cmp(int x, int y) { return ew[x] < ew[y]; }

vector<int> vs[maxn];
bool tree[maxn];
LL X;

int main() {
  scanf("%d%d", &n, &m);
  scanf("%lld", &X);
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", eu + i, ev + i, ew + i);

  for (int i = 1; i <= n; i++) {
    f[i] = i;
    vs[i].pb(i);
  }
  for (int i = 1; i <= m; i++)
    ord[i] = i;
  sort(ord + 1, ord + m + 1, cmp);
  for (int t = 1; t <= m; t++) {
    int id = ord[t], u = gf(eu[id]), v = gf(ev[id]);
    if (u == v)
      continue;
    tree[id] = 1;
    for (int i = 0; i < (int)vs[u].size(); i++)
      for (int j = 0; j < (int)vs[v].size(); j++)
        w[vs[u][i]][vs[v][j]] = w[vs[v][j]][vs[u][i]] = ew[id];
    f[u] = v;
    for (int i = 0; i < (int)vs[u].size(); i++)
      vs[v].pb(vs[u][i]);
    X -= ew[id];
    //		printf("id= %d X= %lld\n",id,X);
  }
  if (X < 0) {
    puts("0");
    return 0;
  }
  if (X == 0) {
    int cnt = 0;
    for (int i = 1; i <= m; i++)
      if (w[eu[i]][ev[i]] == ew[i])
        cnt++;
    printf("%d\n", (qpow(2, m) - qpow(2, m - cnt + 1) + mod) % mod);
  } else {
    int c1 = 0, c2 = 0;
    for (int i = 1; i <= m; i++)
      if (ew[i] - w[eu[i]][ev[i]] < X)
        c1++;
      else if (ew[i] - w[eu[i]][ev[i]] == X)
        c2++;
    //		printf("c1= %d c2= %d\n",c1,c2);
    printf("%d\n",
           (qpow(2, m - c1 + 1) - qpow(2, m - c1 - c2 + 1) + mod) % mod);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;

const int maxn = 1111;
const int maxm = 2111;
const int mod = 1e9 + 7;
int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = (LL)ret * x % mod;
    x = (LL)x * x % mod;
    y >>= 1;
  }
  return ret;
}

int n, m, eu[maxm], ev[maxm], ew[maxm], ord[maxm], f[maxn], w[maxn][maxn];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
bool cmp(int x, int y) { return ew[x] < ew[y]; }

vector<int> vs[maxn];
bool tree[maxm];
LL X;

int main() {
  scanf("%d%d", &n, &m);
  scanf("%lld", &X);
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", eu + i, ev + i, ew + i);

  for (int i = 1; i <= n; i++) {
    f[i] = i;
    vs[i].pb(i);
  }
  for (int i = 1; i <= m; i++)
    ord[i] = i;
  sort(ord + 1, ord + m + 1, cmp);
  for (int t = 1; t <= m; t++) {
    int id = ord[t], u = gf(eu[id]), v = gf(ev[id]);
    if (u == v)
      continue;
    tree[id] = 1;
    for (int i = 0; i < (int)vs[u].size(); i++)
      for (int j = 0; j < (int)vs[v].size(); j++)
        w[vs[u][i]][vs[v][j]] = w[vs[v][j]][vs[u][i]] = ew[id];
    f[u] = v;
    for (int i = 0; i < (int)vs[u].size(); i++)
      vs[v].pb(vs[u][i]);
    X -= ew[id];
    //		printf("id= %d X= %lld\n",id,X);
  }
  if (X < 0) {
    puts("0");
    return 0;
  }
  if (X == 0) {
    int cnt = 0;
    for (int i = 1; i <= m; i++)
      if (w[eu[i]][ev[i]] == ew[i])
        cnt++;
    printf("%d\n", (qpow(2, m) - qpow(2, m - cnt + 1) + mod) % mod);
  } else {
    int c1 = 0, c2 = 0;
    for (int i = 1; i <= m; i++)
      if (ew[i] - w[eu[i]][ev[i]] < X)
        c1++;
      else if (ew[i] - w[eu[i]][ev[i]] == X)
        c2++;
    //		printf("c1= %d c2= %d\n",c1,c2);
    printf("%d\n",
           (qpow(2, m - c1 + 1) - qpow(2, m - c1 - c2 + 1) + mod) % mod);
  }
  return 0;
}