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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \\
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int ans[100001], dist[100001];
int n, m;
vi v[100001];
bool used[100001];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  rep(i, n) dist[i] = 1000000;
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    used[p] = true;
    rep(i, v[p].size()) {
      if (dist[v[p][i]] > dist[p] + 1) {
        dist[v[p][i]] = dist[p] + 1;
        ans[v[p][i]] = p;
        q.push(v[p][i]);
      }
    }
  }
  rep(i, n) {
    if (!used[i]) {
      cout << "No";
      re 0;
    }
  }
  cout << "Yes\\n";
  forn(i, 1, n) cout << ans[i] + 1 << "\\n";
  re 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \\
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int ans[100001], dist[100001];
int n, m;
vi v[100001];
bool used[100001];
void dfs(int x) {
  used[x] = true;
  rep(i, v[x].size()) {
    if (dist[v[x][i]] > dist[x] + 1) {
      dist[v[x][i]] = dist[x] + 1;
      ans[v[x][i]] = x;
      dfs(v[x][i]);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  rep(i, n) dist[i] = 100000;
  dist[0] = 0;
  dfs(0);
  rep(i, n) {
    if (!used[i]) {
      cout << "No";
      re 0;
    }
  }
  cout << "Yes\\n";
  forn(i, 1, n) cout << ans[i] + 1 << "\\n";
  re 0;
}