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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<vector<int>>;
using VVV = vector<vector<vector<int>>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VVVL = vector<vector<vector<ll>>>;
template <class T> using pq = priority_queue<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
void bfs(int x, VV &tree, vector<bool> &visited, V &ans) {
  queue<int> que;
  que.push(x);
  visited[x] = true;
  while (!que.empty()) {
    int X = que.front();
    que.pop();
    visited[X] = true;
    for (auto c : tree[X]) {
      if (!visited[c]) {
        que.push(c);
        visited[c] = true;
        if (ans[c] == -1)
          ans[c] = X + 1;
      }
    }
  }
}
int main() { // 問題文はしっかり読め!!!
  int n, m;
  cin >> n >> m;
  VV G(n);
  set<P<int>> st;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    int prev = st.size();
    if (b < a)
      swap(a, b);
    st.insert(make_pair(a, b));
    if (prev != st.size()) {
      G[a].push_back(b);
      G[b].push_back(a);
    }
  }
  vector<bool> visited(n, false);
  V ans(n, -1);
  bfs(0, G, visited, ans);
  cout << "Yes" << endl;
  rep1(i, n - 1) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<vector<int>>;
using VVV = vector<vector<vector<int>>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VVVL = vector<vector<vector<ll>>>;
template <class T> using pq = priority_queue<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
void bfs(int x, VV &tree, vector<bool> &visited, V &ans) {
  queue<int> que;
  que.push(x);
  visited[x] = true;
  while (!que.empty()) {
    int X = que.front();
    que.pop();
    visited[X] = true;
    for (auto c : tree[X]) {
      if (!visited[c]) {
        que.push(c);
        if (ans[c] == -1)
          ans[c] = X + 1;
      }
    }
  }
}
int main() { // 問題文はしっかり読め!!!
  int n, m;
  cin >> n >> m;
  VV G(n);
  set<P<int>> st;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    int prev = st.size();
    if (b > a)
      swap(a, b);
    st.insert(make_pair(a, b));
    if (prev != st.size()) {
      G[a].push_back(b);
      G[b].push_back(a);
    }
  }
  vector<bool> visited(n, false);
  V ans(n, -1);
  bfs(0, G, visited, ans);
  cout << "Yes" << endl;
  rep1(i, n - 1) { cout << ans[i] << endl; }
  return 0;
}