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

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

using namespace std;
int N, M, Q;
typedef pair<int, int> P;
int U[200001];
int V[200001];
int UV[200001] = {};

vector<int> C[100001];
vector<int> C2[100001];
int R[200001];
int dp[100001] = {};
int man[100001] = {};
const int INF = 10000000;

int main() {
  cin >> N >> M >> Q;

  for (int i = 1; i < M + 1; ++i) {
    cin >> U[i] >> V[i];
    C[U[i]].push_back(i);
    C[V[i]].push_back(i);
  }
  for (int i = 0; i < Q; ++i)
    cin >> R[i];
  for (int i = 2; i < N + 1; ++i)
    dp[i] = INF;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 1));
  P p1;
  int cost, to, to2;
  while (que.size()) {
    p1 = que.top();
    que.pop();
    cost = p1.first;
    to = p1.second;
    if (cost > dp[to])
      continue;
    dp[to] = cost;
    for (auto it = C[to].begin(); it < C[to].end(); ++it) {
      to2 = U[*it];
      if (to2 == to)
        to2 = V[*it];
      if (cost + 1 < dp[to2]) {
        que.push(P(cost + 1, to2));
        dp[to2] = cost + 1;
      } else if (cost + 1 > dp[to2]) {
        C2[to].push_back(*it);
        C[to].erase(it);
        it -= 1;
      }
    }
  }

  int total = 0;
  int sum, flg;
  man[1] = 1;
  queue<int> que2;
  for (int i = 0; i < Q; ++i) {
    sum = 0;
    UV[R[i]] = 1;
    to = V[R[i]];
    to2 = U[R[i]];
    que2.push(to);
    que2.push(to2);

    while (que2.size()) {
      to = que2.front();
      que2.pop();
      flg = 0;

      if (man[to] > 0)
        continue;
      //			cout<<to<<endl;

      for (auto it = C2[to].begin(); it != C2[to].end(); ++it) {
        // if(*it==R[i]){
        //	C2[to].erase(it);
        //	it-=1;
        //	continue;
        // }
        to2 = U[*it];
        if (to2 == to)
          to2 = V[*it];
        if (dp[to2] + 1 + UV[*it] <= dp[to]) {
          flg = 1;
          break;
        }
      }

      if (flg == 0 && to > 1) {
        if (man[to] == 0) {
          man[to] = 1;
          sum += 1;
        }
        for (auto it : C[to]) {
          to2 = U[it];
          if (to2 == to)
            to2 = V[it];
          que2.push(to2);
        }
        dp[to] += 1;
      }
    }
    total += sum;
    //		cout<<dp[2]<<":"<<dp[3]<<":"<<dp[4]<<":"<<dp[5]<<endl;
    cout << total << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

using namespace std;
int N, M, Q;
typedef pair<int, int> P;
int U[200001];
int V[200001];

vector<P> C[100001];
int R[200001];
int dp[100001] = {};
int man[100001] = {};
const int INF = 10000000;

int main() {
  cin >> N >> M >> Q;

  for (int i = 1; i < M + 1; ++i) {
    cin >> U[i] >> V[i];
    C[U[i]].push_back(P(1, i));
    C[V[i]].push_back(P(1, i));
  }
  for (int i = 0; i < Q; ++i)
    cin >> R[i];
  for (int i = 2; i < N + 1; ++i)
    dp[i] = INF;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 1));
  P p1;
  int cost, to, to2;
  while (que.size()) {
    p1 = que.top();
    que.pop();
    cost = p1.first;
    to = p1.second;
    if (cost > dp[to])
      continue;
    dp[to] = cost;
    for (auto it : C[to]) {
      to2 = U[it.second];
      if (to2 == to)
        to2 = V[it.second];
      if (cost + it.first < dp[to2]) {
        que.push(P(cost + it.first, to2));
        dp[to2] = cost + it.first;
      }
    }
  }

  int total = 0;
  int sum, flg;
  man[1] = 1;
  queue<int> que2;
  for (int i = 0; i < Q; ++i) {
    sum = 0;
    if (man[V[R[i]]] == 0) {
      que2.push(V[R[i]]);
      for (auto it = C[V[R[i]]].begin(); it < C[V[R[i]]].end(); ++it) {
        p1 = *it;
        if (p1.second == R[i])
          C[V[R[i]]].erase(it);
      }
    }
    if (man[U[R[i]]] == 0) {
      que2.push(U[R[i]]);
      for (auto it = C[U[R[i]]].begin(); it < C[U[R[i]]].end(); ++it) {
        p1 = *it;
        if (p1.second == R[i])
          C[U[R[i]]].erase(it);
      }
    }
    while (que2.size()) {
      to = que2.front();
      que2.pop();
      flg = 0;
      if (man[to] > 0)
        continue;
      for (auto it : C[to]) {
        to2 = U[it.second];
        if (to2 == to)
          to2 = V[it.second];
        if (dp[to2] + it.first <= dp[to]) {
          flg = 1;
          break;
        }
      }

      if (flg == 0 && to > 1) {
        if (man[to] == 0) {
          man[to] = 1;
          sum += 1;
        }
        for (auto it : C[to]) {
          to2 = U[it.second];
          if (to2 == to)
            to2 = V[it.second];
          if ((dp[to] + it.first <= dp[to2]) && man[to2] == 0)
            que2.push(to2);
        }
        dp[to] += 1;
      }
    }
    total += sum;
    cout << total << endl;
  }
  return 0;
}