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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

const long long MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> path(M, vector<int>(2));
  vector<vector<int>> connect(N);

  REP(m, M) {
    int A, B;
    cin >> A >> B;
    A -= 1;
    B -= 1;
    connect[A].emplace_back(B);
    connect[B].emplace_back(A);
  }

  vector<int> length(N);
  vector<int> next(N);
  REP(n, N) {
    length[n] = 100000000;
    next[n] = -1;
  }

  length[0] = 0;
  deque<vector<int>> deq;
  REP(s1, connect[0].size()) {
    vector<int> vec(2);
    vec[0] = 0;
    vec[1] = connect[0][s1];
    deq.emplace_back(vec);
  }

  while (!deq.empty()) {
    vector<int> vec1(2);
    vec1 = deq[0];
    deq.pop_front();
    int parent, current;
    parent = vec1[0];
    current = vec1[1];
    if (length[current] > length[parent] + 1) {
      length[current] = length[parent] + 1;
      next[current] = parent + 1;
      REP(s2, connect[current].size()) {
        int child;
        child = connect[current][s2];
        if (child != parent) {
          vector<int> vec2(2);
          vec2[0] = current;
          vec2[1] = child;
          deq.emplace_back(vec2);
        }
      }
    }
  }
  bool flag = true;
  REP(n, N - 1) {
    if (next[n + 1] == -1)
      flag = false;
  }
  if (flag == true) {
    cout << "Yes" << endl;
    REP(n, N - 1) cout << next[n + 1] << endl;
  } else {
    cout << "No" << endl;
  }
}


Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

const long long MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> path(M, vector<int>(2));
  vector<vector<int>> connect(N);

  REP(m, M) {
    int A, B;
    cin >> A >> B;
    A -= 1;
    B -= 1;
    connect[A].emplace_back(B);
    connect[B].emplace_back(A);
  }

  vector<int> length(N);
  vector<int> next(N);
  REP(n, N) {
    length[n] = 100000000;
    next[n] = -1;
  }

  length[0] = 0;
  deque<vector<int>> deq;
  REP(s1, connect[0].size()) {
    vector<int> vec(2);
    vec[0] = 0;
    vec[1] = connect[0][s1];
    deq.emplace_back(vec);
  }

  while (!deq.empty()) {
    vector<int> vec1(2);
    vec1 = deq[0];
    deq.pop_front();
    int parent, current;
    parent = vec1[0];
    current = vec1[1];
    if (length[current] >= length[parent] + 1) {
      length[current] = length[parent] + 1;
      next[current] = parent + 1;
      REP(s2, connect[current].size()) {
        int child;
        child = connect[current][s2];
        if (child != parent) {
          vector<int> vec2(2);
          vec2[0] = current;
          vec2[1] = child;
          deq.emplace_back(vec2);
        }
      }
    }
  }
  bool flag = true;
  REP(n, N - 1) {
    if (next[n + 1] == -1)
      flag = false;
  }
  if (flag == true) {
    cout << "Yes" << endl;
    REP(n, N - 1) cout << next[n + 1] << endl;
  } else {
    cout << "No" << endl;
  }
}