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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int mod = 998244353;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const { return mint(*this) += a; }

  mint operator-(const mint a) const { return mint(*this) -= a; }

  mint operator*(const mint a) const { return mint(*this) *= a; }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return *this *= a.inv(); }

  mint operator/(const mint a) const { return mint(*this) /= a; }
};

istream &operator>>(istream &is, const mint &a) { return is >> a.x; }

ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

multiset<int> s[202020], t;

vector<int> A[101010];
int M[101010];
bool B[101010];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    A[a].push_back(b);
    A[b].push_back(a);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (int to : A[from]) {
      if (B[to])
        continue;
      q.push(to);
      M[to] = from;
      B[to] = true;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << M[i] + 1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int mod = 998244353;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const { return mint(*this) += a; }

  mint operator-(const mint a) const { return mint(*this) -= a; }

  mint operator*(const mint a) const { return mint(*this) *= a; }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return *this *= a.inv(); }

  mint operator/(const mint a) const { return mint(*this) /= a; }
};

istream &operator>>(istream &is, const mint &a) { return is >> a.x; }

ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

multiset<int> s[202020], t;

vector<int> A[101010];
int C[101010], M[101010];

void dfs(int from, int c) {
  for (int to : A[from]) {
    if (C[to] <= c)
      continue;
    C[to] = c + 1;
    M[to] = from;
    dfs(to, c + 1);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    A[a].push_back(b);
    A[b].push_back(a);
  }
  for (int i = 1; i < n; ++i) {
    C[i] = 1e9;
  }
  dfs(0, 0);
  for (int i = 1; i < n; ++i) {
    if (C[i] == 1e9) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << M[i] + 1 << endl;
  }

  return 0;
}