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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define print(n) cout << n << endl
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vi vector<int>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
const int M = 200111;
const int inf = 1000000007;
const int MOD = 1000000007;
const double PI = 3.1415926535897932384626433;
const long long INF = 1e18;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int ddx[8] = {1, -1, 0, 0, 1, -1, -1, 1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(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); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

vvl comb(int n) {
  vvl v(n + 1, vl(n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v;
}

void sosu(ll x, bool f[]) {
  ll y = x;
  for (ll i = 2; i <= sqrt(y); i++) {
    if (x % i == 0) {
      x /= i;
      f[i] = true;
      i--;
    }
  }
}

ll __gcd(ll x, ll y) {
  if (x < y)
    swap(x, y);
  while (1) {
    ll tmp = x % y;
    x = y;
    y = tmp;
    if (y == 0)
      break;
  }
  return x;
}

int keta(ll a) {
  int ke = 1;
  while (a /= 10)
    ke++;
  return ke;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

ll zyou(ll x, ll n) {
  ll y = 1;
  rep(i, 0, n) { y *= x; }
  return y;
}

int n, m, a, b;
vi v[2 * M];

int main() {
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vector<int> dist(n, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;
  int ans[M] = {};

  // 初期条件 (頂点 0 を初期ノードとする)
  que.push(1);

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int x = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : v[x]) {
      if (ans[nv] != 0)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      ans[nv] = x;
      que.push(nv);
    }
  }
  bool f = true;

  rep(i, 2, n + 1) if (ans[i] == 0) f = false;
  if (!f) {
    print("No");
    rep(i, 2, n + 1) print(ans[i]);
    return 0;
  } else {
    print("Yes");
    rep(i, 2, n + 1) print(ans[i]);
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define print(n) cout << n << endl
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vi vector<int>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
const int M = 200111;
const int inf = 1000000007;
const int MOD = 1000000007;
const double PI = 3.1415926535897932384626433;
const long long INF = 1e18;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int ddx[8] = {1, -1, 0, 0, 1, -1, -1, 1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(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); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

vvl comb(int n) {
  vvl v(n + 1, vl(n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v;
}

void sosu(ll x, bool f[]) {
  ll y = x;
  for (ll i = 2; i <= sqrt(y); i++) {
    if (x % i == 0) {
      x /= i;
      f[i] = true;
      i--;
    }
  }
}

ll __gcd(ll x, ll y) {
  if (x < y)
    swap(x, y);
  while (1) {
    ll tmp = x % y;
    x = y;
    y = tmp;
    if (y == 0)
      break;
  }
  return x;
}

int keta(ll a) {
  int ke = 1;
  while (a /= 10)
    ke++;
  return ke;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

ll zyou(ll x, ll n) {
  ll y = 1;
  rep(i, 0, n) { y *= x; }
  return y;
}

int n, m, a, b;
vi v[2 * M];
pair<int, int> dp[M] = {};
void solve(int x) {
  for (auto y : v[x]) {
    if (dp[y].first == 0 && y != 1) {
      dp[y].first = x;
      dp[y].second = dp[x].second + 1;
      solve(y);
    } else if (dp[y].second > dp[x].second + 1 && y != 1) {
      dp[y].first = x;
      dp[y].second = dp[x].second + 1;
      solve(y);
    }
  }
  return;
}

int main() {
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  solve(1);
  bool f = true;
  rep(i, 2, n + 1) if (dp[i].first == 0) f = false;
  if (!f) {
    print("No");
    //		rep(i,2,n+1)print(dp[i]);
    return 0;
  } else {
    print("Yes");
    rep(i, 2, n + 1) print(dp[i].first);
  }
}