alexjercan/bugnet huggingface dataset:
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
const int INF = 1001001001;
int main() {
  int N, M;
  // vector<int>to[100005];
  cin >> N >> M;
  vector<vector<int>> to(N);
  for (int n = 0; n < M; ++n) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  queue<int> q;
  vector<int> dist(N, INF); // 深さ
  vector<int> pre(N, -1);   // 直前
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF) {
        continue;
      } else {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }

  bool bRet = true;
  for (int n = 1; n < N; ++n) {
    if (-1 == pre[n]) {
      bRet = false;
      break;
    }
  }
  if (bRet) {
    cout << "Yes" << endl;
    for (int n = 1; n < N; ++n) {
      cout << pre[n] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (int n = 0; n < M; ++n) {
    cin >> A[n] >> B[n];
  }

  vector<int> C(N);
  set<int> st = {1};
  for (int m = 0; m < M; ++m) {
    set<int> temp;
    for (int m1 = 0; m1 < M; ++m1) {
      set<int>::iterator it = st.find(A[m1]);
      if (it != st.end()) {
        if (0 == C[B[m1] - 1]) {
          C[B[m1] - 1] = A[m1];
          temp.insert(B[m1]);
        }
      }
    }
    for (int m2 = 0; m2 < M; ++m2) {
      set<int>::iterator it = st.find(B[m2]);
      if (it != st.end()) {
        if (0 == C[A[m2] - 1]) {
          C[A[m2] - 1] = B[m2];
          temp.insert(A[m2]);
        }
      }
    }
    st = temp;
  }

  bool bRet = true;
  for (int n = 1; n < N; ++n) {
    if (0 == C[n]) {
      bRet = false;
      break;
    }
  }
  if (bRet) {
    cout << "Yes" << endl;
    for (int n = 1; n < N; ++n) {
      cout << C[n] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iomanip>
#include <iostream>

#include <array>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define int64 long long
using namespace std;

int main() {
  int64 nn, mm;
  cin >> nn >> mm;
  int64 tmpa, tmpb, now, xx;
  vector<int64> memo[nn];
  int64 visited[nn];
  for (int64 ii = 0; ii < nn; ii++) {
    visited[ii] = -1;
  }
  for (int64 ii = 0; ii < mm; ii++) {
    cin >> tmpa >> tmpb;
    memo[tmpa - 1].push_back(tmpb - 1);
    memo[tmpb - 1].push_back(tmpa - 1);
  }
  queue<int64> que;
  que.push(0);
  while (!que.empty()) {
    now = que.front();
    que.pop();
    for (int ii = 0; ii < memo[now].size(); ii++) {
      xx = memo[now].at(ii);
      if (visited[xx] == -1) {
        visited[xx] = now;
        que.push(xx);
      }
    }
  }

  cout << "Yes\\n";
  for (int64 ii = 1; ii < nn; ii++) {
    cout << (visited[ii] + 1) << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>

#include <array>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define int64 long long
using namespace std;

int main() {
  int64 nn, mm;
  cin >> nn >> mm;
  int64 tmpa, tmpb, now;
  int64 memo[nn][nn];
  int64 visited[nn];
  for (int64 ii = 0; ii < nn; ii++) {
    visited[ii] = -1;
    for (int64 jj = 0; jj < nn; jj++) {
      memo[ii][jj] = 0;
    }
  }
  for (int64 ii = 0; ii < mm; ii++) {
    cin >> tmpa >> tmpb;
    memo[tmpa - 1][tmpb - 1] = 1;
    memo[tmpb - 1][tmpa - 1] = 1;
  }
  queue<int64> que;
  que.push(0);
  while (!que.empty()) {
    now = que.front();
    que.pop();
    for (int ii = 1; ii < nn; ii++) {
      if (ii != now && memo[now][ii] && visited[ii] == -1) {
        visited[ii] = now;
        que.push(ii);
      }
    }
  }

  cout << "Yes\\n";
  for (int64 ii = 1; ii < nn; ii++) {
    cout << (visited[ii] + 1) << '\\n';
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n, vector<int>(0));
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    v.at(a - 1).push_back(b);
    v.at(b - 1).push_back(a);
  }

  vector<int> r;
  r.push_back(1);
  for (int i = 0; i < n - 1; ++i) {
    r.push_back(0);
  }

  int res = 1;
  list<int> next = {1};
  while (true) {
    list<int> new_next = {};
    for (list<int>::iterator itn = next.begin(); itn != next.end(); ++itn) {
      vector<int> vv = v.at((*itn) - 1);
      for (int j = 0; j < vv.size(); ++j) {
        int t = vv.at(j);
        if (r.at(t - 1) == 0) {
          r.at(t - 1) = (*itn);
          new_next.push_back(t);
          res++;
        }
      }
    }

    if (new_next.size() == 0) {
      break;
    }
    next = new_next;
  }

  if (res == n) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; ++i) {
      cout << r.at(i) << endl;
    }
  } else {
    cout << "No" << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  list<vector<int>> l;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    vector<int> v = {a, b};
    l.push_back(v);
  }

  vector<int> v;
  v.push_back(1);
  for (int i = 0; i < n - 1; ++i) {
    v.push_back(0);
  }

  list<int> next = {1};
  while (true) {
    list<int> new_next = {};
    for (list<int>::iterator itn = next.begin(); itn != next.end(); ++itn) {
      for (list<vector<int>>::iterator it = l.begin(); it != l.end(); ++it) {
        if ((*it).at(0) == (*itn) && v.at((*it).at(1) - 1) == 0) {
          v.at((*it).at(1) - 1) = (*itn);
          new_next.push_back((*it).at(1));
        }
        if ((*it).at(1) == (*itn) && v.at((*it).at(0) - 1) == 0) {
          v.at((*it).at(0) - 1) = (*itn);
          new_next.push_back((*it).at(0));
        }
      }
    }

    if (new_next.size() == 0) {
      break;
    }
    next = new_next;
  }

  bool result = true;
  for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
    if ((*it) == 0) {
      result = false;
      break;
    }
  }
  if (result) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; ++i) {
      cout << v.at(i) << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char **argv) {
  int A, B, N, M;
  bool flag = true;
  std::vector<int> room, ans, num;
  std::vector<std::vector<int>> way;
  std::cin >> N >> M;
  ans.resize(N, -1);
  way.resize(N);
  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    way[A - 1].push_back(B);
    way[B - 1].push_back(A);
  }
  for (int i = 0; i < N; i++) {
    std::sort(way[i].begin(), way[i].end());
    way[i].erase(std::unique(way[i].begin(), way[i].end()), way[i].end());
  }
  num.push_back(1);
  ans[0] = 0;
  while (1) {
    for (int x : num) {
      for (int y : way[x - 1]) {
        if (ans[y - 1] < 0) {
          room.push_back(y);
          ans[y - 1] = x;
        }
      }
    }
    num.clear();
    for (int x : room) {
      num.push_back(x);
    }
    if (!num.empty()) {
      room.clear();
      num.erase(std::unique(num.begin(), num.end()), num.end());
    } else {
      break;
    }
  }
  for (int x : ans) {
    if (x < 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    std::cout << "Yes" << std::endl;
    for (int x : ans) {
      if (x > 0) {
        std::cout << x << std::endl;
      }
    }
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char **argv) {
  int A, B, N, M;
  bool flag = true;
  std::vector<int> room, ans, num;
  std::vector<std::vector<int>> way;
  std::cin >> N >> M;
  room.resize(N, -1);
  ans.resize(N, -1);
  way.resize(N);
  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    way[A - 1].push_back(B);
    way[B - 1].push_back(A);
  }
  for (int i = 0; i < N; i++) {
    std::sort(way[i].begin(), way[i].end());
    way[i].erase(std::unique(way[i].begin(), way[i].end()), way[i].end());
  }
  num.push_back(1);
  room[0] = 0;
  ans[0] = 0;
  while (1) {
    for (int x : num) {
      for (int y : way[x - 1]) {
        if (room[y - 1] < 0) {
          room[y - 1] = x;
          ans[y - 1] = x;
        }
      }
    }
    num.clear();
    for (int i = 0; i < N; i++) {
      if (room[i] > 0) {
        num.push_back(i + 1);
        room[i] = 0;
      }
    }
    if (num.empty()) {
      break;
    } else {
      num.erase(std::unique(num.begin(), num.end()), num.end());
    }
  }
  for (int x : ans) {
    if (x < 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    std::cout << "Yes" << std::endl;
    for (int x : ans) {
      if (x > 0) {
        std::cout << x << std::endl;
      }
    }
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <iostream>
#include <queue>
#include <vector>

#define FOR(i, num) for (i = 0; i < num; i++)

using namespace std;

int main() {
  vector<int> next_list[202020];
  int N, M;
  cin >> N >> M;

  int i;
  FOR(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    next_list[a].push_back(b);
    next_list[b].push_back(a);
  }

  queue<int> check_rooms;
  array<int, 202020> mark;
  mark.fill(-1);

  check_rooms.push(0);
  mark[0] = 0;
  while (check_rooms.size() > 0) {
    int cur_room = check_rooms.front();
    check_rooms.pop();

    for (auto &next_room : next_list[cur_room]) {
      // 決定済みの部屋はスキップ
      if (mark[next_room] != -1) {
        continue;
      }
      mark[next_room] = cur_room + 1;
      check_rooms.push(next_room);
    }
  }

  bool is_ok = true;
  FOR(i, N) {
    if (mark[i] == -1) {
      is_ok = false;
      break;
    }
  }
  if (is_ok) {
    cout << "Yes" << endl;
    for (i = 1; i < N; i++) {
      cout << mark[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <array>
#include <iostream>
#include <list>
#include <vector>

#define FOR(i, num) for (i = 0; i < num; i++)

using namespace std;

int main() {
  array<int, 202020> dist;
  vector<int> next_list[202020];
  int N, M;
  cin >> N >> M;

  int i;
  FOR(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    next_list[a].push_back(b);
    next_list[b].push_back(a);
  }

  list<int> front_rooms;
  array<int, 202020> mark;
  dist.fill(202020);
  mark.fill(-1);

  front_rooms.push_back(0);
  dist[0] = 0;
  mark[0] = 0;
  while (1) {
    int next_det = 0;
    int prev_room = 0;
    int min_dist = 202020;
    int will_remove_front = -1;
    for (auto &it : front_rooms) {
      bool is_det = true;
      for (auto &next_room : next_list[it]) {
        // 決定済みの部屋はスキップ
        if (mark[next_room] != -1) {
          continue;
        }
        if (dist[next_room] > dist[it] && min_dist > dist[it]) {
          min_dist = dist[it] + 1;
          dist[next_room] = dist[it] + 1;
          ;
          next_det = next_room;
          prev_room = it;
        }
        is_det = false;
      }
    }

    // 行進なければ
    if (next_det == 0) {
      break;
    } else {
      front_rooms.push_back(next_det);
      mark[next_det] = prev_room + 1;
    }
  }

  bool is_ok = true;
  FOR(i, N) {
    if (mark[i] == -1) {
      is_ok = false;
      break;
    }
  }
  if (is_ok) {
    cout << "Yes" << endl;
    for (i = 1; i < N; i++) {
      cout << mark[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  cin >> n >> m;
  int hint[n];
  vector<int> arc[m + 10];

  for (int i = 0; i < n; i++)
    hint[i] = -1;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    arc[a].push_back(b);
    arc[b].push_back(a);
  }

  queue<int> room;
  room.push(0);
  while (!room.empty()) {
    int r = room.front();
    room.pop();
    for (int i = 0; i < arc[r].size(); i++) {
      int to = arc[r][i];
      if (hint[to] == -1) {
        hint[to] = r;
        room.push(to);
      }
    }
  }

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


Error: 0

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  int hint[n];
  vector<int> arc[m];
  for (int i = 0; i < n; i++)
    hint[i] = -1;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    arc[a].push_back(b);
    arc[b].push_back(a);
  }

  queue<int> room;
  room.push(0);
  while (!room.empty()) {
    int r = room.front();
    room.pop();
    for (int i = 0; i < arc[r].size(); i++) {
      int to = arc[r][i];
      if (hint[to] == -1) {
        hint[to] = r;
        room.push(to);
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << hint[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iomanip> //cout << fixed << setprecision() <<
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using ll = long long;
using namespace std;
vector<vector<int>> v;
vector<int> v2;
queue<int> q;

void monmo(int n) {
  int momo;
  for (int i = 0; i < v[n].size(); i++) {
    if (v2[v[n][i]] == 0) {
      v2[v[n][i]] = n;
      q.emplace(v[n][i]);
    }
  }
  if (!q.empty()) {
    momo = q.front();
    q.pop();
    monmo(momo);
  }
}

int main(void) {
  int n, m;
  int a, b;
  cin >> n >> m;
  v.emplace_back(); // v[0]は無視
  v2.push_back(0);  // v2[0]は無視
  for (int i = 0; i < n; i++) {
    v.emplace_back();
    v2.push_back(0);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

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


Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip> //cout << fixed << setprecision() <<
#include <iostream>
#include <map>
#include <set>
#include <vector>
using ll = long long;
using namespace std;
vector<vector<int>> v;
vector<int> v2, v3;

void monmo(int n, int keta) {
  for (int i = 0; i < v[n].size(); i++) {
    if (v2[v[n][i]] == 0) {
      v2[v[n][i]] = n;
      v3[v[n][i]] = keta;
      monmo(v[n][i], keta + 1);
    } else {
      if (v3[v[n][i]] > keta) {
        v2[v[n][i]] = n;
        v3[v[n][i]] = keta;
        monmo(v[n][i], keta + 1);
      }
    }
  }
}

int main(void) {
  int n, m;
  int a, b;
  cin >> n >> m;
  v.emplace_back();      // v[0]は無視
  v2.push_back(0);       // v2[0]は無視
  v3.push_back(1000000); // v3[0]は無視
  for (int i = 0; i < n; i++) {
    v.emplace_back();
    v2.push_back(0);
    v3.push_back(1000000);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  monmo(1, 0);
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << v2[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (long long int i = f; i < t; i++)
#define pr(i, f, t) for (long long int i = t - 1; i >= f; i--)
#define ca(n, a)                                                               \\
  rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\\n" : " ")
#define ia(n, a) rp(ia_i, 0, n) cin >> a[ia_i]
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
#define ce(a) cout << (a) << endl
ll vis[222222];
ll a[222222], b[222222];
vl edge[222222];
vl que;
void dfs(ll s) {
  rp(i, 0, edge[s].size()) {
    if (vis[edge[s][i]] == 0) {
      vis[edge[s][i]] = s + 1;
      que.push_back(edge[s][i]);
    }
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  rp(i, 0, m) cin >> a[i] >> b[i];
  rp(i, 0, m) {
    edge[a[i] - 1].push_back(b[i] - 1);
    edge[b[i] - 1].push_back(a[i] - 1);
  }
  vis[0] = 1;
  vl init, que2;
  que.push_back(0);
  while (que.size() > 0) {
    que2 = que;
    que = init;
    rp(i, 0, que2.size()) { dfs(que2[i]); }
  }
  ce("Yes");
  rp(i, 1, n) { ce(vis[i]); }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (long long int i = f; i < t; i++)
#define pr(i, f, t) for (long long int i = t - 1; i >= f; i--)
#define ca(n, a)                                                               \\
  rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\\n" : " ")
#define ia(n, a) rp(ia_i, 0, n) cin >> a[ia_i]
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
#define ce(a) cout << (a) << endl
ll vis[111111];
ll a[222222], b[222222];
vl edge[111111];
vl que;
ll dfs(ll s) {
  //  ce(s);
  // ca(6, vis);
  rp(i, 0, edge[s].size()) {

    if (vis[edge[s][i]] == 0) {
      vis[edge[s][i]] = s + 1;
      que.push_back(edge[s][i]);
    }
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  rp(i, 0, m) cin >> a[i] >> b[i];
  rp(i, 0, m) {
    edge[a[i] - 1].push_back(b[i] - 1);
    edge[b[i] - 1].push_back(a[i] - 1);
  }
  vis[0] = 1;
  vl init, que2;
  que.push_back(0);
  while (que.size() > 0) {
    que2 = que;
    que = init;
    rp(i, 0, que2.size()) { dfs(que2[i]); }
  }
  ce("Yes");
  rp(i, 1, n) { ce(vis[i]); }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// TEL
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> pass(N, vector<int>(0));
  int A, B;
  rep(i, M) {
    cin >> A >> B;
    pass[A - 1].push_back(B - 1);
    pass[B - 1].push_back(A - 1);
  }

  vector<int> deep(N, -1);
  queue<int> bfs;
  vector<int> root(N, -1);
  bfs.push(0);
  deep[0] = 0;

  int now;
  int nextnow;
  int minnum, memo;
  while (!bfs.empty()) {
    now = bfs.front();
    bfs.pop();
    memo = 10000000;
    minnum = 0;
    if (root[now] != -1)
      continue;
    rep(i, pass[now].size()) {
      nextnow = pass[now][i];
      if (deep[nextnow] == -1) {
        bfs.push(nextnow);
      } else {
        if (memo > deep[nextnow]) {
          minnum = nextnow;
          memo = deep[nextnow];
        }
      }
    }
    root[now] = minnum;
    if (memo != 10000000)
      deep[now] = memo + 1;
    else
      deep[now] = 0;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// なんかうまくいかない。解説会聞こう！！
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> pass(N, vector<int>(0));
  int A, B;
  rep(i, M) {
    cin >> A >> B;
    pass[A - 1].push_back(B - 1);
    pass[B - 1].push_back(A - 1);
  }

  vector<int> deep(N, -1);
  queue<int> bfs;
  vector<int> root(N, -1);
  bfs.push(0);
  deep[0] = 0;

  int now;
  int nextnow;
  int minnum, memo;
  while (!bfs.empty()) {
    now = bfs.front();
    bfs.pop();
    memo = 10000000;
    minnum = 0;
    // if(vis[now] != -1) continue;
    rep(i, pass[now].size()) {
      nextnow = pass[now][i];
      if (deep[nextnow] == -1) {
        bfs.push(nextnow);
      } else {
        if (memo > deep[nextnow]) {
          minnum = nextnow;
          memo = deep[nextnow];
        }
      }
    }
    root[now] = minnum;
    if (memo != 10000000)
      deep[now] = memo + 1;
    else
      deep[now] = 0;
  }

  cout << "Yes" << endl;
  rep(i, N - 1) {
    cout << root[i + 1] + 1 << endl;
    ;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define INF 99999999
#define INF_LL 1LL << 60
#define MOD (ll)1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, a, n) for (int i = a; i < (int)(n); i++)
#define RREP(i, a, n) for (int i = (int)(n); i >= a; i--)
#define all(x) (x).begin(), (x).end()

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;
}

void put_double(double obj) { printf("%.12f\\n", obj); };

template <typename T> std::string to_string(const T &n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}

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

  vector<int> a(M);
  vector<int> b(M);
  vector<vector<int>> points(N, vector<int>());
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    points[a[i]].push_back(b[i]);
    points[b[i]].push_back(a[i]);
  }

  vector<int> dist(N, -1);
  vector<int> ans(N, -1);
  queue<int> que;
  int key = 0;

  que.push(0);

  while (!que.empty()) {
    int p = que.front();
    que.pop();

    rep(i, points[p].size()) {
      if (dist[points[p][i]] == -1) {
        dist[points[p][i]] = dist[p] + 1;
        ans[points[p][i]] = p + 1;
        que.push(points[p][i]);
      }
    }
  }
  cout << "Yes" << endl;
  REP(i, 1, N) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define INF 99999999
#define INF_LL 1LL << 60
#define MOD (ll)1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, a, n) for (int i = a; i < (int)(n); i++)
#define RREP(i, a, n) for (int i = (int)(n); i >= a; i--)
#define all(x) (x).begin(), (x).end()

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;
}

void put_double(double obj) { printf("%.12f\\n", obj); };

template <typename T> std::string to_string(const T &n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}

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

  vector<int> a(M);
  vector<int> b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  vector<int> dist(N, -1);
  vector<int> ans(N, -1);
  queue<int> que;
  int key = 0;

  que.push(0);

  while (!que.empty()) {
    if (key == N)
      break;
    int p = que.front();
    que.pop();

    rep(i, M) {
      if (a[i] == p && dist[b[i]] == -1) {
        dist[b[i]] = dist[a[i]] + 1;
        ans[b[i]] = a[i] + 1;
        que.push(b[i]);
        key++;
      }
      if (b[i] == p && dist[a[i]] == -1) {
        dist[a[i]] = dist[b[i]] + 1;
        ans[a[i]] = b[i] + 1;
        que.push(a[i]);
        key++;
      }
    }
  }
  cout << "Yes" << endl;
  REP(i, 1, N) cout << ans[i] << endl;
}