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

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
#define fo(i, a, n) for (i = a; i <= n; i++)
#define F first
#define S second
#define pii pair<int, int>
#define sz size()
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define PB pop_back
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<int>
#define mp make_pair
#define mod 1000000007
typedef unsigned long long ull;
const int N = 200005;
const int MOD = 998244353;

vl adj[200010];
int vis[200010], loc[200010], dis[200010];

void bfs(int k) {
  queue<int> q;
  q.push(k);
  vis[k] = 1;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (int i = 0; i < adj[node].size(); i++) {
      int p = adj[node][i];
      if (!vis[p]) {
        dis[p] = dis[node] + 1;
        vis[p] = 1;
        loc[p] = node;
        q.push(p);
      }
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  f(i, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  bfs(1);
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0) {
      cout << "No";
      return;
    }
  cout << "Yes\\n";
  for (int i = 2; i <= n; i++)
    cout << loc[i] << "\\n";
}

int32_t main() {
  fast int t;
  t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
#define fo(i, a, n) for (i = a; i <= n; i++)
#define F first
#define S second
#define pii pair<int, int>
#define sz size()
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define PB pop_back
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<int>
#define mp make_pair
#define mod 1000000007
typedef unsigned long long ull;
const int N = 200005;
const int MOD = 998244353;

vl adj[100010];
vl vis(100010), loc(100010), dis(100010);
void dfs(int v) {
  vis[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    if (vis[adj[v][i]] == 0)
      dfs(adj[v][i]);
  }
  return;
}

void fun(int v, int par) {
  vis[v] = 1;
  if (dis[v] > (dis[par] + 1)) {
    dis[v] = dis[par] + 1;
    loc[v] = par;
  }
  for (int i = 0; i < adj[v].size(); i++) {
    int k = adj[v][i];
    if (dis[k] > (dis[v] + 1))
      fun(k, v);
  }
  return;
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n + 5; i++)
    adj[i].clear(), vis[i] = 0, loc[i] = 0, dis[i] = INT_MAX;
  f(i, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1);
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0) {
      cout << "No";
      return;
    }
  for (int i = 0; i <= n + 5; i++)
    vis[i] = 0;
  dis[0] = -1;
  fun(1, 0);
  cout << "Yes\\n";
  for (int i = 2; i <= n; i++)
    cout << loc[i] << "\\n";
}

int32_t main() {
  fast int t;
  t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}