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

//
//  atcoder.cpp
//
//
///
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REC(f, ...)                                                            \\
  (void)0;                                                                     \\
  const auto f = [&](const auto f, __VA_ARGS__)
using namespace std;
using namespace boost::integer;

using ll = long long;
namespace bst = boost;
template <typename T> class in {
  T value;

public:
  in() { cin >> value; }
  operator const T &() const { return value; }
  operator T &() { return value; }
  in &operator=(T source) {
    value = source;
    return *this;
  }
};
template <> class in<string> : public string {
public:
  in() { cin >> *this; }
};
template <class T> class inseq : public T {
public:
  template <typename... Arg> inseq(Arg... arg) : T(forward<Arg>(arg)...) {
    for (auto &&i : *this)
      cin >> i;
  }
};
template <class T> using invec = inseq<vector<T>>;

auto solve() {
  in<ll> N, M;

  multimap<int, int> ab;

  REP(i, M) {
    int ta, tb;
    cin >> ta >> tb;
    ab.emplace(ta, tb);
    ab.emplace(tb, ta);
  }
  vector<int> r(N - 1, -1);
  vector<bool> visit(N + 1, false);
  deque<int> que = {1};

  while (!que.empty()) {
    int current = que.front();
    que.pop_front();
    visit[current] = true;
    for (auto it = ab.lower_bound(current); it != ab.upper_bound(current); ++it)
      if (!visit[it->second]) {
        if (r[it->second - 2] == -1)
          r[it->second - 2] = current;
        visit[it->second] = true;

        que.push_back(it->second);
      }
  }

  cout << "Yes" << endl;
  for (auto i : r)
    cout << i << endl;
}

template <typename T> void plot(const T &func) { cout << func() << endl; }
template <> void plot<void()>(void (&func)()) { func(); }
template <> void plot<double()>(double (&func)()) {
  cout << setprecision(9) << func() << endl;
}

int main() {
  plot(solve);

  return 0;
}


Error: TLE

Fixed submission:

//
//  atcoder.cpp
//
//
///
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REC(f, ...)                                                            \\
  (void)0;                                                                     \\
  const auto f = [&](const auto f, __VA_ARGS__)
using namespace std;
using namespace boost::integer;

using ll = long long;
namespace bst = boost;
template <typename T> class in {
  T value;

public:
  in() { cin >> value; }
  operator const T &() const { return value; }
  operator T &() { return value; }
  in &operator=(T source) {
    value = source;
    return *this;
  }
};
template <> class in<string> : public string {
public:
  in() { cin >> *this; }
};
template <class T> class inseq : public T {
public:
  template <typename... Arg> inseq(Arg... arg) : T(forward<Arg>(arg)...) {
    for (auto &&i : *this)
      cin >> i;
  }
};
template <class T> using invec = inseq<vector<T>>;

auto solve() {
  in<ll> N, M;

  multimap<int, int> ab;

  REP(i, M) {
    int ta, tb;
    cin >> ta >> tb;
    ab.emplace(ta, tb);
    ab.emplace(tb, ta);
  }
  vector<int> r(N - 1, -1);
  vector<bool> visit(N + 1, false);
  deque<int> que = {1};

  while (!que.empty()) {
    int current = que.front();
    que.pop_front();
    visit[current] = true;
    for (auto it = ab.lower_bound(current); it != ab.upper_bound(current); ++it)
      if (!visit[it->second]) {
        if (r[it->second - 2] == -1)
          r[it->second - 2] = current;
        que.push_back(it->second);
      }
  }

  cout << "Yes" << endl;
  for (auto i : r)
    cout << i << endl;
}

template <typename T> void plot(const T &func) { cout << func() << endl; }
template <> void plot<void()>(void (&func)()) { func(); }
template <> void plot<double()>(double (&func)()) {
  cout << setprecision(9) << func() << endl;
}

int main() {
  plot(solve);

  return 0;
}