Problem p02689 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rRep(i, a, b) for (int i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define psortsecond(A, N)                                                      \\
  sort(A, A + N,                                                               \\
       [](const pii &a, const pii &b) { return a.second < b.second; });
#define ALL(x) (x).begin(), (x).end()
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define endl '\\n'
#define mod 1000000007
typedef long long ll;

/*
unsigned gcd(int a, int b) {
        while (1) {
                if (a < b) swap(a, b);
                if (!b) break;
                a %= b;
        }
        return a;
}


ll comb(int n, int r) {
        if (r * 2 > n) r = n - r;
        uintmax_t dividend = 1;
        uintmax_t divisor = 1;
        for (unsigned int i = 1; i <= r; ++i) {
                dividend *= (n - i + 1);
                divisor *= i;
        }
        return dividend / divisor;
}

int compare_char(const void* left, const void* right) {
        char *left_char = (char *)left;
        char *right_char = (char *)right;

        return strcmp(left_char, right_char);
}
*/

int main() {

  //	ios::sync_with_stdio(false);
  //	cin.tie(0);

  // int N, M, *A, *B, i, j, *W, *flag, sum=0, start, end, length, tmp;
  ll i, j, k, l, m, n, len;
  ll C, D, N, M, K, num_R, num_G, num_B, temp;
  ll sum = 0, sum_B = 0, sum_C = 0, max, min, count = 0, Q, W, X, flag = 0;
  ll *A, *B, *H, *ans;

  ll temp1, temp2;

  cin >> N >> M;

  H = (ll *)malloc(sizeof(ll) * N);

  A = (ll *)malloc(sizeof(ll) * M);

  B = (ll *)malloc(sizeof(ll) * M);

  ans = (ll *)malloc(sizeof(ll) * N);

  for (i = 0; i < N; i++)
    cin >> H[i];

  for (i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  for (i = 0; i < N; i++)
    ans[i] = 1;

  for (i = 0; i < M; i++) {
    if (H[A[i]] > H[B[i]]) {
      ans[B[i]] = 0;
    } else if (H[A[i]] == H[B[i]]) {
      ans[A[i]] = 0;
      ans[B[i]] = 0;
    } else {
      ans[A[i]] = 0;
    }
  }

  count = 0;
  for (i = 0; i < N; i++)
    count += ans[i];

  cout << count << endl;

} // 1 2 3 4 5 6


Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rRep(i, a, b) for (int i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define psortsecond(A, N)                                                      \\
  sort(A, A + N,                                                               \\
       [](const pii &a, const pii &b) { return a.second < b.second; });
#define ALL(x) (x).begin(), (x).end()
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define endl '\\n'
#define mod 1000000007
typedef long long ll;

/*
unsigned gcd(int a, int b) {
        while (1) {
                if (a < b) swap(a, b);
                if (!b) break;
                a %= b;
        }
        return a;
}


ll comb(int n, int r) {
        if (r * 2 > n) r = n - r;
        uintmax_t dividend = 1;
        uintmax_t divisor = 1;
        for (unsigned int i = 1; i <= r; ++i) {
                dividend *= (n - i + 1);
                divisor *= i;
        }
        return dividend / divisor;
}

int compare_char(const void* left, const void* right) {
        char *left_char = (char *)left;
        char *right_char = (char *)right;

        return strcmp(left_char, right_char);
}
*/

int main() {

  //	ios::sync_with_stdio(false);
  //	cin.tie(0);

  // int N, M, *A, *B, i, j, *W, *flag, sum=0, start, end, length, tmp;
  ll i, j, k, l, m, n, len;
  ll C, D, N, M, K, num_R, num_G, num_B;
  ll sum = 0, sum_B = 0, sum_C = 0, max, min, count = 0, Q, W, X;
  ll *A, *B, *H, *ans;

  ll temp1, temp2;

  cin >> N >> M;

  H = (ll *)malloc(sizeof(ll) * N);

  A = (ll *)malloc(sizeof(ll) * N);

  B = (ll *)malloc(sizeof(ll) * N);

  ans = (ll *)malloc(sizeof(ll) * N);

  for (i = 0; i < N; i++)
    cin >> H[i];

  for (i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  for (i = 0; i < N; i++)
    ans[i] = 1;

  for (i = 0; i < M; i++) {
    if (H[A[i]] > H[B[i]]) {
      ans[B[i]] = 0;
    } else if (H[A[i]] == H[B[i]]) {
      ans[A[i]] = 0;
      ans[B[i]] = 0;
    } else {
      ans[A[i]] = 0;
    }
  }

  count = 0;
  for (i = 0; i < N; i++)
    count += ans[i];

  cout << count << endl;

  return 0;

} // 1 2 3 4 5 6