Problem p03405 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstdio>

using namespace std;
const int Max_N(1050);
const int Max_M(2050);
typedef long long int LL;
const int MOD(1000000000 + 7);

constexpr int Mult(int a, int b) { return a * 1LL * b % MOD; }

constexpr int Add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }

constexpr int Sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }

struct edge {
  int pos, u, v, w;
};
int N, M, Father[Max_N], power[Max_M];
edge E[Max_M];
LL X, MST[Max_M];

inline bool operator<(const edge &a, const edge &b) { return a.w < b.w; }

int Get_Father(int x) {
  return Father[x] == x ? x : Father[x] = Get_Father(Father[x]);
}

void query(int must) {
  for (int u = 1; u <= N; ++u)
    Father[u] = u;
  for (int i = 1; i <= M; ++i)
    if (E[i].pos == must)
      Father[E[i].v] = E[i].u, MST[must] += E[i].w;
  for (int i = 1, u, v; i <= M; ++i)
    if ((u = Get_Father(E[i].u)) != (v = Get_Father(E[i].v)))
      Father[v] = u, MST[must] += E[i].w;
}

int main() {
  scanf("%d%d", &N, &M);
  power[0] = 1;
  for (int i = 1; i <= M; ++i)
    E[i].pos = i, scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w),
    power[i] = Add(power[i - 1], power[i - 1]);
  sort(E + 1, E + 1 + M);
  for (int i = 0; i <= M; ++i)
    query(i);
  if (MST[0] > X) {
    printf("0");
    return 0;
  }
  int a = 0, b = 0, c = 0;
  for (int i = 1; i <= M; ++i)
    a += (MST[i] < X), b += (MST[i] == X), c += (MST[i] > X);
  if (MST[0] == X)
    printf("%d", Mult(power[c], Sub(power[b], 2)));
  else
    printf("%d", Mult(2, Mult(Sub(power[b], 1), power[c])));
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;
const int Max_N(1050);
const int Max_M(2050);
typedef long long int LL;
const int MOD(1000000000 + 7);

constexpr int Mult(int a, int b) { return a * 1LL * b % MOD; }

constexpr int Add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }

constexpr int Sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }

struct edge {
  int pos, u, v, w;
};
int N, M, Father[Max_N], power[Max_M];
edge E[Max_M];
LL X, MST[Max_M];

inline bool operator<(const edge &a, const edge &b) { return a.w < b.w; }

int Get_Father(int x) {
  return Father[x] == x ? x : Father[x] = Get_Father(Father[x]);
}

void query(int must) {
  for (int u = 1; u <= N; ++u)
    Father[u] = u;
  for (int i = 1; i <= M; ++i)
    if (E[i].pos == must)
      Father[E[i].v] = E[i].u, MST[must] += E[i].w;
  for (int i = 1, u, v; i <= M; ++i)
    if ((u = Get_Father(E[i].u)) != (v = Get_Father(E[i].v)))
      Father[v] = u, MST[must] += E[i].w;
}

int main() {
  scanf("%d%d", &N, &M);
  scanf("%lld", &X);
  power[0] = 1;
  for (int i = 1; i <= M; ++i)
    E[i].pos = i, scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w),
    power[i] = Add(power[i - 1], power[i - 1]);
  sort(E + 1, E + 1 + M);
  for (int i = 0; i <= M; ++i)
    query(i);
  if (MST[0] > X) {
    printf("0");
    return 0;
  }
  int a = 0, b = 0, c = 0;
  for (int i = 1; i <= M; ++i)
    a += (MST[i] < X), b += (MST[i] == X), c += (MST[i] > X);
  if (MST[0] == X)
    printf("%d", Mult(power[c], Sub(power[b], 2)));
  else
    printf("%d", Mult(2, Mult(Sub(power[b], 1), power[c])));
  return 0;
}