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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vbl vector<bool>
#define vvll vector<vll>
#define vld vector<ld>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T> ostream &operator<<(ostream &os, set<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, map<T, R> V) {
  os << "[ ";
  for (auto v : V)
    os << "(" << v.first << ":" << v.second << ")";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, pair<T, R> V) {
  return os << "(" << V.first << "," << V.second << ")";
}

#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
clock_t clk = clock();
#define checktime()                                                            \\
  cout << "\\nTime Elapsed : " << float(clock() - clk) / CLOCKS_PER_SEC << endl
#else
#define trace(...) ;
#define checktime() ;
#endif

const ll Mod = 1e9 + 7;
// const ll Mod = 998244353;
const ll MAXN = 1e6;

vvll g(1e5 + 100);
vll vis(1e5 + 100, 0), dis(1e5 + 100, 1e6), par(1e5 + 100, 1);

void dfs(ll n, ll p = -1) {
  // trace(n,g[n]);
  vis[n] = 1;
  for (ll x : g[n]) {
    if (x != p) {
      if (!vis[x])
        dfs(x, n);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }

  // dfs(1);

  dis[1] = 0;
  queue<ll> qu;
  qu.push(1);
  vis[1] = 1;
  while (qu.size() > 0) {
    ll nd = qu.front();
    qu.pop();
    for (ll x : g[nd]) {
      if (!vis[x]) {
        qu.push(x);
        par[x] = nd;
        vis[x] = 1;
      }
    }
  }

  ll cnt = 0;
  for (ll i = 1; i <= n; i++) {
    if (vis[i]) {
      cnt++;
    }
  }
  if (cnt != n) {
    cout << "No\\n";
    return;
  }

  cout << "Yes\\n";
  for (ll i = 2; i <= n; i++)
    cout << par[i] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(25);

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vbl vector<bool>
#define vvll vector<vll>
#define vld vector<ld>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T> ostream &operator<<(ostream &os, set<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, map<T, R> V) {
  os << "[ ";
  for (auto v : V)
    os << "(" << v.first << ":" << v.second << ")";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, pair<T, R> V) {
  return os << "(" << V.first << "," << V.second << ")";
}

#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
clock_t clk = clock();
#define checktime()                                                            \\
  cout << "\\nTime Elapsed : " << float(clock() - clk) / CLOCKS_PER_SEC << endl
#else
#define trace(...) ;
#define checktime() ;
#endif

const ll Mod = 1e9 + 7;
// const ll Mod = 998244353;
const ll MAXN = 1e6;

vvll g(1e5 + 100);
vll vis(1e5 + 100, 0), dis(1e5 + 100, 1e6), par(1e5 + 100, 1);

void dfs(ll n, ll p = -1) {
  // trace(n,g[n]);
  vis[n] = 1;
  for (ll x : g[n]) {
    if (x != p) {
      if (!vis[x])
        dfs(x, n);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }

  // dfs(1);

  dis[1] = 0;
  queue<ll> qu;
  qu.push(1);
  while (qu.size() > 0) {
    ll nd = qu.front();
    qu.pop();
    vis[nd] = 1;
    for (ll x : g[nd]) {
      if (dis[x] > dis[nd] + 1) {
        dis[x] = dis[nd] + 1;
        par[x] = nd;
      }
      if (!vis[x])
        qu.push(x);
    }
  }

  ll cnt = 0;
  for (ll i = 1; i <= n; i++) {
    if (vis[i]) {
      vis[i] = 0;
      cnt++;
    }
  }
  if (cnt != n) {
    cout << "No\\n";
    return;
  }

  cout << "Yes\\n";
  for (ll i = 2; i <= n; i++)
    cout << par[i] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(25);

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}