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]) {
      to2 = U[it];
      if (to2 == to)
        to2 = V[it];
      if (cost + 1 < dp[to2]) {
        que.push(P(cost + 1, to2));
        dp[to2] = cost + 1;
      }
    }
  }
  for (int i = 2; i < N + 1; ++i) {
    for (auto it : C[i]) {
      if (dp[i] > dp[V[it]])
        C2[i].push_back(it);
      if (dp[i] > dp[U[it]])
        C2[i].push_back(it);
    }
  }

  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;
      for (auto it : C2[to]) {
        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];
          if (man[to2] == 0)
            que2.push(to2);
        }
        dp[to] += 1;
      }
    }
    total += sum;
    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];
int UV[200001] = {};

vector<int> 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(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;
      }
    }
  }

  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;
      for (auto it : C[to]) {
        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];
          if (man[to2] == 0)
            que2.push(to2);
        }
        dp[to] += 1;
      }
    }
    total += sum;
    cout << total << endl;
  }
  return 0;
}