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];
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= m; i++) {
    d[i] = (1 << 30);
    e[i] = 0;
  }
  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);
      }
    }
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>, less<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 <bits/stdc++.h>
using namespace std;
int n, m, q, a[1 << 18], b[1 << 18], c, dist[1 << 18], T[1 << 18], cnt,
    U[1 << 18];
vector<pair<int, int>> x[110000], y[110000];
bool used[210000];
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    x[a[i]].push_back(make_pair(b[i], i));
    x[b[i]].push_back(make_pair(a[i], i));
  }
  fill(dist, dist + n + 1, -1);
  dist[1] = 0;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();
    for (pair<int, int> i : x[a1]) {
      if (dist[i.first] == -1) {
        dist[i.first] = dist[a1] + 1;
        Q.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);
        T[j.first]++;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    scanf("%d", &c);
    c--;
    queue<pair<int, int>> Q1;
    int ss = -1;
    if (dist[a[c]] < dist[b[c]])
      ss = b[c];
    if (dist[a[c]] > dist[b[c]])
      ss = a[c];
    if (ss >= 1 && U[ss] == 0 && used[c] == false) {
      Q1.push(make_pair(ss, c));
      while (!Q1.empty()) {
        int a1 = Q1.front().first, a2 = Q1.front().second;
        Q1.pop();
        if (U[a1] == 1 || used[a2] == true)
          continue;
        T[a1]--;
        used[a2] = true;
        if (T[a1] != 0)
          continue;
        cnt++;
        U[a1] = 1;
        for (pair<int, int> j : y[a1]) {
          if (U[j.first] == 0)
            Q1.push(j);
        }
      }
    }
    printf("%d\\n", cnt);
  }
}