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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

struct aEdge {
  int u;
  int v;
};
typedef pair<int, int> Edge;

vector<int> gettimes(const int N, const vector<Edge> &alles) {
  vector<vector<Edge>> es(N);
  for (auto e : alles) {
    es[e.first].push_back(e);
    es[e.second].push_back(Edge{e.second, e.first});
  }
  vector<int> times(N, 1e9);
  queue<int> que;
  que.push(0);
  times[0] = 0;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (const auto &e : es[now]) {
      if (times[now] + 1 < times[e.second]) {
        times[e.second] = times[now] + 1;
        que.push(e.second);
      }
    }
  }
  return times;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<Edge> alles;
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    alles.push_back(Edge{u, v});
  }
  vector<int> times = gettimes(N, alles);
  vector<int> qs(Q);
  vector<int> used(M);
  for (int i = 0; i < Q; ++i) {
    int n;
    cin >> n;
    n--;
    qs[i] = n;
    used[n] = true;
  }
  vector<vector<Edge>> es(N);
  for (int i = 0; i < M; ++i) {
    if (!used[i]) {
      Edge e = alles[i];
      es[e.first].push_back(e);
      es[e.second].push_back(Edge{e.second, e.first});
    }
  }
  vector<int> canmove(N);
  canmove[0] = true;
  queue<int> que;
  que.push(0);
  int monku = N - 1;
  vector<int> anss;

  for (int q = Q - 1; q >= 0; --q) {
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (canmove[now]) {
        vector<Edge> newes;
        for (const auto &e : es[now]) {
          if (!canmove[e.second] && times[e.second] == times[e.first] + 1) {
            canmove[e.second] = true;
            que.push(e.second);
            monku--;
          } else {
            newes.emplace_back(e);
          }
        }
        es[now] = newes;
      }
    }
    anss.emplace_back(monku);
    Edge e = alles[qs[q]];
    es[e.first].push_back(e);
    es[e.second].push_back(Edge{e.second, e.first});
    if (canmove[e.first] ^ canmove[e.second]) {
      if (canmove[e.first]) {
        que.push(e.first);
      }
      if (canmove[e.second]) {

        que.push(e.second);
      }
    }
  }
  for (int i = anss.size() - 1; i >= 0; --i) {
    cout << anss[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

struct aEdge {
  int u;
  int v;
};
typedef pair<int, int> Edge;

vector<int> gettimes(const int N, const vector<Edge> &alles) {
  vector<vector<Edge>> es(N);
  for (auto e : alles) {
    es[e.first].push_back(e);
    es[e.second].push_back(Edge{e.second, e.first});
  }
  vector<int> times(N, 1e9);
  queue<int> que;
  que.push(0);
  times[0] = 0;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (const auto &e : es[now]) {
      if (times[now] + 1 < times[e.second]) {
        times[e.second] = times[now] + 1;
        que.push(e.second);
      }
    }
  }
  return times;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<Edge> alles;
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    alles.push_back(Edge{u, v});
  }
  vector<int> times = gettimes(N, alles);
  vector<int> qs(Q);
  vector<int> used(M);
  for (int i = 0; i < Q; ++i) {
    int n;
    cin >> n;
    n--;
    qs[i] = n;
    used[n] = true;
  }
  vector<vector<Edge>> es(N);
  for (int i = 0; i < M; ++i) {
    if (!used[i]) {
      Edge e = alles[i];
      es[e.first].push_back(e);
      es[e.second].push_back(Edge{e.second, e.first});
    }
  }
  vector<int> canmove(N);
  canmove[0] = true;
  queue<int> que;
  que.push(0);
  int monku = N - 1;
  vector<int> anss;

  for (int q = Q - 1; q >= 0; --q) {
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (canmove[now]) {
        for (const auto &e : es[now]) {
          if (!canmove[e.second] && times[e.second] == times[e.first] + 1) {
            canmove[e.second] = true;
            que.push(e.second);
            monku--;
          }
        }
      }
    }
    anss.emplace_back(monku);
    Edge e = alles[qs[q]];
    es[e.first].push_back(e);
    es[e.second].push_back(Edge{e.second, e.first});
    if (canmove[e.first] ^ canmove[e.second]) {
      if (canmove[e.first]) {
        que.push(e.first);
      }
      if (canmove[e.second]) {

        que.push(e.second);
      }
    }
  }
  for (int i = anss.size() - 1; i >= 0; --i) {
    cout << anss[i] << endl;
  }

  return 0;
}