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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\\n";
  }
  return out;
}

vector<vector<int>> graph;
vector<int> signpost, visited;

void bfs(int u) {
  deque<int> vertices;
  vertices.push_back(u);
  visited[u] = 1;

  while (!vertices.empty()) {
    int v = vertices.front();
    vertices.pop_front();

    for (int i = 0; i < graph[v].size(); ++i) {
      if (!visited[graph[v][i]]) {
        visited[graph[v][i]] = 1;
        signpost[graph[v][i]] = v;
        vertices.push_back(graph[v][i]);
      }
    }
  }
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;

  graph = vector<vector<int>>(n + 1, vector<int>());
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  signpost = vector<int>(n + 1);
  visited = vector<int>(n + 1);

  bfs(1);

  for (int i = 2; i <= n; ++i) {
    if (signpost[i] == 0) {
      cout << "No\\n";
      return 0;
    }
  }

  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i) {
    cout << signpost[i] << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\\n";
  }
  return out;
}

vector<vector<int>> graph;
vector<int> signpost, least;

void dfs(int u, int dist, int parent) {
  ++dist;
  if (!signpost[u] || dist < least[u]) {
    signpost[u] = parent;
    least[u] = dist;
  } else {
    return;
  }

  for (int i = 0; i < graph[u].size(); ++i) {
    if (!signpost[graph[u][i]] || least[graph[u][i]] > dist) {
      dfs(graph[u][i], dist, u);
    }
  }
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;

  graph = vector<vector<int>>(n + 1, vector<int>());
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  signpost = vector<int>(n + 1);
  least = vector<int>(n + 1, INT_MAX);

  dfs(1, 0, -1);

  for (int i = 2; i <= n; ++i) {
    if (signpost[i] == 0) {
      cout << "No\\n";
      return 0;
    }
  }

  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i) {
    cout << signpost[i] << "\\n";
  }

  return 0;
}