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

/*
 *  stay hungry, stay foolish, stay home.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ml cout << "\\n"
#define tc(w)                                                                  \\
  ll w;                                                                        \\
  cin >> w;                                                                    \\
  while (w--)

#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define all(a) (a).begin(), (a).end()

#define pb push_back
#define in insert
#define ff first
#define ss second
#define mod 1000000007

#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define test4(x, y, z, a)                                                      \\
  cout << "x is " << x << "		y is " << y << "		z is "              \\
       << z << "		a is " << a << endl;
#define test3(x, y, z)                                                         \\
  cout << "x is " << x << "		y is " << y << "		z is "            \\
       << z << endl;
#define test2(x, y) cout << "x is " << x << "		y is " << y << endl;
#define test1(x) cout << "x is " << x << endl;

vi v[2000000];
ll visited[2000000];
ll ans[2000000];
mi m;
void dfs(ll root = 1, ll pre = -1, ll lev = 1) {
  if (visited[root] <= lev)
    return;
  visited[root] = lev;
  ans[root] = pre;
  for (auto x : v[root]) {
    // if(!visited[x]){
    dfs(x, root, lev + 1);
    //}
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  cin >> n >> k;
  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  rep(i, 0, n + 1) { visited[i] = -1; }
  memset(ans, 0, sizeof(ans));

  // dfs();
  queue<ll> q;
  q.push(1);
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    for (auto x : v[p]) {
      if (visited[x] == -1) {
        visited[x] = p;
        q.push(x);
      }
    }
  }
  yes;
  rep(i, 2, n + 1) { cout << visited[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

/*
 *  stay hungry, stay foolish, stay home.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ml cout << "\\n"
#define tc(w)                                                                  \\
  ll w;                                                                        \\
  cin >> w;                                                                    \\
  while (w--)

#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define all(a) (a).begin(), (a).end()

#define pb push_back
#define in insert
#define ff first
#define ss second
#define mod 1000000007

#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define test4(x, y, z, a)                                                      \\
  cout << "x is " << x << "		y is " << y << "		z is "              \\
       << z << "		a is " << a << endl;
#define test3(x, y, z)                                                         \\
  cout << "x is " << x << "		y is " << y << "		z is "            \\
       << z << endl;
#define test2(x, y) cout << "x is " << x << "		y is " << y << endl;
#define test1(x) cout << "x is " << x << endl;

vi v[2000000];
ll visited[2000000];
ll ans[2000000];
mi m;
void dfs(ll root = 1, ll pre = -1, ll lev = 1) {
  if (visited[root] <= lev)
    return;
  visited[root] = lev;
  ans[root] = pre;
  for (auto x : v[root]) {
    // if(!visited[x]){
    dfs(x, root, lev + 1);
    //}
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  cin >> n >> k;
  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  rep(i, 0, n + 1) { visited[i] = INT_MAX; }
  memset(ans, 0, sizeof(ans));

  dfs();

  //  	vi ans;
  rep(i, 2, n + 1) {
    if (visited[i] == INT_MAX) {
      no;
      return 0;
    }
  }
  yes;
  rep(i, 2, n + 1) { cout << ans[i] << endl; }

  //  	rep(i,2,n+1){
  //  		ll h=LONG_MAX;
  //  		ll j=0;
  //  		for(auto x:v[i]){
  //  			if(visited[x]<h){
  //  				h=visited[x];
  //  				j=x;
  //  			}
  //  		}
  //  		ans.pb(j);
  //  	}
  // yes;
  //  	for(auto x:ans){
  //  		cout<<x;
  //  		ml;
  //  	}
  return 0;
}