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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m, q;
  cin >> n >> m >> q;
  vector<vector<Int>> G(n);
  auto T = G;
  vector<Int> u(m), v(m);
  vector<map<Int, Int>> R(n);
  for (Int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    G[u[i]].emplace_back(v[i]);
    G[v[i]].emplace_back(u[i]);
    R[u[i]][v[i]] = i;
    R[v[i]][u[i]] = i;
  }
  vector<Int> dp(n, -1);
  {
    dp[0] = 0;
    queue<Int> qu({0});
    while (!qu.empty()) {
      Int v = qu.front();
      qu.pop();
      for (Int u : G[v]) {
        if (~dp[u])
          continue;
        dp[u] = dp[v] + 1;
        qu.emplace(u);
      }
    }
  }
  vector<Int> b(q), c(m, q);
  for (Int i = 0; i < q; i++) {
    cin >> b[i];
    b[i]--;
    c[b[i]] = i;
  }
  vector<Int> dp2(n, -1);
  {
    using P = pair<Int, Int>;
    priority_queue<P> qu;
    dp2[0] = q;
    qu.emplace(q, 0);
    while (!qu.empty()) {
      P p = qu.top();
      qu.pop();
      Int d = p.first, x = p.second;
      // cout<<x<<":"<<d<<endl;
      if (dp2[x] > d)
        continue;
      for (Int y : G[x]) {
        if (dp[x] + 1 != dp[y])
          continue;
        if (min(d, c[R[x][y]]) <= dp2[y])
          continue;
        dp2[y] = min(d, c[R[x][y]]);
        qu.emplace(dp2[y], y);
      }
    }
  }

  vector<Int> a(q + 1, 0);
  for (Int i = 0; i < n; i++)
    a[dp2[i]]++;
  for (Int i = q; i > 0; i--)
    a[i - 1] += a[i];
  for (Int i = 1; i <= q; i++)
    cout << n - a[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m, q;
  cin >> n >> m >> q;
  vector<vector<Int>> G(n);
  auto T = G;
  vector<Int> u(m), v(m);
  vector<map<Int, Int>> R(n);
  for (Int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    G[u[i]].emplace_back(v[i]);
    G[v[i]].emplace_back(u[i]);
    R[u[i]][v[i]] = i;
    R[v[i]][u[i]] = i;
  }
  vector<Int> dp(n, -1);
  {
    dp[0] = 0;
    queue<Int> qu({0});
    while (!qu.empty()) {
      Int v = qu.front();
      qu.pop();
      for (Int u : G[v]) {
        if (~dp[u])
          continue;
        dp[u] = dp[v] + 1;
        qu.emplace(u);
      }
    }
  }
  vector<Int> b(q), c(m, q);
  for (Int i = 0; i < q; i++) {
    cin >> b[i];
    b[i]--;
    c[b[i]] = i;
  }
  vector<Int> dp2(n, -1);
  {
    using P = pair<Int, Int>;
    priority_queue<P, vector<P>, greater<P>> qu;
    dp2[0] = q;
    qu.emplace(q, 0);
    while (!qu.empty()) {
      P p = qu.top();
      qu.pop();
      Int d = p.first, x = p.second;
      // cout<<x<<":"<<d<<endl;
      if (dp2[x] > d)
        continue;
      for (Int y : G[x]) {
        if (dp[x] + 1 != dp[y])
          continue;
        if (min(d, c[R[x][y]]) <= dp2[y])
          continue;
        dp2[y] = min(d, c[R[x][y]]);
        qu.emplace(dp2[y], y);
      }
    }
  }

  vector<Int> a(q + 1, 0);
  for (Int i = 0; i < n; i++)
    a[dp2[i]]++;
  for (Int i = q; i > 0; i--)
    a[i - 1] += a[i];
  for (Int i = 1; i <= q; i++)
    cout << n - a[i] << endl;
  return 0;
}