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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define endl "\\n"
#define F first
#define S second
#define EN cout << endl;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll dp[2000][2000];

vector<pair<int, int>> v[101010];
P ans[101010] = {};

void d(int x, int y, int last) {

  if (ans[x].S > y) {
    ans[x] = P(last, y);
  }
  rep(i, v[x].size()) {
    if (v[x][i].S) {
      v[x][i].S = 1;
      d(v[x][i].F, y + 1, x);
    }
  }
}

int main() {
  int n, a = 0, b, h, m;
  string s;
  bool f = 1;
  cin >> n >> m;
  REP(i, 1, n) { ans[i] = P(0, 1010101); }

  rep(i, m) {
    cin >> a >> b;
    v[a - 1].pb(P(b - 1, 0));
    v[b - 1].pb(P(a - 1, 0));
  }
  // d(0,0,0);
  vector<int> o;
  ll w = 0;
  o.pb(0);
  ll cnt = 1;
  bool f2[101010] = {};
  while (o.size() - w > 0) {
    ll w2 = w;
    ll o2 = o.size();
    rep(j, o2 - w) {
      rep(i, v[o[w + j]].size()) {
        // cout<<"fa"<<endl;
        if (ans[v[o[w + j]][i].F].S > cnt)
          ans[v[o[w + j]][i].F] = P(o[w + j], cnt);

        if (f2[v[o[w + j]][i].F] == 0) {
          f2[v[o[w + j]][i].F] = 1;
          o.pb(v[o[w + j]][i].F);
        }
      }
      w2++;
    }

    cnt++;
    w = w2;
  }
  // cout<<w<<endl;
  // cout<<v[0][0].F<<endl;

  REP(i, 1, n) {
    if (ans[i].S == 1010101) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  REP(i, 1, n) { cout << ans[i].F + 1 << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define endl "\\n"
#define F first
#define S second
#define EN cout << endl;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll dp[2000][2000];

vector<pair<int, int>> v[101010];
P ans[101010] = {};

void d(int x, int y, int last) {

  if (ans[x].S > y) {
    ans[x] = P(last, y);
  }
  rep(i, v[x].size()) {
    if (v[x][i].S) {
      v[x][i].S = 1;
      d(v[x][i].F, y + 1, x);
    }
  }
}

int main() {
  int n, a = 0, b, h, m;
  string s;
  bool f = 1;
  cin >> n >> m;
  REP(i, 1, n) { ans[i] = P(0, 1010101); }

  rep(i, m) {
    cin >> a >> b;
    v[a - 1].pb(P(b - 1, 0));
    v[b - 1].pb(P(a - 1, 0));
  }
  // d(0,0,0);
  vector<int> o;
  ll w = 0;
  o.pb(0);
  ll cnt = 1;
  while (o.size() - w > 0) {
    ll w2 = w;
    ll o2 = o.size();
    rep(j, o2 - w) {
      rep(i, v[o[w + j]].size()) {
        // cout<<"fa"<<endl;
        if (ans[v[o[w + j]][i].F].S > cnt)
          ans[v[o[w + j]][i].F] = P(o[w + j], cnt);

        if (v[o[w + j]][i].S == 0) {
          v[o[w + j]][i].S = 1;
          o.pb(v[o[w + j]][i].F);
        }
      }
      w2++;
    }

    cnt++;
    w = w2;
  }
  // cout<<w<<endl;
  // cout<<v[0][0].F<<endl;

  REP(i, 1, n) {
    if (ans[i].S == 1010101) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  REP(i, 1, n) { cout << ans[i].F + 1 << endl; }
}