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

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define lli long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()

#define forn(i, n) for (int i = 0; i < n; ++i)
#define rforn(i, start) for (int i = start; i >= 0; --i)
#define forsn(i, start, end) for (int i = start; i < end; ++i)
#define rforsn(i, start, end) for (int i = start; i >= end; --i)
#define debug(x) cout << #x << " = " << x << "\\n";
const int CONST = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt","w",stdout);
#endif

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

  vector<vector<int>> adj(n + 1, vector<int>());
  forn(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  int par[n + 1];
  par[1] = 0;
  queue<int> q;
  q.push(1);
  memset(par, -1, sizeof(par));

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : adj[u]) {
      if (par[v] == -1) {
        par[v] = u;
        q.push(v);
      }
    }
  }

  forsn(i, 1, n + 1) {
    if (par[i] == -1) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  forsn(i, 2, n + 1) { cout << par[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define lli long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()

#define forn(i, n) for (int i = 0; i < n; ++i)
#define rforn(i, start) for (int i = start; i >= 0; --i)
#define forsn(i, start, end) for (int i = start; i < end; ++i)
#define rforsn(i, start, end) for (int i = start; i >= end; --i)
#define debug(x) cout << #x << " = " << x << "\\n";
const int CONST = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt","w",stdout);
#endif

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

  vector<vector<int>> adj(n + 1, vector<int>());
  forn(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  int par[n + 1];
  par[1] = 0;
  queue<int> q;
  q.push(1);
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  memset(par, -1, sizeof(par));

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = true;
    for (int v : adj[u]) {
      if (vis[v])
        continue;

      if (par[v] == -1)
        par[v] = u;
      q.push(v);
    }
  }

  forsn(i, 1, n + 1) {
    if (!vis[i]) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  forsn(i, 2, n + 1) { cout << par[i] << endl; }
  return 0;
}