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

#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod = 1000000007;

vector<int> G[100100];
int p[100001];
queue<int> q;

int main() {

  int a, b, n, m, room;
  cin >> n >> m;

  FOR(i, 0, m) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  FORQ(i, 0, n) p[i] = 0;

  q.push(1);
  while (!q.empty()) {
    room = q.front();
    q.pop();
    FOR(i, 0, G[room].size()) {
      if (G[room][i] != 1 && p[G[room][i]] == 0) {
        p[G[room][i]] = room;
        q.push(G[room][i]);
      }
    }
  }

  FORQ(i, 2, n) {
    if (p[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  FORQ(i, 2, n) { cout << p[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod = 1000000007;

int p[100001], a[200001], b[200001];
queue<int> q;

int main() {

  int n, m, room;
  cin >> n >> m;

  FOR(i, 0, m) { cin >> a[i] >> b[i]; }
  FORQ(i, 0, n) p[i] = 0;

  q.push(1);
  while (!q.empty()) {
    room = q.front();
    q.pop();
    FOR(i, 0, m) {
      if (a[i] == room && b[i] != 1 && p[b[i]] == 0) {
        p[b[i]] = room;
        q.push(b[i]);
      } else if (b[i] == room && a[i] != 1 && p[a[i]] == 0) {
        p[a[i]] = room;
        q.push(a[i]);
      }
    }
  }

  FORQ(i, 2, n) {
    if (p[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  FORQ(i, 2, n) { cout << p[i] << endl; }

  return 0;
}