Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;

const int MAXN = 1005;
const int MAXM = 2005;
const ll MOD = 1000000007;

struct Edge {
  int to;
  int w;
  int nxt;
} edge[MAXM << 1];

int n, m, id, MAX;
int first[MAXN];
int u[MAXM];
int v[MAXM];
int w[MAXN];
int fa[MAXN];

ll X, tot;
ll pw[MAXM];

bool vis[MAXM];

void addE(int u, int v, int w) {
  edge[++id] = (Edge){v, w, first[u]};
  first[u] = id;
}

int getroot(int u) { return u == fa[u] ? u : fa[u] = getroot(fa[u]); }

bool getmax(int u, int fa, int T) {
  if (u == T)
    return 1;
  for (int i = first[u]; i; i = edge[i].nxt)
    if (edge[i].to != fa && getmax(edge[i].to, u, T)) {
      MAX = max(MAX, edge[i].w);
      return 1;
    }
  return 0;
}

ll solve(ll x) {
  if (tot >= x)
    return pw[m];
  int cnt = m - n + 1;
  for (int i = 1; i <= m; i++)
    if (!vis[i]) {
      MAX = 0;
      getmax(u[i], 0, v[i]);
      if (tot - MAX + w[i] < x)
        cnt--;
    }
  return pw[cnt + 1];
}

int main() {
  scanf("%d%d%lld", &n, &m, &X);
  pw[0] = 1;
  for (int i = 1; i <= m; i++)
    pw[i] = pw[i - 1] * 2 % MOD;
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
  for (int i = 1; i <= m; i++)
    for (int j = i + 1; j <= m; j++)
      if (w[i] > w[j]) {
        swap(u[i], u[j]);
        swap(v[i], v[j]);
        swap(w[i], w[j]);
      }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++)
    if (getroot(u[i]) != getroot(v[i])) {
      vis[i] = 1;
      addE(u[i], v[i], w[i]);
      addE(v[i], u[i], w[i]);
      fa[getroot(u[i])] = getroot(v[i]);
      tot += w[i];
    }
  printf("%lld\n", (solve(X) - solve(X + 1) + MOD) % MOD);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;

const int MAXN = 1005;
const int MAXM = 2005;
const ll MOD = 1000000007;

struct Edge {
  int to;
  int w;
  int nxt;
} edge[MAXM << 1];

int n, m, id, MAX;
int first[MAXN];
int u[MAXM];
int v[MAXM];
int w[MAXM];
int fa[MAXN];

ll X, tot;
ll pw[MAXM];

bool vis[MAXM];

void addE(int u, int v, int w) {
  edge[++id] = (Edge){v, w, first[u]};
  first[u] = id;
}

int getroot(int u) { return u == fa[u] ? u : fa[u] = getroot(fa[u]); }

bool getmax(int u, int fa, int T) {
  if (u == T)
    return 1;
  for (int i = first[u]; i; i = edge[i].nxt)
    if (edge[i].to != fa && getmax(edge[i].to, u, T)) {
      MAX = max(MAX, edge[i].w);
      return 1;
    }
  return 0;
}

ll solve(ll x) {
  if (tot >= x)
    return pw[m];
  int cnt = m - n + 1;
  for (int i = 1; i <= m; i++)
    if (!vis[i]) {
      MAX = 0;
      getmax(u[i], 0, v[i]);
      if (tot - MAX + w[i] < x)
        cnt--;
    }
  return pw[cnt + 1];
}

int main() {
  scanf("%d%d%lld", &n, &m, &X);
  pw[0] = 1;
  for (int i = 1; i <= m; i++)
    pw[i] = pw[i - 1] * 2 % MOD;
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
  for (int i = 1; i <= m; i++)
    for (int j = i + 1; j <= m; j++)
      if (w[i] > w[j]) {
        swap(u[i], u[j]);
        swap(v[i], v[j]);
        swap(w[i], w[j]);
      }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++)
    if (getroot(u[i]) != getroot(v[i])) {
      vis[i] = 1;
      addE(u[i], v[i], w[i]);
      addE(v[i], u[i], w[i]);
      fa[getroot(u[i])] = getroot(v[i]);
      tot += w[i];
    }
  printf("%lld\n", (solve(X) - solve(X + 1) + MOD) % MOD);
  return 0;
}