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

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

#define INFTY INT32_MAX

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a, b;
  vector<int> sign(n, INT32_MAX);
  vector<int> sirube(n, INFTY);
  vector<vector<int>> load;
  vector<int> state(n, 0);
  queue<int> search;
  sign[0] = 0;

  rep(i, n) {
    vector<int> tmp, tmp2;
    load.push_back(tmp2);
    // state.push_back(tmp);
  }

  vector<int> check(n, 0);

  rep(i, m) {
    int A, B;
    cin >> A >> B;
    a.push_back(A);
    b.push_back(B);
    load[A - 1].push_back(B - 1);
    load[B - 1].push_back(A - 1);
    if (A == 1) {
      // load[A-1][B-1] = 2;
      // load[B-1][A-1] = 2;
      sign[B - 1] = 1;
      sirube[B - 1] = 1;
      check[B - 1] = 1;
      search.push(B - 1);
    }
    if (B == 1) {
      // load[A-1][B-1] = 2;
      // load[B-1][A-1] = 2;
      sign[A - 1] = 1;
      sirube[A - 1] = 1;
      check[A - 1] = 1;
      search.push(A - 1);
    }
  }

  while (!search.empty()) {
    int p = search.front();
    search.pop();
    rep(i, load[p].size()) {
      int c = load[p][i];
      if (check[c])
        continue;
      sign[c] = p + 1;
      check[c] = 1;
      search.push(c);
    }
    // rep2(i, 1, n){
    //     if(check[i]==0&&load[p][i]==1){
    //         //sirube[i] = sirube[p] + 1;
    //         check[i] = 1;
    //         sign[i] = p+1;
    //         load[p][i] = 2;
    //         load[i][p] = 2;
    //         search.push(i);
    //     }
    // }
  }

  // int minv;
  // state[0] = 1;
  // sirube[0] = 0;
  // while(true){
  //     minv = INFTY;
  //     int u = -1;
  //     rep(i, n){
  //         if(minv > sirube[i] && state[i] != 2){
  //             u = i;
  //             minv = sirube[i];
  //         }
  //     }
  //     if(u==-1) break;
  //     state[u] = 2;
  //     rep(v, n){
  //         if(state[v]!=2&&load[u][v]!=INFTY){
  //             if(sirube[v] > sirube[u] + 1){
  //                 sirube[v] = sirube[u] + 1;
  //                 state[v] = 1;
  //                 sign[v] = u+1;
  //             }
  //         }
  //     }
  // }

  // rep(i, m){
  //     if(sign[a[i]-1]!=0&&sign[a[i]-1]!=INT32_MAX){
  //         int tmp;
  //         tmp = min(sign[b[i]-1], sign[a[i]-1]+1);
  //         if(tmp!=sign[b[i]-1]){
  //             sign[b[i]-1] = tmp;
  //             sirube[b[i]-1] = a[i];
  //         }
  //     }
  //     if(sign[b[i]-1]!=0&&sign[b[i]-1]!=INT32_MAX){
  //         int tmp;
  //         tmp = min(sign[a[i]-1], sign[b[i]-1]+1);
  //         if(tmp!=sign[a[i]-1]){
  //             sign[a[i]-1] = tmp;
  //             sirube[a[i]-1] = b[i];
  //         }
  //     }
  // }

  bool flag = true;
  rep2(i, 1, n) {
    if (sign[i] == INT32_MAX) {
      flag = false;
      cout << "No" << endl;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    rep2(i, 1, n) { cout << sign[i] << endl; }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

#define INFTY INT32_MAX

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a, b;
  vector<int> sign(n, INT32_MAX);
  vector<int> sirube(n, INFTY);
  vector<vector<int>> load;
  vector<int> state(n, 0);
  queue<int> search;
  sign[0] = 0;

  rep(i, n) {
    vector<int> tmp, tmp2;
    rep(j, n) {
      // tmp.push_back(0);
      tmp2.push_back(0);
    }
    load.push_back(tmp2);
    // state.push_back(tmp);
  }

  vector<int> check(n, 0);

  rep(i, m) {
    int A, B;
    cin >> A >> B;
    a.push_back(A);
    b.push_back(B);
    load[A - 1][B - 1] = 1;
    load[B - 1][A - 1] = 1;
    if (A == 1) {
      load[A - 1][B - 1] = 2;
      load[B - 1][A - 1] = 2;
      sign[B - 1] = 1;
      sirube[B - 1] = 1;
      check[B - 1] = 1;
      search.push(B - 1);
    }
    if (B == 1) {
      load[A - 1][B - 1] = 2;
      load[B - 1][A - 1] = 2;
      sign[A - 1] = 1;
      sirube[A - 1] = 1;
      check[A - 1] = 1;
      search.push(A - 1);
    }
  }

  while (!search.empty()) {
    int p = search.front();
    search.pop();
    rep2(i, 1, n) {
      if (check[i] == 0 && load[p][i] == 1) {
        // sirube[i] = sirube[p] + 1;
        check[i] = 1;
        sign[i] = p + 1;
        load[p][i] = 2;
        load[i][p] = 2;
        search.push(i);
      }
    }
  }

  // int minv;
  // state[0] = 1;
  // sirube[0] = 0;
  // while(true){
  //     minv = INFTY;
  //     int u = -1;
  //     rep(i, n){
  //         if(minv > sirube[i] && state[i] != 2){
  //             u = i;
  //             minv = sirube[i];
  //         }
  //     }
  //     if(u==-1) break;
  //     state[u] = 2;
  //     rep(v, n){
  //         if(state[v]!=2&&load[u][v]!=INFTY){
  //             if(sirube[v] > sirube[u] + 1){
  //                 sirube[v] = sirube[u] + 1;
  //                 state[v] = 1;
  //                 sign[v] = u+1;
  //             }
  //         }
  //     }
  // }

  // rep(i, m){
  //     if(sign[a[i]-1]!=0&&sign[a[i]-1]!=INT32_MAX){
  //         int tmp;
  //         tmp = min(sign[b[i]-1], sign[a[i]-1]+1);
  //         if(tmp!=sign[b[i]-1]){
  //             sign[b[i]-1] = tmp;
  //             sirube[b[i]-1] = a[i];
  //         }
  //     }
  //     if(sign[b[i]-1]!=0&&sign[b[i]-1]!=INT32_MAX){
  //         int tmp;
  //         tmp = min(sign[a[i]-1], sign[b[i]-1]+1);
  //         if(tmp!=sign[a[i]-1]){
  //             sign[a[i]-1] = tmp;
  //             sirube[a[i]-1] = b[i];
  //         }
  //     }
  // }

  bool flag = true;
  rep2(i, 1, n) {
    if (sign[i] == INT32_MAX) {
      flag = false;
      cout << "No" << endl;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    rep2(i, 1, n) { cout << sign[i] << endl; }
  }
  return 0;
}