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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mod 1000000007
using namespace std;
ll binpow(ll x, ll y) /* (x^y)%p in O(log y) */ {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll binpowmod(ll x, ll y, ll p) /* (x^y)%p in O(log y) */ {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll mod_inverse(ll n, ll p) /* Returns n^(-1) mod p */ {
  return binpowmod(n, p - 2, p);
}
ull gcd(ull x, ull y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
bool comp(pair<ll, ll> x, pair<ll, ll> y) {
  if (x.first < y.second)
    return true;
  else if (x.first == y.second)
    return x.second < y.second;
  else
    return false;
}
bool comp_pairs_by_s(pair<ll, ll> &x, pair<ll, ll> &y) {
  return x.second < y.second;
}
bool isPowerOfTwo(ll x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

class cmp // comparator for priority_queue
{         // declaration: priority_queue<int,vector<int>,cmp>
public:
  bool operator()(pair<int, int> A, pair<int, int> B) {
    if (abs(A.first - A.second) == abs(B.first - B.second))
      return A.first > B.first;
    return abs(A.first - A.second) < abs(B.first - B.second);
  }
};
// int prime[100005]={0};
// void sieve(void)
// {
//  int i,j;
//  for(i=0;i<100005;i++)
//         prime[i]=1;
//  prime[0]=0,prime[1]=0;
//  for(i=2;i<=sqrt(100005);i++){
//      if(prime[i]){
//          for(j=i*i;j<100005;j+=i){
//              prime[j]=0;
//          }
//      }
//  }

// }
void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}
vector<int> g[100005];
int vis[100005];
vector<int> ans(100005, 1);
vector<ll> h(100005);

void dfs(int node) {
  vis[node] = 1;
  for (int to : g[node]) {
    if (vis[to]) {
      if (h[node] <= h[to])
        ans[node] = 0;
    } else {
      if (h[node] <= h[to])
        ans[node] = 0;
      dfs(to);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (ans[i])
      cnt++;
  cout << cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mod 1000000007
using namespace std;
ll binpow(ll x, ll y) /* (x^y)%p in O(log y) */ {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll binpowmod(ll x, ll y, ll p) /* (x^y)%p in O(log y) */ {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll mod_inverse(ll n, ll p) /* Returns n^(-1) mod p */ {
  return binpowmod(n, p - 2, p);
}
ull gcd(ull x, ull y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
bool comp(pair<ll, ll> x, pair<ll, ll> y) {
  if (x.first < y.second)
    return true;
  else if (x.first == y.second)
    return x.second < y.second;
  else
    return false;
}
bool comp_pairs_by_s(pair<ll, ll> &x, pair<ll, ll> &y) {
  return x.second < y.second;
}
bool isPowerOfTwo(ll x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

class cmp // comparator for priority_queue
{         // declaration: priority_queue<int,vector<int>,cmp>
public:
  bool operator()(pair<int, int> A, pair<int, int> B) {
    if (abs(A.first - A.second) == abs(B.first - B.second))
      return A.first > B.first;
    return abs(A.first - A.second) < abs(B.first - B.second);
  }
};
// int prime[100005]={0};
// void sieve(void)
// {
//  int i,j;
//  for(i=0;i<100005;i++)
//         prime[i]=1;
//  prime[0]=0,prime[1]=0;
//  for(i=2;i<=sqrt(100005);i++){
//      if(prime[i]){
//          for(j=i*i;j<100005;j+=i){
//              prime[j]=0;
//          }
//      }
//  }

// }
void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}
vector<unordered_set<int>> g(100001);
int vis[200001];
vector<int> ans(200001, 1);

void dfs(int node, vector<ll> h) {
  vis[node] = 1;
  for (int to : g[node]) {
    if (vis[to]) {
      if (h[node] <= h[to])
        ans[node] = 0;
    } else {
      if (h[node] <= h[to])
        ans[node] = 0;
      dfs(to, h);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<ll> h(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].insert(y);
    g[y].insert(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i, h);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (ans[i])
      cnt++;
  cout << cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}