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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define cases                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define scan(n) scanf("%d", &n)
#define mod 1000000007
#define maxn 200001
#define PI 3.1415926535897932384626433832795
#define int ll
#define loop0 for (int i = 0; i < n; i++)
#define loop1 for (int i = 1; i <= n; i++)
#define inpar0(ar)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> ar[i];
#define inpar1(ar)                                                             \\
  for (int i = 1; i <= n; i++)                                                 \\
    cin >> ar[i];
#define showar0(ar)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << ar[i] << " ";
#define showar1(ar)                                                            \\
  for (int i = 1; i <= n; i++)                                                 \\
    cout << ar[i] << " ";
#define manu cout << (checkerCounter++) << endl;
static int checkerCounter = 1;
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2) {
      res = (res * a) % mod;
      b--;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res % mod;
}
bool isPrime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int ans[maxn];
bool vis[maxn];
void bfs(int ch, vector<int> adj[]) {
  queue<int> q;
  q.push(ch);
  while (!q.empty()) {
    int x = q.front();
    vis[x] = true;
    q.pop();
    for (int i = 0; i < adj[x].size(); i++) {
      if (vis[adj[x][i]] == false) {
        vis[adj[x][i]] = true;
        q.push(adj[x][i]);
        ans[adj[x][i]] = x;
      }
    }
  }
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  bfs(1, adj);
  bool ch = true;
  for (int i = 1; i <= n; i++)
    ch &= vis[i];
  if (!ch) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define cases                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define scan(n) scanf("%d", &n)
#define mod 1000000007
#define maxn 200001
#define PI 3.1415926535897932384626433832795
#define int ll
#define loop0 for (int i = 0; i < n; i++)
#define loop1 for (int i = 1; i <= n; i++)
#define inpar0(ar)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> ar[i];
#define inpar1(ar)                                                             \\
  for (int i = 1; i <= n; i++)                                                 \\
    cin >> ar[i];
#define showar0(ar)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << ar[i] << " ";
#define showar1(ar)                                                            \\
  for (int i = 1; i <= n; i++)                                                 \\
    cout << ar[i] << " ";
#define manu cout << (checkerCounter++) << endl;
static int checkerCounter = 1;
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2) {
      res = (res * a) % mod;
      b--;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res % mod;
}
bool isPrime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int ans[maxn];
bool vis[maxn];
void bfs(int ch, vector<int> adj[]) {
  queue<int> q;
  q.push(ch);
  while (!q.empty()) {
    int x = q.front();
    vis[x] = true;
    q.pop();
    for (int i = 0; i < adj[x].size(); i++) {
      if (vis[adj[x][i]] == false) {
        q.push(adj[x][i]);
        if (ans[adj[x][i]] == 0)
          ans[adj[x][i]] = x;
      }
    }
  }
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  bfs(1, adj);
  bool ch = true;
  for (int i = 1; i <= n; i++)
    ch &= vis[i];
  if (!ch) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << ans[i] << endl;
  }
}