Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define pbds                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define fastio()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

ll fast(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}

ll inv(ll n) { return fast(n, mod - 2); }

void pre() {}

ll p[maxs];
ll r[maxs];

ll fn(ll x) {
  if (p[x] == x) {
    return x;
  }
  return p[x] = fn(p[x]);
}

bool add(ll x, ll y) {
  x = fn(x);
  y = fn(y);
  if (x == y)
    return false;
  if (r[x] < r[y])
    swap(x, y);
  p[y] = x;
  if (r[x] == r[y])
    r[x]++;
  return true;
}

vector<ll> g[maxs];

void solve() {

  ll n, m;
  cin >> n >> m;

  for (ll i = 1; i < n + 1; i++) {
    p[i] = i;
  }

  while (m--) {
    ll s, d;
    cin >> s >> d;
    add(s, d);
    g[s].pb(d);
    g[d].pb(s);
  }

  set<ll> SET;
  for (ll i = 1; i < n + 1; i++) {
    SET.I(fn(i));
  }

  if (SET.size() > 1) {
    cout << "No";
    return;
  }

  vll ans(n + 1, -1);
  vll vis(n + 1, 0);
  queue<ll> q;
  q.push(1);

  while (q.size()) {
    ll s = q.front();
    q.pop();
    vis[s] = 1;
    for (auto d : g[s]) {
      if (!vis[d]) {
        if (ans[d] == -1)
          ans[d] = s;
        q.push(d);
      }
    }
  }

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

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input00.txt","r",stdin);
  // #endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  ll tt = 1;
  while (T--) {
    // cout << "Case #" << tt << ": ";
    tt++;
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define pbds                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define fastio()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

ll fast(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}

ll inv(ll n) { return fast(n, mod - 2); }

void pre() {}

ll p[maxs];
ll r[maxs];

ll fn(ll x) {
  if (p[x] == x) {
    return x;
  }
  return p[x] = fn(p[x]);
}

bool add(ll x, ll y) {
  x = fn(x);
  y = fn(y);
  if (x == y)
    return false;
  if (r[x] < r[y])
    swap(x, y);
  p[y] = x;
  if (r[x] == r[y])
    r[x]++;
  return true;
}

vector<ll> g[maxs];

void solve() {

  ll n, m;
  cin >> n >> m;

  for (ll i = 1; i < n + 1; i++) {
    p[i] = i;
  }

  while (m--) {
    ll s, d;
    cin >> s >> d;
    add(s, d);
    g[s].pb(d);
    g[d].pb(s);
  }

  set<ll> SET;
  for (ll i = 1; i < n + 1; i++) {
    SET.I(fn(i));
  }

  if (SET.size() > 1) {
    cout << "No";
    return;
  }

  vll ans(n + 1, -1);
  vll vis(n + 1, 0);
  queue<ll> q;
  q.push(1);

  while (q.size()) {
    ll s = q.front();
    q.pop();
    vis[s] = 1;
    for (auto d : g[s]) {
      if (!vis[d]) {
        if (ans[d] == -1)
          ans[d] = s;
        vis[d] = true;
        q.push(d);
      }
    }
  }

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

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input00.txt","r",stdin);
  // #endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  ll tt = 1;
  while (T--) {
    // cout << "Case #" << tt << ": ";
    tt++;
    solve();
  }
  return 0;
}