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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, m, q, a[1 << 18], b[1 << 18], c[1 << 18], d[1 << 18], e[1 << 18],
    dist[1 << 18];
vector<pair<int, int>> x[1 << 18], y[1 << 18];
void reader(int *x) {
  int k = getchar_unlocked();
  *x = k - '0';
  while (true) {
    k = getchar_unlocked();
    if (k < '0' || k > '9')
      break;
    *x = ((*x) << 1) + ((*x) << 3) + k - '0';
  }
}
int main() {
  reader(&n);
  reader(&m);
  reader(&q);
  fill(d, d + m + 1, 1 << 30);
  for (int i = 1; i <= m; i++)
    reader(&a[i]), reader(&b[i]);
  for (int i = 1; i <= q; i++)
    reader(&c[i]), d[c[i]] = i;
  for (int i = 1; i <= m; i++) {
    x[a[i]].push_back(make_pair(b[i], d[i]));
    x[b[i]].push_back(make_pair(a[i], d[i]));
  }
  fill(dist, dist + n + 1, -1);
  dist[1] = 0;
  queue<int> Q1;
  Q1.push(1);
  while (!Q1.empty()) {
    int a1 = Q1.front();
    Q1.pop();
    for (pair<int, int> i : x[a1]) {
      if (dist[i.first] == -1) {
        dist[i.first] = dist[a1] + 1;
        Q1.push(i.first);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (pair<int, int> j : x[i]) {
      if (dist[i] < dist[j.first])
        y[i].push_back(j);
    }
  }
  priority_queue<pair<int, int>> Q;
  Q.push(make_pair(1 << 30, 1));
  e[1] = (1 << 30);
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < y[a2].size(); i++) {
      if (e[y[a2][i].first] < min(y[a2][i].second, a1)) {
        e[y[a2][i].first] = min(y[a2][i].second, a1);
        Q.push(make_pair(e[y[a2][i].first], y[a2][i].first));
      }
    }
  }
  sort(e + 1, e + n + 1);
  int T = 1;
  for (int i = 1; i <= q; i++) {
    while (e[T] <= i)
      T++;
    printf("%d\\n", T - 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, m, q, a[1 << 18], b[1 << 18], c[1 << 18], d[1 << 18], e[1 << 18],
    dist[1 << 18];
vector<pair<int, int>> x[1 << 18], y[1 << 18];
int main() {
  scanf("%d%d%d", &n, &m, &q);
  fill(d, d + m + 1, 1 << 30);
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &a[i], &b[i]);
  for (int i = 1; i <= q; i++)
    scanf("%d", &c[i]), d[c[i]] = i;
  for (int i = 1; i <= m; i++) {
    x[a[i]].push_back(make_pair(b[i], d[i]));
    x[b[i]].push_back(make_pair(a[i], d[i]));
  }
  fill(dist, dist + n + 1, -1);
  dist[1] = 0;
  queue<int> Q1;
  Q1.push(1);
  while (!Q1.empty()) {
    int a1 = Q1.front();
    Q1.pop();
    for (pair<int, int> i : x[a1]) {
      if (dist[i.first] == -1) {
        dist[i.first] = dist[a1] + 1;
        Q1.push(i.first);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (pair<int, int> j : x[i]) {
      if (dist[i] < dist[j.first])
        y[i].push_back(j);
    }
  }
  queue<pair<int, int>> Q;
  Q.push(make_pair(1 << 30, 1));
  e[1] = (1 << 30);
  while (!Q.empty()) {
    int a1 = Q.front().first, a2 = Q.front().second;
    Q.pop();
    for (int i = 0; i < y[a2].size(); i++) {
      if (e[y[a2][i].first] < min(y[a2][i].second, a1)) {
        e[y[a2][i].first] = min(y[a2][i].second, a1);
        Q.push(make_pair(e[y[a2][i].first], y[a2][i].first));
      }
    }
  }
  sort(e + 1, e + n + 1);
  int T = 1;
  for (int i = 1; i <= q; i++) {
    while (e[T] <= i)
      T++;
    printf("%d\\n", T - 1);
  }
  return 0;
}