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

// Code by Anshuman Das Gupta

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define loop(I, N) for (int i = I; i < N; i++)
#define sortvec(V) sort(V.begin(), V.end())
#define vec vector<int>
#define endl "\\n"
using namespace std;
int fast_power(int base, int power) {
  int result = 1;
  while (power > 0) {

    if (power & 1 == 1) {
      result = (result * base) % MOD;
    }
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result % MOD;
}

bool isPrime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  if (x == 1)
    return false;
  return true;
}

int binsearch(int a[], int f, int r, int x) {
  while (f <= r) {
    int mid = (f + r) / 2;
    if (a[mid] == x)
      return mid;
    else if (a[mid] > x) {
      r = mid - 1;
    } else {
      f = mid + 1;
    }
  }
  return -1;
}
void sieve(int a[], int n) {
  a[0] = 0;
  a[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] == 1) {
      for (int j = i + i; j <= n; j += i) {
        a[j] = 0;
      }
    }
  }
}
int vis[100005];
int par[100005];
vector<vector<int>> v(100005);
void bfs(vector<int> root) {
  vector<int> child;
  for (auto it : root) {
    vis[it]++;
    for (int i = 0; i < v[it].size(); i++) {
      if (vis[v[it][i]] == 0) {
        par[v[it][i]] = it;
        vis[v[it][i]]++;
        child.pb(v[it][i]);
        // cout << v[it][i] << " ";
      }
    }
  }
  // cout << endl;
  if (child.size() == 0)
    return;
  else {
    bfs(child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("out.txt", "w", stdout);
#endif
  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    loop(0, m) {
      int t1, t2;
      cin >> t1 >> t2;
      v[t1].pb(t2);
      v[t2].pb(t1);
    }
    vector<int> root;
    root.pb(1);
    int flag = 0;
    bfs(root);

    loop(2, n + 1) {
      if (vis[i] == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 1) {
      cout << "No\\n";
    } else {
      cout << "Yes\\n";
      loop(2, n + 1) { cout << par[i] << endl; }
      // cout << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

// Code by Anshuman Das Gupta

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define loop(I, N) for (int i = I; i < N; i++)
#define sortvec(V) sort(V.begin(), V.end())
#define vec vector<int>
#define endl "\\n"
using namespace std;
int fast_power(int base, int power) {
  int result = 1;
  while (power > 0) {

    if (power & 1 == 1) {
      result = (result * base) % MOD;
    }
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result % MOD;
}

bool isPrime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  if (x == 1)
    return false;
  return true;
}

int binsearch(int a[], int f, int r, int x) {
  while (f <= r) {
    int mid = (f + r) / 2;
    if (a[mid] == x)
      return mid;
    else if (a[mid] > x) {
      r = mid - 1;
    } else {
      f = mid + 1;
    }
  }
  return -1;
}
void sieve(int a[], int n) {
  a[0] = 0;
  a[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] == 1) {
      for (int j = i + i; j <= n; j += i) {
        a[j] = 0;
      }
    }
  }
}
int vis[100005];
int par[100005];
void bfs(vector<vector<int>> v, vector<int> root) {
  vector<int> child;
  for (auto it : root) {
    vis[it]++;
    for (int i = 0; i < v[it].size(); i++) {
      if (vis[v[it][i]] == 0) {
        par[v[it][i]] = it;
        vis[v[it][i]]++;
        child.pb(v[it][i]);
        // cout << v[it][i] << " ";
      }
    }
  }
  // cout << endl;
  if (child.size() == 0)
    return;
  else {
    bfs(v, child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("out.txt", "w", stdout);
#endif
  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> v(n + 1);
    loop(0, m) {
      int t1, t2;
      cin >> t1 >> t2;
      v[t1].pb(t2);
      v[t2].pb(t1);
    }
    vector<int> root;
    root.pb(1);
    int flag = 0;
    bfs(v, root);
    loop(2, n + 1) {
      if (vis[i] == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 1) {
      cout << "No\\n";
    } else {
      cout << "Yes\\n";
      loop(2, n + 1) { cout << par[i] << endl; }
      // cout << endl;
    }
  }
  return 0;
}