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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

#define INF ((65536 / 2) * (65536 / 2))
#define RT_0to1 2
#define RT_1to0 3
#define RT_INV 4

using namespace std;

int city, rail, year, con[200000][2], node[100000], rctype[200000];
vector<int> dest[100000], from[100000];
queue<int> prc;

int main(void) {
  cin >> city >> rail >> year;
  for (int i = 0; i < rail; i++) {
    cin >> con[i][0] >> con[i][1];
    con[i][0]--;
    con[i][1]--;
    dest[con[i][0]].push_back(con[i][1]);
    dest[con[i][1]].push_back(con[i][0]);
  }
  for (int i = 0; i < 100000; i++)
    node[i] = INF;
  node[0] = 0;
  prc.push(0);
  int t;
  auto itr = dest[0].begin();
  while (!prc.empty()) {
    t = prc.front();
    prc.pop();
    for (itr = dest[t].begin(); itr < dest[t].end(); itr++) {
      if (node[*itr] > node[t] + 1) {
        node[*itr] = node[t] + 1;
        prc.push(*itr);
      }
    }
  }
  for (int i = 0; i < 100000; i++) {
    sort(dest[i].begin(), dest[i].end());
  }
  for (int i = 0; i < rail; i++) {
    if (node[con[i][0]] + 1 == node[con[i][1]]) {
      dest[con[i][1]].erase(lower_bound(dest[con[i][1]].begin(),
                                        dest[con[i][1]].end(), con[i][0]));
      rctype[i] = RT_1to0;
    } else if (node[con[i][0]] == node[con[i][1]] + 1) {
      dest[con[i][0]].erase(lower_bound(dest[con[i][0]].begin(),
                                        dest[con[i][0]].end(), con[i][1]));
      rctype[i] = RT_0to1;
    } else {
      dest[con[i][0]].erase(lower_bound(dest[con[i][0]].begin(),
                                        dest[con[i][0]].end(), con[i][1]));
      dest[con[i][1]].erase(lower_bound(dest[con[i][1]].begin(),
                                        dest[con[i][1]].end(), con[i][0]));
      rctype[i] = RT_INV;
    }
  }
  for (int i = 0; i < 100000; i++) {
    for (itr = dest[i].begin(); itr < dest[i].end(); itr++) {
      from[*itr].push_back(i);
    }
  }
  for (int i = 0; i < 100000; i++) {
    sort(from[i].begin(), from[i].end());
  }
  // dest[x].elements ?????????????????????????????????
  // from[x].elements ?????????????????\\????????????
  for (int i = 0; i < 100000; i++) {
    node[i] = 1;
  }
  int result = 0;
  for (int k = 0; k < year; k++) {
    cin >> t;
    t--;
    if (rctype[t] != RT_INV) {
      if (rctype[t] != RT_0to1) {
        dest[con[t][0]].erase(lower_bound(dest[con[t][0]].begin(),
                                          dest[con[t][0]].end(), con[t][1]));
        if (binary_search(from[con[t][1]].begin(), from[con[t][1]].end(),
                          con[t][0]))
          from[con[t][1]].erase(lower_bound(from[con[t][1]].begin(),
                                            from[con[t][1]].end(), con[t][0]));
        prc.push(con[t][1]);
      } else {
        if (binary_search(from[con[t][0]].begin(), from[con[t][0]].end(),
                          con[t][1]))
          from[con[t][0]].erase(lower_bound(from[con[t][0]].begin(),
                                            from[con[t][0]].end(), con[t][1]));
        dest[con[t][1]].erase(lower_bound(dest[con[t][1]].begin(),
                                          dest[con[t][1]].end(), con[t][0]));
        prc.push(con[t][0]);
      }
      while (!prc.empty()) {
        t = prc.front();
        prc.pop();
        if (from[t].empty() && node[t] == 1) {
          result++;
          node[t] = 0;
          for (itr = dest[t].begin(); itr < dest[t].end(); itr++) {
            from[*itr].erase(
                lower_bound(from[*itr].begin(), from[*itr].end(), t));
            prc.push(*itr);
          }
        }
      }
    }
    cout << result << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define INF ((65536 / 2) * (65536 / 2))

using namespace std;

int N, M, Q, R[200000][3], D[100000], O[100000];
bool C[100000];

int main(void) {
  cin >> N >> M >> Q;
  for (int i = 0; i < M; i++) {
    cin >> R[i][1] >> R[i][2];
    R[i][0] = 1;
    R[i][1]--;
    R[i][2]--;
  }
  int S;
  for (int i = 0; i < 100000; i++) {
    C[i] = false;
    O[i] = INF;
  }
  O[0] = 0;
  while (true) {
    bool update = false;
    int mincost = INF;
    int mincosti = -1;
    for (int t = 0; t < N; t++) {
      if (!C[t] && O[t] < mincost) {
        mincost = O[t];
        mincosti = t;
        update = true;
      }
    }
    if (!update)
      break;
    C[mincosti] = true;
    for (int t = 0; t < M; t++) {
      if (R[t][1] == mincosti)
        O[R[t][2]] = min(O[R[t][2]], O[R[t][1]] + R[t][0]);
      if (R[t][2] == mincosti)
        O[R[t][1]] = min(O[R[t][1]], O[R[t][2]] + R[t][0]);
    }
  }
  for (int i = 0; i < Q; i++) {
    cin >> S;
    S--;
    R[S][0] = 2;
    for (int t = 0; t < 100000; t++) {
      C[t] = false;
      D[t] = INF;
    }
    D[0] = 0;
    while (true) {
      bool update = false;
      int mincost = INF;
      int mincosti = -1;
      for (int t = 0; t < N; t++) {
        if (!C[t] && D[t] < mincost) {
          mincost = D[t];
          mincosti = t;
          update = true;
        }
      }
      if (!update)
        break;
      C[mincosti] = true;
      for (int t = 0; t < M; t++) {
        if (R[t][1] == mincosti)
          D[R[t][2]] = min(D[R[t][2]], D[R[t][1]] + R[t][0]);
        if (R[t][2] == mincosti)
          D[R[t][1]] = min(D[R[t][1]], D[R[t][2]] + R[t][0]);
      }
    }
    int res = 0;
    for (int t = 0; t < N; t++) {
      if (D[t] > O[t])
        res++;
    }
    cout << res << endl;
  }
}