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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void func_A() {

  string s;
  cin >> s;

  if (s == "ABC")
    cout << "ARC";
  else
    cout << "ABC";
}

void func_B() {

  int n, k;
  cin >> n >> k;

  int d, a;
  vector<int> sunuke(n, 0);

  for (int i = 0; i < k; ++i) {
    cin >> d;
    for (int j = 0; j < d; ++j) {
      cin >> a;
      ++sunuke[a - 1];
    }
  }

  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (sunuke[i] == 0)
      ++count;
  }

  cout << count;
}

void func_C() {

  int n, m;
  cin >> n >> m;
  vector<long long> h(n);
  for (int i = 0; i < n; ++i)
    cin >> h[i];

  // 道がつながってるか。0で初期化
  vector<vector<int>> load(n, vector<int>(n, 0));

  // 道についての情報を格納
  int temp1, temp2;
  for (int i = 0; i < m; ++i) {
    cin >> temp1 >> temp2;
    load[temp1 - 1][temp2 - 1] = 1;
    load[temp2 - 1][temp1 - 1] = 1;
  }

  /*入力できてるかデバッグ
  for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
          cout << load[i][j] << " ";
      }
      cout << "\\n";
  }
  */

  // 実行時間超過の予感するけど書く
  // やっぱりした
  int count = 0; // 良い展望台を数える
  for (int i = 0; i < n; ++i) {
    long long tower = h[i];
    for (int j = 0; j < n; ++j) {
      if (load[i][j] == 1 && h[j] >= h[i])
        break;
      if (j == n - 1)
        ++count;
    }
  }

  cout << count;
}

void func_Cfix() {

  int n, m;
  cin >> n >> m;

  vector<long long> h(n);
  for (int i = 0; i < n; ++i)
    cin >> h[i];

  vector<int> judge(n, 1);
  int tower1, tower2;

  for (int i = 0; i < m; ++i) {
    cin >> tower1 >> tower2;
    if (h[tower1 - 1] == h[tower2 - 1]) {
      judge[tower1 - 1] = 0;
      judge[tower2 - 1] = 0;
    } else if (h[tower1 - 1] > h[tower2 - 1]) {
      judge[tower2 - 1] = 0;
    } else {
      judge[tower1 - 1] = 0;
    }
  }

  int count = 0;
  for (int i = 0; i < n; ++i) {
    // cout << judge[i];
    if (judge[i] == 1)
      ++count;
  }

  cout << count;
}
int main() {

  // func_A();
  // func_B();
  // func_C();
  func_Cfix();

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void func_A() {

  string s;
  cin >> s;

  if (s == "ABC")
    cout << "ARC";
  else
    cout << "ABC";
}

void func_B() {

  int n, k;
  cin >> n >> k;

  int d, a;
  vector<int> sunuke(n, 0);

  for (int i = 0; i < k; ++i) {
    cin >> d;
    for (int j = 0; j < d; ++j) {
      cin >> a;
      ++sunuke[a - 1];
    }
  }

  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (sunuke[i] == 0)
      ++count;
  }

  cout << count;
}

void func_C() {

  int n, m;
  cin >> n >> m;
  vector<long long> h(n);
  for (int i = 0; i < n; ++i)
    cin >> h[i];

  // 道がつながってるか。0で初期化
  vector<vector<int>> load(n, vector<int>(n, 0));

  // 道についての情報を格納
  int temp1, temp2;
  for (int i = 0; i < m; ++i) {
    cin >> temp1 >> temp2;
    load[temp1 - 1][temp2 - 1] = 1;
    load[temp2 - 1][temp1 - 1] = 1;
  }

  /*入力できてるかデバッグ
  for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
          cout << load[i][j] << " ";
      }
      cout << "\\n";
  }
  */

  // 実行時間超過の予感するけど書く
  int count = 0; // 良い展望台を数える
  for (int i = 0; i < n; ++i) {
    long long tower = h[i];
    for (int j = 0; j < n; ++j) {
      if (load[i][j] == 1 && h[j] >= h[i])
        break;
      if (j == n - 1)
        ++count;
    }
  }

  cout << count;
}

int main() {

  // func_A();
  // func_B();
  func_C();

  return 0;
}