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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\\t" #x " = " << (x) << endl

using pii = pair<int, int>;

using Graph = vector<vector<pii>>;
int n, m, Q;
Graph g;

int u[200010], v[200010], er[200010];
pii dist[100010];
int ans[200010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> m >> Q;
  g.assign(n, {});
  rep(i, m) {
    cin >> u[i] >> v[i];
    --u[i];
    --v[i];
    er[i] = 1e9;
  }

  rep(i, Q) {
    int r;
    cin >> r;
    --r;
    er[r] = i;
  }

  rep(i, m) {
    int &u = ::u[i], &v = ::v[i], &e = er[i];
    g[u].emplace_back(v, e);
    g[v].emplace_back(u, e);
  }

  using state = tuple<int, int, int>;
  priority_queue<state> q;
  rep(i, n) dist[i] = pii(1e9, -1e9);
  dist[0].first = 0;
  dist[0].second = 1e9;
  q.emplace(0, 1e9, 0);
  while (q.size()) {
    int cd, ct, cv;
    tie(cd, ct, cv) = q.top();
    cd = -cd;
    q.pop();
    if (dist[cv].first < cd)
      continue;
    for (auto &e : g[cv]) {
      int nd = cd + 1, nt, nv;
      tie(nv, nt) = e;
      nt = min(ct, nt);
      if (dist[nv].first > nd) {
        dist[nv] = pii(nd, nt);
        q.emplace(-nd, nt, nv);
      } else if (dist[nv].first == nd && dist[nv].second < nt) {
        dist[nv] = pii(nd, nt);
        q.emplace(-nd, nt, nv);
      }
    }
  }

  rep(i, n) {
    if (dist[i].second != 1e9)
      ++ans[dist[i].second];
  }

  rep(i, Q) {
    cout << ans[i] << '\\n';
    ans[i + 1] += ans[i];
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\\t" #x " = " << (x) << endl

using pii = pair<int, int>;

using Graph = vector<vector<pii>>;
int n, m, Q;
Graph g;

int u[200010], v[200010];
int er[200010];
pii dist[100010];
int ans[100010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  while (cin >> n >> m >> Q) {
    g.assign(n, {});
    rep(i, m) {
      cin >> u[i] >> v[i];
      --u[i];
      --v[i];
      er[i] = 1e9;
    }

    rep(i, Q) {
      int r;
      cin >> r;
      --r;
      er[r] = i;
    }

    rep(i, m) {
      int &u = ::u[i], &v = ::v[i], &e = er[i];
      g[u].emplace_back(v, e);
      g[v].emplace_back(u, e);
    }

    using state = tuple<int, int, int>;
    priority_queue<state, vector<state>, greater<state>> q;
    rep(i, n) dist[i] = pii(1e9, -1e9);
    dist[0].first = 0;
    dist[0].second = 1e9;
    q.emplace(0, 1e9, 0);
    while (q.size()) {
      int cd, ct, cv;
      tie(cd, ct, cv) = q.top();
      q.pop();
      if (dist[cv].first < cd)
        continue;
      for (auto &e : g[cv]) {
        int nd = cd + 1, nt, nv;
        tie(nv, nt) = e;
        nt = min(ct, nt);
        if (dist[nv].first > nd) {
          dist[nv] = pii(nd, nt);
          q.emplace(nd, nt, nv);
        } else if (dist[nv].first == nd && dist[nv].second < nt) {
          dist[nv] = pii(nd, nt);
          q.emplace(nd, nt, nv);
          // dump(e.first);
          // dump(e.second);
          // dump(ct);
          // dump(cv);
          // dump(nv);
          // dump(nd);
          // dump(nt);
        }
      }
    }

    // rep(i, n){
    //     cout << dist[i].first << ' ' << dist[i].second << endl;
    // }

    memset(ans, 0, sizeof ans);
    rep(i, n) {
      if (dist[i].second != 1e9)
        ++ans[dist[i].second];
    }

    rep(i, Q) {
      cout << ans[i] << '\\n';
      ans[i + 1] += ans[i];
    }
  }
}