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

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
constexpr ll INF = 1e18;

vector<vector<int>> graph;
vector<int> visited;
vector<int> guide;

void bfs(int i) {
  queue<int> que;
  queue<int> guide_que;
  que.push(i);
  guide_que.push(-1);
  while (!que.empty()) {
    int f = que.front();
    que.pop();
    int g = guide_que.front();
    guide_que.pop();
    if (guide[f] == 0)
      guide[f] = g;
    for (auto &&n : graph[f]) {
      if (visited[n])
        continue;
      visited[n] = 1;
      que.push(n);
      guide_que.push(f + 1);
    }
  }
}

int main(void) {
  ll N, M;
  cin >> N >> M;
  visited = vector<int>(N, 0);
  graph = vector<vector<int>>(N, vector<int>());
  guide = vector<int>(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  bfs(0);

  // for (int i = 0; i < N; i++) {
  //     if (guide[i] == 0) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << guide[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
constexpr ll INF = 1e18;

vector<vector<int>> graph;
vector<int> guide;

void bfs(int i) {
  queue<int> que;
  queue<int> guide_que;
  que.push(i);
  guide_que.push(-1);
  while (!que.empty()) {
    int f = que.front();
    que.pop();
    int g = guide_que.front();
    guide_que.pop();
    if (guide[f] == 0)
      guide[f] = g;
    for (auto &&n : graph[f]) {
      if (guide[n])
        continue;
      que.push(n);
      guide_que.push(f + 1);
    }
  }
}

int main(void) {
  ll N, M;
  cin >> N >> M;
  graph = vector<vector<int>>(N, vector<int>());
  guide = vector<int>(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  bfs(0);

  // for (int i = 0; i < N; i++) {
  //     if (guide[i] == 0) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << guide[i] << endl;
  }

  return 0;
}