alexjercan/bugnet huggingface dataset:
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using GRAPH = vector<vector<ll>>;
const string YES = "Yes";

void bfs(ll start, vector<ll> &prev, const GRAPH &G) {
  queue<ll> que;

  prev[start] = 0;
  que.push(start);

  while (!que.empty()) {
    auto v = que.front();
    que.pop();

    for (auto e : G[v]) {
      if (prev[e] == -1) {
        prev[e] = v;
        que.push(e);
      }
    }
  }
}

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  GRAPH graph(N + 1);
  for (ll i = 0; i < M; i++) {
    graph[A[i]].push_back(B[i]);
    graph[B[i]].push_back(A[i]);
  }
  vector<ll> prev(N + 1, -1);
  bfs(1, prev, graph);

  cout << YES << "\\n";
  copy(begin(prev) + 2, end(prev), ostream_iterator<ll>(cout, "\\n"));
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
// clang-format off
#pragma GCC diagnostic ignored "-Wunused-result"
int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(M);
    std::vector<long long> B(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&A[i]);
        scanf("%lld",&B[i]);
    }
    solve(N, M, std::move(A), std::move(B));
    return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using GRAPH = vector<vector<ll>>;
const string YES = "Yes";

void bfs(ll start, vector<ll> &prev, const GRAPH &G) {
  queue<ll> que;

  prev[start] = 0;
  que.push(start);

  while (!que.empty()) {
    auto v = que.front();
    que.pop();

    for (auto e : G[v]) {
      if (prev[e] == -1) {
        prev[e] = v;
        que.push(e);
      }
    }
  }
}

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  GRAPH graph(M + 1);
  for (ll i = 0; i < M; i++) {
    graph[A[i]].push_back(B[i]);
    graph[B[i]].push_back(A[i]);
  }
  vector<ll> prev(N + 1, -1);
  bfs(1, prev, graph);

  cout << YES << "\\n";
  copy(begin(prev) + 2, end(prev), ostream_iterator<ll>(cout, "\\n"));
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
// clang-format off
#pragma GCC diagnostic ignored "-Wunused-result"
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(M);
    std::vector<long long> B(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&A[i]);
        scanf("%lld",&B[i]);
    }
    solve(N, M, std::move(A), std::move(B));
    return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

int N, M;
int frm[200001] = {};
multimap<int, int> route;
int find_num = 1;
queue<int> que;
;

void execute() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    route.insert(multimap<int, int>::value_type(A, B));
    route.insert(multimap<int, int>::value_type(B, A));
  }

  frm[1] = 1;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    auto p = route.equal_range(v);
    for (auto it = p.first; it != p.second; ++it) {
      int idx = it->second;
      if (frm[idx] == 0) {
        frm[idx] = v;
        que.push(idx);
        if (++find_num == N) {
          cout << "Yes" << endl;
          for (int i = 2; i <= N; i++) {
            cout << frm[i] << endl;
          }
          return;
        }
      }
    }
  }

write:

  cout << "No" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

int N, M;
int d[200001] = {};
int frm[200001] = {};
multimap<int, int> route;
int find_num = 0;

void srch(int start, int depth) {
  auto p = route.equal_range(start);
  for (auto it = p.first; it != p.second; ++it) {
    int idx = it->second;
    if (d[idx] != 0 && d[idx] < depth) {
      // すでにたどり着いている
    } else {
      if (d[idx] == 0)
        find_num++;
      d[idx] = depth;
      frm[idx] = start;
      srch(idx, depth + 1);
    }
  }
}

void execute() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    route.insert(multimap<int, int>::value_type(A, B));
    route.insert(multimap<int, int>::value_type(B, A));
  }

  srch(1, 1);

  if (find_num == N) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << frm[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

struct Data {
  int p, v, l;
  Data(int p, int v, int l) : p(p), v(v), l(l) {}
  bool operator<(const Data &d) const { return l > d.l; }
};

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> to(N);
  ll a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  priority_queue<Data> q;
  vector<pair<ll, ll>> prev_list(N, {INF, -1});
  vector<ll> MEMO(N, INF);
  q.emplace(-1, 0, 0);
  while (!q.empty()) {
    int prev = q.top().p;
    int pos = q.top().v;
    int len = q.top().l;
    q.pop();
    if (prev != -1 && MEMO[pos] <= len)
      continue;
    for (int next_pos : to[pos]) {
      if (MEMO[next_pos] > len + 1) {
        q.emplace(pos, next_pos, len + 1);
      }
    }
    MEMO[pos] = min(MEMO[pos], (ll)len);
    if (prev_list[pos].first > len)
      prev_list[pos] = {len, prev + 1};
  }
  bool flag = true;
  for (int i = 1; i < N; i++) {
    if (prev_list[i].second == -1)
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << prev_list[i].second << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

struct Data {
  int p, v, l;
  Data(int p, int v, int l) : p(p), v(v), l(l) {}
};

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> to(N);
  ll a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  deque<Data> q;
  vector<pair<ll, ll>> prev_list(N, {INF, -1});
  vector<vector<bool>> MEMO(N, vector<bool>(N, false));
  q.emplace_back(-1, 0, 0);
  while (!q.empty()) {
    int prev = q.front().p;
    int pos = q.front().v;
    int len = q.front().l;
    q.pop_front();
    if (prev != -1 && MEMO[prev][pos])
      continue;
    for (int next_pos : to[pos]) {
      q.emplace_back(pos, next_pos, len + 1);
    }
    if (prev != -1)
      MEMO[prev][pos] = true;
    if (prev_list[pos].first > len)
      prev_list[pos] = {len, prev + 1};
  }
  bool flag = true;
  for (int i = 1; i < N; i++) {
    if (prev_list[i].second == -1)
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << prev_list[i].second << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

bool visited[100001];
int official[100001] = {0};

struct input {
  int node1;
  int node2;
};

int main() {
  // gathering input
  int n, m, counter = 1;
  cin >> n >> m;
  input nodes[m];
  queue<int> ans;
  vector<int> connect[n + 1];
  // getting input
  for (int i = 0; i < m; i++) {
    cin >> nodes[i].node1 >> nodes[i].node2;
    connect[nodes[i].node1].push_back(nodes[i].node2);
    connect[nodes[i].node2].push_back(nodes[i].node1);
  }
  // DEBUG
  //    for(int i=0; i<n+1; i++){
  //        for(int k=0; k<connect[i].size(); k++){
  //            cout << connect[i][k] << " ";
  //        }
  //        cout << endl;
  //    }
  // initializing
  visited[1] = true;
  ans.push(1);
  // function
  while (counter <= n and !ans.empty()) {
    int wanted = ans.front();
    ans.pop();
    for (int i = 0; i < connect[wanted].size(); i++) {
      if (!visited[connect[wanted][i]]) {
        visited[connect[wanted][i]] = true;
        ans.push(connect[wanted][i]);
        official[connect[wanted][i]] = wanted;
        counter++;
      }
    }
    if (ans.empty() and counter != n) {
      cout << "No" << endl;
      return 0;
    }
  }
  // end printing
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << official[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

bool visited[100001];
int official[100001] = {0};

struct input {
  int node1;
  int node2;
};

int main() {
  // gathering input
  int n, m;
  cin >> n >> m;
  input nodes[m];
  queue<int> ans;
  int counter = 1;
  // getting input
  for (int i = 0; i < m; i++) {
    cin >> nodes[i].node1 >> nodes[i].node2;
  }
  // initializing
  visited[1] = true;
  ans.push(1);
  // function
  while (counter < n and !ans.empty()) {
    int wanted = ans.front();
    ans.pop();
    for (int i = 0; i < m; i++) {
      if (nodes[i].node1 == wanted and !visited[nodes[i].node2]) {
        ans.push(nodes[i].node2);
        visited[nodes[i].node2] = true;
        counter++;
        official[nodes[i].node2] = wanted;
      } else if (nodes[i].node2 == wanted and !visited[nodes[i].node1]) {
        ans.push(nodes[i].node1);
        visited[nodes[i].node1] = true;
        counter++;
        official[nodes[i].node1] = wanted;
      }
    }
    if (counter == n) {
      break;
    }
    if (counter != n and ans.empty() == true) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << official[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define repr(i, x, y) for (int i = x; i >= y; i--)
#define mod 1000000007
using namespace std;
int depth[100000], parent[100000];
vector<int> g[100000];
void Onigiri() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }

  rep(i, 0, n) depth[i] = INT64_MAX;

  depth[0] = 0;
  parent[0] = 0;

  set<pair<int, int>> s;
  s.insert({0, 0});
  while (!s.empty()) {
    int v = (*s.begin()).second;
    int dist = (*s.begin()).first;
    s.erase(s.begin());
    for (auto x : g[v]) {
      if (depth[x] > dist + 1) {
        s.insert({dist + 1, x});
        depth[x] = dist + 1;
        parent[x] = v;
      }
    }
  }

  if (count(depth, depth + n, -1) > 0)
    cout << "No";
  else {
    cout << "Yes\\n";
    rep(i, 1, n) { cout << parent[i] + 1 << '\\n'; }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef Zoro
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int t = 1;
  // cin>>t;

  while (t--) {
    Onigiri();
    cout << "\\n";
  }

  cerr << "\\n" << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define repr(i, x, y) for (int i = x; i >= y; i--)
#define mod 1000000007
using namespace std;
int depth[100000], parent[100000];
vector<int> g[100000];
void dfs(int v, int p, int d) {
  if (depth[v] == -1)
    parent[v] = p, depth[v] = d;
  else if (depth[v] > d)
    parent[v] = p, depth[v] = d;

  for (auto x : g[v]) {
    if (depth[x] == -1)
      dfs(x, v, d + 1);
    else if (depth[x] > d + 1)
      dfs(x, v, d + 1);
  }
}
void Onigiri() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  memset(depth, -1, sizeof(depth));
  memset(depth, -1, sizeof(depth));
  parent[1] = 1;
  dfs(0, 0, 0);
  if (count(depth, depth + n, -1) > 0)
    cout << "No";
  else {
    cout << "Yes\\n";
    rep(i, 1, n) { cout << parent[i] + 1 << '\\n'; }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef Zoro
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int t = 1;
  // cin>>t;

  while (t--) {
    Onigiri();
    cout << "\\n";
  }

  cerr << "\\n" << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, m, ch[200010];
  cin >> n >> m;
  vector<ll> a[200010];
  for (ll i = 0; i < n; i++) {
    ch[i] = -1;
  }
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  ch[0] = 0;
  queue<ll> q;
  q.push(0);
  while (q.size() != 0) {
    ll now = q.front();
    q.pop();
    for (ll i = 0; i < a[now].size(); i++) {
      ll next = a[now][i];
      if (ch[next] != -1) {
        continue;
      }
      ch[next] = now + 1;
      q.push(next);
    }
  }
  cout << "Yes" << endl;
  for (ll i = 1; i < n; i++) {
    cout << ch[i] << endl;
  }
  // your code goes here
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, m, ch[100010];
  cin >> n >> m;
  vector<ll> a[100010];
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
    ch[i] = -1;
  }
  ch[0] = 0;
  queue<ll> q;
  q.push(0);
  while (q.size() != 0) {
    ll now = q.front();
    q.pop();
    for (ll i = 0; i < a[now].size(); i++) {
      ll next = a[now][i];
      if (ch[next] != -1) {
        continue;
      }
      ch[next] = now + 1;
      q.push(next);
    }
  }
  cout << "No" << endl;
  return 0;
  for (ll i = 1; i < n; i++) {
    cout << ch[i] << endl;
  }
  // your code goes here
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
#define mod 200003
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  ll n, m;
  cin >> n >> m;
  vl g[n];
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n + 10);
  queue<ll> q;
  q.push(0);
  vector<bool> seen(n);
  seen[0] = 1;
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
#define mod 200003
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  ll n, m;
  cin >> n >> m;
  vl g[n];
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans{n + 10};
  queue<ll> q;
  q.push(0);
  vector<bool> seen(n);
  seen[0] = 1;
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
using pl = pair<ll, ll>;
signed main() {
  ll n, m;
  cin >> n >> m;
  vvl g(n, vl(0));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n);
  vector<bool> seen(n);
  seen[0] = 1;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
using pl = pair<ll, ll>;
signed main() {
  ll n, m;
  cin >> n >> m;
  vvl g(n, vl(n));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n);
  vector<bool> seen(n);
  seen[0] = 1;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  vector<int> milestone(n, INF);
  queue<int> que;
  milestone[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (milestone[nv] != INF)
        continue;
      milestone[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 1; i <= n - 1; i++) {
    cout << milestone[i] + 1 << "\\n";
  }
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  vector<int> milestone(n, INF);
  queue<int> que;
  milestone[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      milestone[nv] = min(milestone[nv], milestone[v] + 1);
      que.push(nv);
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 1; i <= n - 1; i++) {
    cout << milestone[i] << "\\n";
  }
}