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

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define read(FILE) freopen(FILE, "r", stdin);
#define out(FILE) freopen(FILE, "w", stdout);
#define clr(x, y) memset(x, y, sizeof(x));
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define rep(i, m, n) for (int i = m; i < n; i++)
#define forr(i, m, n) for (int i = m; i <= n; i++)
#define endl '\\n'
#define pb push_back
#define re return
const int N = 1e5 + 5;
const ll mod = 1e9 + 7;
using namespace std;
bool vis[N];
vector<vi> graph;

int main() {
  IO;
  int n, m;
  cin >> n >> m;
  graph.resize(n + 2);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }

  queue<int> qu;
  qu.push(1);
  vis[1] = 1;
  vector<int> ans(n + 1, 0);

  while (!qu.empty()) {
    int siz = qu.size();
    for (int i = 0; i < siz; i++) {
      int v = qu.front();
      qu.pop();

      for (auto u : graph[v]) {
        if (vis[u] == 0) {
          ans[u] = v;
          vis[u] = 1;
          qu.push(u);
        }
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

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


Error: TLE

Fixed submission:

// #include <__msvc_all_public_headers.hpp>
#include <bits/stdc++.h>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
bool bad = 0;
vector<int> ans;
void BFS(vector<vector<int>> &adj, int s, int n, int dest) {
  queue<int> q;
  vector<bool> used(n);
  vector<int> d(n), p(n);
  d.assign(n, 0);
  p.assign(n, -1);
  q.push(s);
  used[s] = true;
  p[s] = -1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    // cout << v << " ";
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }
  if (!used[dest]) {
    bad = 1;
    return;
  }

  else {
    vector<int> path;
    for (int v = dest; v != -1; v = p[v])
      path.pb(v);
    // for (auto i : path) cout << i << " ";
    // cout << endl;
    int sz = path.size();
    ans.pb(path[sz - 2]);
  }
}

int main(void) {
  IO;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(n + 1);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 2; i <= n; i++) {
    bad = 0;
    BFS(adj, i, n + 1, 1);
    if (bad)
      return cout << "NO", 0;
  }
  cout << "Yes" << endl;
  for (auto i : ans)
    cout << i << endl;
}