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

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

template <typename T> ll com(T n, T m, T mod) {
  ll a = 1, b = 1, ans;
  if (n == m || m == 0) {
    ans = 1;
  } else {
    for (ll i = 0; i < m; i++) {
      a *= n - i;
      a %= mod;
      b *= i + 1;
      b %= mod;
    }
    ans = (a * powmod(b, mod - 2, mod)) % mod;
  }

  return ans;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> ans(N, -1);
  vector<vector<int>> que(N);
  rep(i, 0, M) {
    int Ai, Bi;
    cin >> Ai >> Bi;

    que[Ai - 1].push_back(Bi - 1);
    que[Bi - 1].push_back(Ai - 1);
  }

  // 幅優先探索
  queue<int> detect;
  detect.push(0);

  while (!detect.empty()) {
    int now = detect.front();
    detect.pop();

    for (auto &node : que[now]) {
      if (ans[node] != -1)
        continue;

      ans[node] = now;
      detect.push(node);
    }
  }

  cout << "Yes" << endl;
  rep(i, 1, N) { cout << ans[i] + 1 << endl; }

  // cout << setprecision(20) << ans << endl;  // For double
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

template <typename T> ll com(T n, T m, T mod) {
  ll a = 1, b = 1, ans;
  if (n == m || m == 0) {
    ans = 1;
  } else {
    for (ll i = 0; i < m; i++) {
      a *= n - i;
      a %= mod;
      b *= i + 1;
      b %= mod;
    }
    ans = (a * powmod(b, mod - 2, mod)) % mod;
  }

  return ans;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> v(N, -1), ans(N, -1);
  vector<vector<int>> way(N, v);
  vector<set<int>> que(N);
  rep(i, 0, M) {
    int Ai, Bi;
    cin >> Ai >> Bi;
    way[Ai - 1][Bi - 1] = 1;
    way[Ai - 1][Bi - 1] = 1;
    que[Ai - 1].insert(Bi - 1);
    que[Bi - 1].insert(Ai - 1);
  }

  // 幅優先探索
  queue<int> detect;
  detect.push(0);

  while (!detect.empty()) {
    int now = detect.front();
    detect.pop();

    for (auto &node : que[now]) {
      if (ans[node] != -1)
        continue;

      ans[node] = now;
      detect.push(node);
    }
  }

  cout << "Yes" << endl;
  rep(i, 1, N) { cout << ans[i] + 1 << endl; }

  // cout << setprecision(20) << ans << endl;  // For double
}