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

#include <deque>
#include <iostream>
#include <vector>

struct BFS {
  size_t N;
  size_t M;
  std::vector<std::vector<size_t>> Graph;
  std::vector<size_t> dist;
  std::vector<std::vector<size_t>> distMap;
  std::deque<size_t> que;
  std::vector<size_t> arrow;

  BFS() {
    std::cin >> N;
    std::cin >> M;

    Graph.assign(N, std::vector<size_t>());
    for (size_t i = 0; i != M; i++) {
      size_t j1, j2;
      std::cin >> j1 >> j2;
      j1--;
      j2--;
      Graph[j1].push_back(j2);
      Graph[j2].push_back(j1);
    }

    dist.assign(N, -1);
    distMap.assign(N, std::vector<size_t>());
    arrow.assign(N, -1);
  }

  void find(size_t pos) {
    dist[pos] = 0;
    distMap[dist[pos]].push_back(pos);
    que.push_back(pos);

    while (que.empty() == false) {
      pos = que.front();
      que.pop_front();

      for (size_t i = 0; i != Graph[pos].size(); i++) {
        size_t nextPos = Graph[pos][i];

        if (dist[nextPos] != -1)
          continue;

        dist[nextPos] = dist[pos] + 1;
        distMap[dist[nextPos]].push_back(nextPos);
        que.push_back(nextPos);
        arrow[nextPos] = pos;
      }
    }
  }
};

int main() {
  BFS f;
  f.find(0);

  for (size_t i = 1; i != f.arrow.size(); i++) {
    if (f.arrow[i] == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }

  std::cout << "Yes" << std::endl;
  for (size_t i = 1; i != f.arrow.size(); i++) {
    std::cout << f.arrow[i] + 1 << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <vector>

struct BFS {
  size_t N;
  size_t M;
  std::vector<std::vector<size_t>> Graph;
  std::vector<size_t> dist;
  std::vector<std::vector<size_t>> distMap;
  std::deque<size_t> que;
  std::vector<size_t> arrow;

  BFS() {
    std::cin >> N;
    std::cin >> M;

    Graph.assign(N, std::vector<size_t>());
    for (size_t i = 0; i != M; i++) {
      size_t j1, j2;
      std::cin >> j1 >> j2;
      j1--;
      j2--;
      Graph[j1].push_back(j2);
      Graph[j2].push_back(j1);
    }

    dist.assign(N, -1);
    distMap.assign(N, std::vector<size_t>());
    arrow.assign(N, -1);
  }

  void find(size_t pos) {
    dist[pos] = 0;
    distMap[dist[pos]].push_back(pos);
    que.push_back(pos);

    while (que.empty() == false) {
      pos = que.front();
      que.pop_front();

      for (size_t i = 0; i != Graph[pos].size(); i++) {
        size_t nextPos = Graph[pos][i];

        if (dist[nextPos] != -1)
          continue;

        dist[nextPos] = dist[pos] + 1;
        distMap[dist[nextPos]].push_back(nextPos);
        que.push_back(nextPos);
      }
    }

    for (size_t i = 1; i != N; i++) {
      std::vector<size_t> v1(N, 0);
      for (auto e : distMap[dist[i] - 1]) {
        v1[e]++;
      }
      std::vector<size_t> v2(N, 0);
      for (auto e : Graph[i]) {
        if (v1[e] != 0) {
          arrow[i] = e;
        }
      }
    }
  }
};

int main() {
  BFS f;
  f.find(0);

  for (size_t i = 1; i != f.arrow.size(); i++) {
    if (f.arrow[i] == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }

  std::cout << "Yes" << std::endl;
  for (size_t i = 1; i != f.arrow.size(); i++) {
    std::cout << f.arrow[i] + 1 << std::endl;
  }

  return 0;
}