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

#include <bits/stdc++.h>

using namespace std;

vector<int> directions;
vector<vector<int>> relations;
vector<bool> isVisited;

int N, M;

void bfs() {
  // cout << "\\nbfs start!" << endl;
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int q = que.front();
    que.pop();
    for (int i : relations.at(q)) {
      if (isVisited.at(i))
        continue;
      directions.at(i) = q;
      isVisited.at(i) = true;
      que.push(i);
    }

    // for(int i=1; i<=N; i++){
    //     if(i > relations.at)
    //     if(!isVisited.at(i) && relations.at(q).at(i)){
    //         // cout << i << " ";
    //         directions.at(i) = q;
    //         isVisited.at(i) = true;
    //         que.push(i);
    //     }
    // }
    // cout << endl;
  }
}

void show_isVisited() {
  cout << "isVisited" << endl;
  for (int i = 1; i <= N; i++)
    cout << (isVisited.at(i) ? 1 : 0);
  cout << endl;
}

void show_directions() {
  cout << "directions" << endl;
  for (int i = 1; i <= N; i++)
    cout << directions.at(i) << " ";
  cout << endl;
}

void show_relations() {
  cout << "relations" << endl;
  for (auto relation : relations) {
    for (auto r : relation)
      cout << r << " ";
    cout << endl;
  }
}

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

  // init
  directions = vector<int>(N + 1);
  // directions.at(1) = 0;
  relations = vector<vector<int>>(N + 1, vector<int>(0));
  isVisited = vector<bool>(N + 1, false);
  isVisited.at(1) = true;

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    relations.at(a).push_back(b);
    relations.at(b).push_back(a);
  }

  // show_relations();

  bfs();

  // show_isVisited();
  // show_directions();

  bool res = true;
  for (int i = 1; i <= N; i++) {
    if (!isVisited.at(i)) {
      res = false;
      break;
    }
  }

  if (res) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++)
      cout << directions.at(i) << endl;
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
vector<int> directions;
vector<vector<bool>> relations;
vector<bool> isVisited;

int N, M;

void bfs() {
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int q = que.front();
    que.pop();
    for (int i = 1; i <= N; i++) {
      if (!isVisited.at(i) && relations.at(q).at(i)) {
        directions.at(i) = q;
        isVisited.at(i) = true;
        que.push(i);
      }
    }
  }
}

void show_isVisited() {
  cout << "isVisited" << endl;
  for (int i = 1; i <= N; i++)
    cout << (isVisited.at(i) ? 1 : 0);
  cout << endl;
}

void show_relations() {
  cout << "relations" << endl;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      cout << (relations.at(i).at(j) ? 1 : 0);
    cout << endl;
  }
}

void show_directions() {
  cout << "directions" << endl;
  for (int i = 1; i <= N; i++)
    cout << directions.at(i) << " ";
  cout << endl;
}

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

  // init
  directions = vector<int>(N + 1);
  // directions.at(1) = 0;
  relations = vector<vector<bool>>(N + 1, vector<bool>(N + 1));
  isVisited = vector<bool>(N + 1, false);
  isVisited.at(1) = true;

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    relations.at(a).at(b) = true;
    relations.at(b).at(a) = true;
  }

  // show_relations();

  bfs();

  // show_isVisited();
  // show_directions();

  bool res = true;
  for (int i = 1; i <= N; i++) {
    if (!isVisited.at(i)) {
      res = false;
      break;
    }
  }

  if (!res) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++)
      cout << directions.at(i) << endl;
  }
}