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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define INF 9999999
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> cost(N + 1, INF);
  std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,
                      std::greater<std::pair<int, int>>>
      NodeCost;
  std::vector<int> prevNode(N + 1, 0);
  std::vector<bool> visit(N + 1, false);
  std::vector<int> unvisitNode(0);
  std::vector<std::vector<int>> neighbor(N + 1, std::vector<int>(0));

  for (int i = 0; i < M; i++) {
    int A, B;
    std::cin >> A >> B;
    neighbor[A].push_back(B);
    neighbor[B].push_back(A);
  }
  for (int i = 1; i < N + 1; i++) {
    unvisitNode.push_back(i);
  }

  std::pair<int, int> curPair;
  curPair.first = 0;
  curPair.second = 1;
  cost[1] = 0;
  NodeCost.push(curPair);
  int h = 0;
  while (h < N + 1) {
    // curNodeを訪問済みに
    visit[curPair.second] = true;

    // curNode隣接ノードのコスト更新
    for (int i = 0; i < neighbor[curPair.second].size(); i++) {
      std::pair<int, int> targetNode;
      targetNode.second = neighbor[curPair.second][i];
      if (visit[targetNode.second] == false &&
          cost[targetNode.second] > cost[curPair.second] + 1) {
        cost[targetNode.second] = cost[curPair.second] + 1;
        prevNode[targetNode.second] = curPair.second;
        targetNode.first = cost[targetNode.second];
        NodeCost.push(targetNode);
      }
    }

    // 次の訪問ノードの決定
    curPair = NodeCost.top();
    NodeCost.pop();
    h++;
  }
  // 完了チェック
  bool complete = true;
  for (int i = 1; i < N + 1; i++) {
    if (visit[i] == false) {
      complete = false;
      break;
    }
  }

  if (!complete) {
    std::cout << "No";
  } else {
    std::cout << "Yes" << std::endl;
    for (int i = 2; i < N + 1; i++) {
      std::cout << prevNode[i] << std::endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define INF 9999999
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> cost(N + 1, INF);
  std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,
                      std::greater<std::pair<int, int>>>
      NodeCost;
  std::vector<int> prevNode(N + 1, 0);
  std::vector<bool> visit(N + 1, false);
  std::vector<int> unvisitNode(0);
  std::vector<std::vector<int>> neighbor(N + 1, std::vector<int>(0));

  for (int i = 0; i < M; i++) {
    int A, B;
    std::cin >> A >> B;
    neighbor[A].push_back(B);
    neighbor[B].push_back(A);
  }
  for (int i = 1; i < N + 1; i++) {
    unvisitNode.push_back(i);
  }

  std::pair<int, int> curPair;
  curPair.first = 0;
  curPair.second = 1;
  cost[1] = 0;
  NodeCost.push(curPair);
  bool complete = false;
  while (!complete) {
    // curNodeを訪問済みに
    visit[curPair.second] = true;

    // curNode隣接ノードのコスト更新
    for (int i = 0; i < neighbor[curPair.second].size(); i++) {
      std::pair<int, int> targetNode;
      targetNode.second = neighbor[curPair.second][i];
      if (visit[targetNode.second] == false &&
          cost[targetNode.second] > cost[curPair.second] + 1) {
        cost[targetNode.second] = cost[curPair.second] + 1;
        prevNode[targetNode.second] = curPair.second;
        targetNode.first = cost[targetNode.second];
        NodeCost.push(targetNode);
      }
    }
    // 完了チェック
    complete = true;
    for (int i = 1; i < N + 1; i++) {
      if (visit[i] == false) {
        complete = false;
        break;
      }
    }
    // 次の訪問ノードの決定
    curPair = NodeCost.top();
    NodeCost.pop();
  }

  if (!complete) {
    std::cout << "No";
  } else {
    std::cout << "Yes" << std::endl;
    for (int i = 2; i < N + 1; i++) {
      std::cout << prevNode[i] << std::endl;
    }
  }
  return 0;
}