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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define itc                                                                    \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define otc                                                                    \\
  ll t = 1;                                                                    \\
  while (t--)
#define ll long long int
#define ld long double
#define pb push_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
#define ull unsigned long long int
#define memset(arr) memset(arr, 0, sizeof(arr));
bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi > b.fi);
} // sort by first in descending order
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.se < b.se);
} // sort by second in ascending order
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
#define PI 3.141592653589793238
vector<int> adj[200005];
bool vist[200005];
int parent[200005];

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  queue<pair<int, int>> q;
  q.push({1, -1});
  vist[1] = 1;
  while (!q.empty()) {
    int node = q.front().fi;
    int p = q.front().se;

    if (parent[node] == 0)
      parent[node] = p;
    q.pop();

    for (auto child : adj[node])
      if (vist[child] == 0 && child != p) {
        vist[child] = 1;
        q.push({child, node});
      }
  }

  bool f = 0;
  for (int i = 1; i <= n; i++) {
    if (vist[i] != 1) {
      f = 1;
      break;
    }
  }
  if (f)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << parent[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define itc                                                                    \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define otc                                                                    \\
  ll t = 1;                                                                    \\
  while (t--)
#define ll long long int
#define ld long double
#define pb push_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
#define ull unsigned long long int
#define memset(arr) memset(arr, 0, sizeof(arr));
bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi > b.fi);
} // sort by first in descending order
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.se < b.se);
} // sort by second in ascending order
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
#define PI 3.141592653589793238
vector<int> adj[200005];
bool vist[200005];
int parent[200005];

int main() {
  fio otc {
    int n, m;
    cin >> n >> m;
    while (m--) {
      int u, v;
      cin >> u >> v;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    queue<pair<int, int>> q;
    q.push({1, -1});
    while (!q.empty()) {
      int node = q.front().fi;
      int p = q.front().se;

      vist[node] = 1;
      if (parent[node] == 0)
        parent[node] = p;
      q.pop();

      for (auto child : adj[node])
        if (vist[child] == 0 && child != p) {
          q.push({child, node});
        }
    }

    bool f = 0;
    for (int i = 1; i <= n; i++) {
      if (vist[i] != 1) {
        f = 1;
        break;
      }
    }
    if (f)
      cout << "No" << endl;
    else {
      cout << "Yes" << endl;
      for (int i = 2; i <= n; i++)
        cout << parent[i] << endl;
    }
  }

  return 0;
}