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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int pi = acos(-1.0);
const int MAX_N = 1e5;
vector<int> Graph[MAX_N];
vector<int> ans(MAX_N);
vector<bool> isvisit(MAX_N, false);

signed main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  map<int, int> next;

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.merge(a, b);
    Graph[a].pb(b);
    Graph[b].pb(a);
  }
  REP(i, N) {
    if (!uf.issame(0, i)) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[0] = 0;
  que.push(0); // 0 を橙色頂点にする

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();
    for (int nv : Graph[v]) {
      if (dist[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない
      dist[nv] = dist[v] + 1;
      ans[nv] = v + 1;
      que.push(nv);
    }
  }
  for (int i = 1; i < N; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int pi = acos(-1.0);
const int MAX_N = 1e5;
vector<int> Graph[MAX_N];
vector<int> ans(MAX_N);
vector<int> isvisit(MAX_N, INF);

void dfs(int o, int k, int depth) {
  isvisit[k] = depth;
  ans[k] = o + 1;
  for (auto v : Graph[k]) {
    if (v == o || isvisit[v] <= depth + 1)
      continue;
    dfs(k, v, depth + 1);
  }
}

signed main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  map<int, int> next;

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.merge(a, b);
    Graph[a].pb(b);
    Graph[b].pb(a);
  }
  REP(i, N) {
    if (!uf.issame(0, i)) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  dfs(-1, 0, 0);
  for (int i = 1; i < N; i++)
    cout << ans[i] << endl;
}