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

/**
 *    author:	raghuvanshraj
 *    created:	17.05.2020 05:52:32 PM
 **/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define MOD 1000000007
#define MAX 1000000000
#define MIN -1000000000

#define SET_ARR(arr, n, val)                                                   \\
  for (int i = 0; i < n; ++i)                                                  \\
  arr[i] = val
#define SET_ARR2D(arr, n, m, val)                                              \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
  arr[i][j] = val
#define INPUT_ARR(arr, n)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cin >> arr[i];
#define INPUT_ARR2D(arr, n, m)                                                 \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> arr[i][j];
#define PRINT_ARR(arr)                                                         \\
  for (auto x : arr)                                                           \\
    cout << x << ' ';                                                          \\
  cout << endl
#define PRINT_ARR2D(arr)                                                       \\
  for (auto x : arr) {                                                         \\
    for (auto y : x)                                                           \\
      cout << y << ' ';                                                        \\
    cout << endl;                                                              \\
  }

#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

template <typename X, typename T> auto vectors(X x, T a) {
  return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> using max_heap = priority_queue<T>;

template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type>
pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) {
  return {a.first + b.first, a.second + b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type>
pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) {
  return {a.first - b.first, a.second - b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type,
    typename V, typename = typename enable_if<is_arithmetic<V>::value, V>::type>
pair<T, U> operator*(const V &a, const pair<T, U> &b) {
  return {a * b.first, a * b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type,
    typename V, typename = typename enable_if<is_arithmetic<V>::value, V>::type>
pair<T, U> operator*(const pair<T, U> &b, const V &a) {
  return {a * b.first, a * b.second};
}

template <typename T, typename U>
istream &operator>>(istream &input, pair<T, U> &b) {
  input >> b.first >> b.second;
  return input;
}

template <typename T, typename U>
ostream &operator<<(ostream &output, pair<T, U> &b) {
  output << b.first << ' ' << b.second;
  return output;
}

vector<int> ans;

void bfs(vector<vector<int>> &adj_list, int u, vector<bool> &visited) {
  queue<pair<int, int>> qu;
  qu.push({u, -1});
  visited[u] = true;
  while (not qu.empty()) {
    pair<int, int> curr = qu.front();
    qu.pop();
    int v = curr.first, parent = curr.second;
    // if (ans[v] == -1) {
    ans[v] = parent;
    // }

    for (int x : adj_list[v]) {
      if (not visited[x]) {
        visited[x] = true;
        qu.push({x, v});
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  ans = vector<int>(n, -1);
  vector<vector<int>> adj_list(n);
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }

  vector<bool> visited(n);
  bfs(adj_list, 0, visited);

  // PRINT_ARR(visited);

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

  cout << "Yes\\n";
  for (int i = 1; i <= n - 1; ++i) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:	raghuvanshraj
 *    created:	17.05.2020 05:52:32 PM
 **/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define MOD 1000000007
#define MAX 1000000000
#define MIN -1000000000

#define SET_ARR(arr, n, val)                                                   \\
  for (int i = 0; i < n; ++i)                                                  \\
  arr[i] = val
#define SET_ARR2D(arr, n, m, val)                                              \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
  arr[i][j] = val
#define INPUT_ARR(arr, n)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cin >> arr[i];
#define INPUT_ARR2D(arr, n, m)                                                 \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> arr[i][j];
#define PRINT_ARR(arr)                                                         \\
  for (auto x : arr)                                                           \\
    cout << x << ' ';                                                          \\
  cout << endl
#define PRINT_ARR2D(arr)                                                       \\
  for (auto x : arr) {                                                         \\
    for (auto y : x)                                                           \\
      cout << y << ' ';                                                        \\
    cout << endl;                                                              \\
  }

#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

template <typename X, typename T> auto vectors(X x, T a) {
  return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> using max_heap = priority_queue<T>;

template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type>
pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) {
  return {a.first + b.first, a.second + b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type>
pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) {
  return {a.first - b.first, a.second - b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type,
    typename V, typename = typename enable_if<is_arithmetic<V>::value, V>::type>
pair<T, U> operator*(const V &a, const pair<T, U> &b) {
  return {a * b.first, a * b.second};
}

template <
    typename T, typename = typename enable_if<is_arithmetic<T>::value, T>::type,
    typename U, typename = typename enable_if<is_arithmetic<U>::value, U>::type,
    typename V, typename = typename enable_if<is_arithmetic<V>::value, V>::type>
pair<T, U> operator*(const pair<T, U> &b, const V &a) {
  return {a * b.first, a * b.second};
}

template <typename T, typename U>
istream &operator>>(istream &input, pair<T, U> &b) {
  input >> b.first >> b.second;
  return input;
}

template <typename T, typename U>
ostream &operator<<(ostream &output, pair<T, U> &b) {
  output << b.first << ' ' << b.second;
  return output;
}

vector<int> ans;

void bfs(vector<vector<int>> &adj_list, int u, vector<bool> &visited) {
  queue<pair<int, int>> qu;
  qu.push({u, -1});
  while (not qu.empty()) {
    pair<int, int> curr = qu.front();
    qu.pop();
    int v = curr.first, parent = curr.second;
    visited[v] = true;
    if (parent != -1 and ans[v] == -1) {
      ans[v] = parent;
    }

    for (int x : adj_list[v]) {
      if (not visited[x]) {
        qu.push({x, v});
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  ans = vector<int>(n, -1);
  vector<vector<int>> adj_list(n);
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }

  vector<bool> visited(n);
  bfs(adj_list, 0, visited);

  // PRINT_ARR(visited);

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

  cout << "Yes\\n";
  for (int i = 1; i <= n - 1; ++i) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}