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

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef int32_t i32;

#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front

#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define RFOR(i, a, b) for (ll i = a; i >= b; i--)
#define FILL(a, b, c)                                                          \\
  for (ll i = 0; i < c; i++)                                                   \\
    a[i] = b;

#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()

#define endl "\\n"
#define setpri(x) setprecision(x)
#define vp(x) cout << #x << " : " << (x) << " | "
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define forf(i, a, b) for (int i = a; i < b; i++)

#define INF 1e17 + 4
#define MINF
#define MAX_INT 100000
#define MOD 1000000007
ll mod(ll n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
vector<int> adj[1000002];
int vis[1000002], dep[1000002], siz[1000002];

// void dfs(ll cur,ll par)
// {
//     vis[cur]=1;siz[cur]=1;
//     for(auto x:adj[cur])
//     {
//         if(vis[x]!=1)
//         {
//             dep[x]=dep[cur]+1;
//             dfs(x,cur);
//             siz[cur]+=siz[x];
//         }
//     }
// }
void dfs(ll cur) {
  queue<int> q;
  q.push(1);
  vis[1] = 0;
  dep[1] = 0;
  int p = 0;
  while (!q.empty()) {
    int tp = q.front();
    q.pop();
    if (vis[tp] == 1) {
      continue;
    }
    int r = dep[tp];
    vis[tp] = 1;
    for (auto x : adj[tp]) {
      if (vis[x] != 1) {
        if (dep[x] == 0)
          dep[x] = tp;
        q.push(x);
      }
    }
  }
}

// bool sortfunc(const pair<int,int> &a, const pair<int,int> &b)
// {
//     return ;

void solve() {
  int n, m;
  cin >> n >> m;
  forf(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1);
  forf(i, 1, n + 1) if (vis[i] == 0) {
    no;
    return;
  }
  yes;
  forf(i, 2, n + 1) cout << dep[i] << endl;
}

signed main() {
  FAST;

  // ll t;cin>>t;while(t--)
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef int32_t i32;

#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front

#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define RFOR(i, a, b) for (ll i = a; i >= b; i--)
#define FILL(a, b, c)                                                          \\
  for (ll i = 0; i < c; i++)                                                   \\
    a[i] = b;

#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()

#define endl "\\n"
#define setpri(x) setprecision(x)
#define vp(x) cout << #x << " : " << (x) << " | "
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define forf(i, a, b) for (int i = a; i < b; i++)

#define INF 1e17 + 4
#define MINF
#define MAX_INT 100000
#define MOD 1000000007
ll mod(ll n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
vector<int> adj[1000002];
int vis[1000002], dep[1000002], siz[1000002];

// void dfs(ll cur,ll par)
// {
//     vis[cur]=1;siz[cur]=1;
//     for(auto x:adj[cur])
//     {
//         if(vis[x]!=1)
//         {
//             dep[x]=dep[cur]+1;
//             dfs(x,cur);
//             siz[cur]+=siz[x];
//         }
//     }
// }
void dfs(ll cur) {
  queue<int> q;
  q.push(1);
  vis[1] = 1;
  dep[1] = 0;
  int p = 0;
  while (!q.empty()) {
    int tp = q.front();
    q.pop();
    int r = dep[tp];
    vis[tp] = 1;
    for (auto x : adj[tp]) {
      if (vis[x] != 1) {
        if (dep[x] == 0)
          dep[x] = tp;
        q.push(x);
      }
    }
  }
}

// bool sortfunc(const pair<int,int> &a, const pair<int,int> &b)
// {
//     return ;

void solve() {
  int n, m;
  cin >> n >> m;
  forf(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1);
  forf(i, 1, n + 1) if (vis[i] == 0) {
    no;
    return;
  }
  yes;
  forf(i, 2, n + 1) cout << dep[i] << endl;
}

signed main() {
  FAST;

  // ll t;cin>>t;while(t--)
  solve();

  return 0;
}