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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int ky[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int kx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef long long ll;
#define pi pair
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define rep1(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define LB lower_bound
#define UP upper_bound
#define INF 999999999999
const double PI = acos(-1.0);
int N;
ll M, H;
ll h[100001];
ll mx[100001];
ll ans;
ll u, v;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> N >> M;
  rep2(i, 1, N) {
    cin >> h[i];
    mx[i] = 0;
  }

  rep1(i, 0, M) {
    cin >> u >> v;
    mx[u] = max(mx[u], h[v]);
    mx[v] = max(mx[v], h[u]);
  }

  rep2(i, 1, N) if (mx[i] < h[i]) ans++;
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int ky[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int kx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef long long ll;
#define pi pair
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define rep1(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define LB lower_bound
#define UP upper_bound
#define INF 999999999999
const double PI = acos(-1.0);
int n, m;
vector<int> adj[100001];
ll h[100001];
bool vis[100001];
set<pi<int, int>> s;
ll mx;
int ans;
int a, b;
int cnt;
bool ok;
int dup;

void bfs(int s) {
  queue<int> q;
  int depth = 0;
  q.push(s);
  vis[s] = true;
  ok = false;

  while (!q.empty()) {
    int size = q.size();
    rep1(i, 0, size) {
      int here = q.front();
      q.pop();
      cnt++;
      if (depth == 1) {
        if (mx == h[here])
          dup++;
        mx = max(mx, h[here]);
      }

      rep1(j, 0, (int)adj[here].size()) {
        int next = adj[here][j];
        if (vis[next])
          continue;
        vis[next] = true;
        q.push(next);
      }
    }
    depth++;
    if (depth >= 2)
      break;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n >> m;
  rep2(i, 1, n) cin >> h[i];
  rep1(i, 0, m) {
    cin >> a >> b;
    swap(a, b);
    if (s.count({a, b}))
      continue;
    s.insert({a, b});
    adj[a].PB(b);
    adj[b].PB(a);
  }

  rep2(i, 1, n) {
    cnt = dup = 0;
    mx = h[i];
    memset(vis, false, sizeof(vis));
    bfs(i);
    //   cout << mx << ' ';
    if (cnt == 1 || (mx == h[i] && !dup))
      ans++;
  }

  cout << ans;
  return 0;
}