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

// ConsoleApplication1.cpp : Defines the entry point for the console
// application.
//

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define MAXCHAR 255

using namespace std;

map<int, vector<int>> accessibles;
map<int, int> indexDistanceMap;
vector<int> shortestRouteIndex;
queue<int> Q;

void Loop(int prevIndex, int currentDistance, set<int> visitedInd) {
  currentDistance++;

  vector<int> possibleRoutes = accessibles[prevIndex];
  for (auto it = possibleRoutes.cbegin(); it != possibleRoutes.cend(); it++) {
    int a = *it; // Next index candidate
    if (visitedInd.find(a) != visitedInd.cend())
      continue;
    visitedInd.insert(a);
    if (indexDistanceMap[a] ==
        0 /* || indexDistanceMap[a] > currentDistance*/) {
      indexDistanceMap[a] = currentDistance;
      shortestRouteIndex[a] = prevIndex;
      Q.push(a);
    }
  }
}

int main() {
  int N = 0;
  int M = 0;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A = 0;
    int B = 0;
    cin >> A >> B;

    accessibles[B - 1].push_back(A - 1);
    accessibles[A - 1].push_back(B - 1);
  }

  shortestRouteIndex = vector<int>(N);
  set<int> visitedIndex;
  Q.push(0);
  for (int i = 0; i < accessibles[0].size(); i++) {
    int currentDistance = 0;
    int nextIndex = accessibles[0][i];
    visitedIndex.clear();
    visitedIndex.insert(0);

    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      Loop(u, currentDistance, visitedIndex);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < shortestRouteIndex.size(); i++) {
    cout << shortestRouteIndex[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// ConsoleApplication1.cpp : Defines the entry point for the console
// application.
//

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define MAXCHAR 255

using namespace std;

map<int, vector<int>> accessibles;
map<int, int> indexDistanceMap;
vector<int> shortestRouteIndex;

void Loop(int prevIndex, int Index, int currentDistance, set<int> visitedInd) {
  currentDistance++;
  if (indexDistanceMap[Index] == 0 ||
      indexDistanceMap[Index] > currentDistance) {
    indexDistanceMap[Index] = currentDistance;
    shortestRouteIndex[Index] = prevIndex;
  } else
    return;

  vector<int> possibleRoutes = accessibles[Index];
  for (auto it = possibleRoutes.cbegin(); it != possibleRoutes.cend(); it++) {
    int a = *it;
    if (visitedInd.find(a) != visitedInd.cend())
      continue;
    visitedInd.insert(a);
    Loop(Index, a, currentDistance, visitedInd);
  }
}

int main() {
  int N = 0;
  int M = 0;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A = 0;
    int B = 0;
    cin >> A >> B;

    accessibles[B - 1].push_back(A - 1);
    accessibles[A - 1].push_back(B - 1);
  }

  shortestRouteIndex = vector<int>(N);
  set<int> visitedIndex;
  for (int i = 0; i < accessibles[0].size(); i++) {
    int currentDistance = 0;
    int nextIndex = accessibles[0][i];
    // int previousIndex = 0;
    visitedIndex.clear();
    visitedIndex.insert(0);

    Loop(0, nextIndex, currentDistance, visitedIndex);
  }
  cout << "Yes" << endl;
  for (int i = 1; i < shortestRouteIndex.size(); i++) {
    cout << shortestRouteIndex[i] + 1 << endl;
  }

  return 0;
}