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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;

#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define PER(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define PERS(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).end(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n) setfill('0') << right << setw(n)
#define mp(a, b) make_pair(a, b)
#define toLower(c) c + 0x20
#define toUpper(c) c - 0x20
#define pb push_back
#define eb emplace_back

const int INF = 1e9;
const long long LLINF = 1e18;
const ll MOD = 1000000007;
const int MSIZE = 100007;
const double Pi = 3.14159265358979323146;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vi> route(n + 1);
  vector<int> ans(n + 1, -1);
  ans[0] = 0;
  ans[1] = 0;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    route[a].emplace_back(b);
    route[b].emplace_back(a);
  }

  {
    queue<int> floor;
    floor.push(1);
    while (!floor.empty()) {
      int room = floor.front();
      floor.pop();
      // cout << room << "is pop" << endl;
      REP(i, route[room].size()) {
        if (ans[route[room][i]] == -1) {
          // cout << route[room][i] << "is push" << endl;
          ans[route[room][i]] = room;
          floor.push(route[room][i]);
        }
      }
    }
  }
  if (find(ALL(ans), -1) != ans.end()) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;

#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define PER(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define PERS(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).end(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n) setfill('0') << right << setw(n)
#define mp(a, b) make_pair(a, b)
#define toLower(c) c + 0x20
#define toUpper(c) c - 0x20
#define pb push_back
#define eb emplace_back

const int INF = 1e9;
const long long LLINF = 1e18;
const ll MOD = 1000000007;
const int MSIZE = 100007;
const double Pi = 3.14159265358979323146;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vec(n);
  vector<int> dep(n, INF), sign(n, -1);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[b].push_back(a);
    vec[a].push_back(b);
  }
  dep[0] = 0;
  sign[0] = 0;
  REP(cnt, n) {
    REP(i, n) {
      REP(j, vec[i].size()) {
        if (dep[vec[i][j]] > dep[i] + 1) {
          dep[vec[i][j]] = dep[i] + 1;
          sign[vec[i][j]] = i + 1;
        }
      }
    }
  }

  for (int i = 1; i < n; i++) {
    if (sign[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << sign[i] << endl;
  }
  return 0;
}