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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef long double lld;
typedef long long int lli;
using namespace std;
const int N = 1000001;
const int MOD = 1e9 + 7;
const bool DEBUG = 1;
#define sd(x) scanf("%d ", &x)
#define sd2(x, y) scanf("%d%d", &x, &y)
#define sd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define endl "\\n"
#define fi first
#define se second
#define eb emplace_back
#define fbo find_by_order
#define ook order_of_key
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define LET(x, a) __typeof(a) x(a)
#define foreach(it, v) for (LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define __                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define all(c) c.begin(), c.end()
#define inf 1000000000000000001
#define epsilon 1e-6
#define int ll
#define RUN_T                                                                  \\
  int _t;                                                                      \\
  cin >> _t;                                                                   \\
  while (_t--)
#define tr(...)                                                                \\
  if (DEBUG) {                                                                 \\
    cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                          \\
    trace(#__VA_ARGS__, __VA_ARGS__);                                          \\
  }
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.fi << ", " << p.se << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, set<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << "\\n" << (i->first) << ":" << (i->second);
  return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, unordered_map<T, V> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << "\\n" << (i->first) << ":" << (i->second);
  return out;
}
template <typename T> ostream &operator<<(ostream &out, multiset<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_set<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_multiset<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  ll l = v.size();
  for (ll i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}
template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << ":" << arg1 << endl;
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ":" << arg1 << "|";
  trace(comma + 1, args...);
}
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<int> dist, par, vis;
vector<vector<int>> g;
void bfs(int x) {
  queue<int> q;
  q.push(x);
  vis[x] = 1;
  dist[x] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : g[v]) {
      if (!vis[u]) {
        vis[u] = 1;
        dist[u] = dist[v] + 1;
        q.push(u);
        par[u] = v;
      }
    }
  }
}
int32_t main() {
  _ int n, m, x, y;
  cin >> n >> m;
  g.resize(n + 1);
  par.resize(n + 1);
  vis.resize(n + 1, 0);
  dist.resize(n + 1, 1e9);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  bfs(1);
  for (int i = 2; i <= n; i++) {
    if (dist[i] >= 1e9) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << par[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef long double lld;
typedef long long int lli;
using namespace std;
const int N = 1000001;
const int MOD = 1e9 + 7;
const bool DEBUG = 1;
#define sd(x) scanf("%d ", &x)
#define sd2(x, y) scanf("%d%d", &x, &y)
#define sd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define endl "\\n"
#define fi first
#define se second
#define eb emplace_back
#define fbo find_by_order
#define ook order_of_key
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define LET(x, a) __typeof(a) x(a)
#define foreach(it, v) for (LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define __                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define all(c) c.begin(), c.end()
#define inf 1000000000000000001
#define epsilon 1e-6
#define int ll
#define RUN_T                                                                  \\
  int _t;                                                                      \\
  cin >> _t;                                                                   \\
  while (_t--)
#define tr(...)                                                                \\
  if (DEBUG) {                                                                 \\
    cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                          \\
    trace(#__VA_ARGS__, __VA_ARGS__);                                          \\
  }
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.fi << ", " << p.se << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, set<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << "\\n" << (i->first) << ":" << (i->second);
  return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, unordered_map<T, V> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << "\\n" << (i->first) << ":" << (i->second);
  return out;
}
template <typename T> ostream &operator<<(ostream &out, multiset<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_set<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_multiset<T> const &v) {
  for (auto i = v.begin(); i != v.end(); i++)
    out << (*i) << ' ';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  ll l = v.size();
  for (ll i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}
template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << ":" << arg1 << endl;
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ":" << arg1 << "|";
  trace(comma + 1, args...);
}
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<int> dist, par;
vector<vector<int>> g;
void bfs(int n) {
  vector<int> vis(n + 1, 0);
  queue<int> q;
  q.push(1);
  dist[1] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = 1;
    for (int p : g[x]) {
      if (vis[p] != 1) {
        q.push(p);
      }
      if (dist[x] + 1 < dist[p]) {
        dist[p] = min(dist[x] + 1, dist[p]);
        par[p] = x;
      }
    }
  }
}
int32_t main() {
  _ int n, m, x, y;
  cin >> n >> m;
  g.resize(n + 1);
  par.resize(n + 1);
  dist.clear();
  dist.resize(n + 1, 1e9);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  bfs(n);
  for (int i = 2; i <= n; i++) {
    if (dist[i] >= 1e9) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << par[i] << endl;
  }
}