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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define PAIR pair<int, int>
using namespace std;
class road {
public:
  int from, to;
  bool use;
  road() : use(true) {}
};
class town {
public:
  int fast;
  bool used, said;
  vector<class road *> road;
  int num;
  town() : used(false), num(0), said(false) {}
};
int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  town *map;
  map = new town[N + 1];
  road *train;
  train = new road[M * 2 + 1];
  int a, b;
  for (int i = 1; i <= M; ++i) {
    cin >> a >> b;
    train[i].from = a;
    train[i].to = b;
    train[i + M].from = b;
    train[i + M].to = a;
    map[a].road.push_back(&train[i]);
    map[b].road.push_back(&train[i + M]);
  }
  priority_queue<PAIR, vector<PAIR>, greater<PAIR>> que;
  PAIR dummy;
  que.push(PAIR(0, 1));
  while (!que.empty()) {
    dummy = que.top();
    que.pop();
    if (map[dummy.second].used) {
      continue;
    }
    map[dummy.second].used = true;
    map[dummy.second].fast = dummy.first;
    for (int i = 0, nmax = map[dummy.second].road.size(); i < nmax; ++i) {
      if (map[map[dummy.second].road[i]->to].used == false) {
        que.push(PAIR(dummy.first + 1, map[dummy.second].road[i]->to));
      }
    }
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 0, nmax = map[i].road.size(); j < nmax; ++j) {
      if (map[map[i].road[j]->to].fast - map[i].fast == 0) {
        map[i].road[j]->use = false;
        swap(map[i].road[j], map[i].road[nmax - 1]);
        map[i].road.pop_back();
        --nmax;
        --j;
      } else if (map[map[i].road[j]->to].fast - map[i].fast == -1) {
        ++map[i].num;
        map[i].road[j]->use = false;
        swap(map[i].road[j], map[i].road[nmax - 1]);
        map[i].road.pop_back();
        --nmax;
        --j;
      }
    }
  }
  int answer = 0;
  int up;
  queue<int> say;
  for (int i = 0; i < Q; ++i) {
    cin >> up;
    if (train[up].use) {
      say.push(train[up].to);
      train[up].use = false;
    } else if (train[up + M].use) {
      say.push(train[up + M].to);
      train[up + M].use = false;
    }
    while (!say.empty()) {
      int mati = say.front();
      say.pop();
      if (map[mati].said) {
        continue;
      } else if (--map[mati].num != 0) {
        continue;
      }
      ++answer;
      map[mati].said = true;
      for (int i = 0, nmax = map[mati].road.size(); i < nmax; ++i) {
        if (map[mati].road[i]->use) {
          say.push(map[mati].road[i]->to);
          map[mati].road[i]->use = false;
        }
        swap(map[mati].road[i], map[mati].road[nmax - 1]);
        --i;
        --nmax;
        map[mati].road.pop_back();
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define PAIR pair<int, int>
int inf = 20000000;
class map {
public:
  int fast;
  vector<PAIR *> road;
  map() : fast(inf) {}
};

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  map *city;
  city = new map[N + 1];
  PAIR *train;
  train = new PAIR[M * 2 + 1];
  int a, b;
  for (int i = 1; i <= M; ++i) {
    cin >> a >> b;
    train[i].first = b;
    train[i].second = 1;
    train[i + M].first = a;
    train[i + M].second = 1;
    city[a].road.push_back(&train[i]);
    city[b].road.push_back(&train[i + M]);
  }
  int *ofirst;
  ofirst = new int[N + 1];
  bool *used;
  used = new bool[N + 1]{0};
  priority_queue<PAIR, vector<PAIR>, greater<PAIR>> que;
  que.push(PAIR(0, 1));
  PAIR stock;
  while (!que.empty()) {
    stock = que.top();
    que.pop();
    if (used[stock.second])
      continue;
    used[stock.second] = true;
    city[stock.second].fast = stock.first;
    for (int i = 0, nmax = city[stock.second].road.size(); i < nmax; ++i) {
      if (used[city[stock.second].road[i]->first] == false) {
        que.push(PAIR(stock.first + city[stock.second].road[i]->second,
                      city[stock.second].road[i]->first));
      }
    }
  }
  for (int i = 1; i <= N; ++i) {
    ofirst[i] = city[i].fast;
  }
  int dummy;
  for (int i = 0; i < Q; ++i) {
    for (int j = 1; j <= N; ++j) {
      used[j] = false;
    }
    cin >> dummy;
    train[dummy].second = 2;
    train[dummy + M].second = 2;
    que.push(PAIR(0, 1));
    PAIR stock;
    while (!que.empty()) {
      stock = que.top();
      que.pop();
      if (used[stock.second])
        continue;
      used[stock.second] = true;
      city[stock.second].fast = stock.first;
      for (int i = 0, nmax = city[stock.second].road.size(); i < nmax; ++i) {
        if (used[city[stock.second].road[i]->first] == false) {
          que.push(PAIR(stock.first + city[stock.second].road[i]->second,
                        city[stock.second].road[i]->first));
        }
      }
    }
    int answer = 0;
    for (int j = 1; j <= N; ++j) {
      if (city[j].fast != ofirst[j]) {
        ++answer;
      }
    }
    cout << answer << endl;
  }

  return 0;
}