Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define MAX 1002

int n;
int m;
long long int X;

struct st {
  int a;
  int b;
  long long int c;
};
vector<st> v;
bool cmp(st a, st b) { return a.c < b.c; }
int belong[MAX];
inline int root(int b) {
  if (belong[b] == -1)
    return b;
  return belong[b] = root(belong[b]);
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return;
  belong[a] = b;
}

vector<pair<int, long long int>> g[MAX];

bool flag[MAX];

inline long long int dfs(int cur, int pr, int goal) {
  if (cur == goal)
    return 0;
  long long int z = LLONG_MIN;
  for (auto go : g[cur]) {
    if (go.first == pr)
      continue;
    long long int tmp = dfs(go.first, cur, goal);
    if (tmp >= 0) {
      tmp = max(tmp, go.second);
    }
    z = max(z, tmp);
  }
  return z;
}

map<long long int, int> mp;
long long int p2[MAX];

#define MOD 1000000007

int main() {
  p2[0] = 1;
  for (int i = 1; i < MAX; i++) {
    p2[i] = p2[i - 1];
    p2[i] *= 2LL;
    p2[i] %= MOD;
  }
  cin >> n >> m;
  scanf("%lld", &X);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c});
  }
  sort(v.begin(), v.end(), cmp);
  memset(belong, -1, sizeof(belong));
  long long int sum = 0;
  for (int i = 0; i < v.size(); i++) {
    int a = v[i].a;
    int b = v[i].b;
    if (root(a) != root(b)) {
      merge(a, b);
      sum += v[i].c;
      g[a].push_back(make_pair(b, v[i].c));
      g[b].push_back(make_pair(a, v[i].c));
      flag[i] = true;
    }
  }
  int ng = 0;
  int valid = 0;
  for (int i = 0; i < v.size(); i++) {
    if (flag[i] == false) {
      long long int dim = dfs(v[i].a, -1, v[i].b);
      long long int U = -dim + sum + v[i].c;
      if (U < X) {
        ng++;
      }
      if (U == X) {
        valid++;
      }
    } else {
    }
  }
  long long int ans = 0;
  if (sum == X) {
    ans += (p2[n - 1] - 2) * p2[m - (n - 1)];
  }
  if (valid) {
    long long int ava = p2[m - (n - 1) - ng - valid];
    long long int ava2 = (p2[valid] - 1);
    ava *= ava2;
    ava %= MOD;
    ava *= 2LL;
    ava %= MOD;
    ans += ava;
  }
  ans %= MOD;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define MAX 3002

int n;
int m;
long long int X;

struct st {
  int a;
  int b;
  long long int c;
};
vector<st> v;
bool cmp(st a, st b) { return a.c < b.c; }
int belong[MAX];
inline int root(int b) {
  if (belong[b] == -1)
    return b;
  return belong[b] = root(belong[b]);
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return;
  belong[a] = b;
}

vector<pair<int, long long int>> g[MAX];

bool flag[MAX];

inline long long int dfs(int cur, int pr, int goal) {
  if (cur == goal)
    return 0;
  long long int z = LLONG_MIN;
  for (auto go : g[cur]) {
    if (go.first == pr)
      continue;
    long long int tmp = dfs(go.first, cur, goal);
    if (tmp >= 0) {
      tmp = max(tmp, go.second);
    }
    z = max(z, tmp);
  }
  return z;
}

map<long long int, int> mp;
long long int p2[MAX];

#define MOD 1000000007

int main() {
  p2[0] = 1;
  for (int i = 1; i < MAX; i++) {
    p2[i] = p2[i - 1];
    p2[i] *= 2LL;
    p2[i] %= MOD;
  }
  cin >> n >> m;
  scanf("%lld", &X);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c});
  }
  sort(v.begin(), v.end(), cmp);
  memset(belong, -1, sizeof(belong));
  long long int sum = 0;
  for (int i = 0; i < v.size(); i++) {
    int a = v[i].a;
    int b = v[i].b;
    if (root(a) != root(b)) {
      merge(a, b);
      sum += v[i].c;
      g[a].push_back(make_pair(b, v[i].c));
      g[b].push_back(make_pair(a, v[i].c));
      flag[i] = true;
    }
  }
  int ng = 0;
  int valid = 0;
  for (int i = 0; i < v.size(); i++) {
    if (flag[i] == false) {
      long long int dim = dfs(v[i].a, -1, v[i].b);
      long long int U = -dim + sum + v[i].c;
      if (U < X) {
        ng++;
      }
      if (U == X) {
        valid++;
      }
    } else {
    }
  }
  long long int ans = 0;
  if (sum == X) {
    ans += (p2[n - 1] - 2) * p2[m - (n - 1)];
  }
  if (valid) {
    long long int ava = p2[m - (n - 1) - ng - valid];
    long long int ava2 = (p2[valid] - 1);
    ava *= ava2;
    ava %= MOD;
    ava *= 2LL;
    ava %= MOD;
    ans += ava;
  }
  ans %= MOD;
  printf("%lld\n", ans);
  return 0;
}