Problem p02689 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  int n, m;
  cin >> n >> m;
  VI h(n, 0);
  rep(i, n) { cin >> h[i]; }
  VI hm(n, -1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    chmax(hm[a], h[b]);
    chmax(hm[b], h[a]);
  }
  int ans = 0;
  rep(i, n) {
    if (h[i] > hm[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  int n = 0, m = 0;
  cin >> n >> m;
  VI h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  VVI t(n, VI(n, 0));
  for (int i = 0; i < m; i++) {
    int a = 0, b = 0;
    cin >> a >> b;
    a--;
    b--;
    if (h[a] < h[b]) {
      t[a][b]++;
    } else if (h[b] < h[a]) {
      t[b][a]++;
    } else if (h[a] == h[b]) {
      t[a][b]++;
      t[b][a]++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    bool ok = true;
    for (int j = 0; j < n; j++) {
      if (t[i][j] > 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      ans++;
  }
  cout << ans << endl;
  return 0;
}