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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define INF 1000000000
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
// #define __DEBUG__
#ifdef __DEBUG__
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#endif
#ifndef __DEBUG__
#define DEBUG(x)
#define DEBUGS(v)
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\\n";
/* clang-format on */

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> child(n, vector<int>(0));
  vector<int> ans(n, -1);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    child.at(a - 1).push_back(b - 1);
    child.at(b - 1).push_back(a - 1);
  }
  set<int> s;

  s.insert(0);
  while (!s.empty()) {
    set<int> next;
    for (auto x : s) {
      DEBUG(x);
      for (auto &c : child[x]) {
        DEBUG(c);
        if (ans[c] == -1) {
          ans[c] = x;
          next.insert(c);
        }
        DEBUGS(ans);
        if (!child[c].empty())
          child[c].erase(find(ALL(child[c]), x));
        REP(i, n) { DEBUGS(child[i]); }
      }
      s.erase(x);
    }
    for (auto &ne : next) {
      s.insert(ne);
    }
  }

  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define INF (INT_MAX / 4)
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\\n";
/* clang-format on */

int tansaku(vector<int> &x) {}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> tmp(n, vector<int>(0));
  // vector<bool> f(n, false);
  vector<int> ans(n, 0);
  vector<vector<int>> cost(n + 1, vector<int>(0));
  vector<int> cost2(n, INF);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    tmp.at(a - 1).push_back(b - 1);
    tmp.at(b - 1).push_back(a - 1);
  }
  cost2.at(0) = 0;
  cost.at(0).push_back(0);

  for (int i = 0; i < n; i++) {
    for (auto x : cost.at(i)) {

      for (auto y : tmp.at(x)) {
        cost.at(i + 1).push_back(y);
        if (cost2.at(y) > i) {
          cost2.at(y) = i;
          ans.at(y) = x;
        }
      }
    }
  }

  cout << "Yes\\n";
  REP(i, n - 1) { cout << ans.at(i + 1) + 1 << "\\n"; }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const double pi = 3.14159265358979323846;
const ll mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  graph G(n + 1);
  vector<int> ans(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
    if (a == 1)
      ans.at(b) = 1;
    if (b == 1)
      ans.at(a) = 1;
  }
  queue<int> que;
  for (int v : G.at(1)) {
    que.push(v);
  }
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G.at(v)) {
      if (ans.at(nv) != 0)
        continue;
      ans.at(nv) = v;
      que.push(nv);
    }
  }
  bool ok = true;
  for (int i = 1; i < n + 1; i++) {
    if (ans.at(i) == 0) {
      ok = false;
      break;
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      cout << ans.at(i) << endl;
    }
  } else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

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

const double pi = 3.14159265358979323846;
const ll mod = 1000000007;

vector<int> ans;
vector<int> order;

void dfs(int pv, int v, graph &G, int cnt) {
  if (order.at(v) > cnt) {
    order.at(v) = cnt;
    ans.at(v) = pv;
  }
  for (auto nv : G.at(v)) {
    if (order.at(nv) < cnt)
      continue;
    dfs(v, nv, G, cnt + 1);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  graph G(n + 1);
  ans.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
    if (a == 1)
      ans.at(b) = 1;
    if (b == 1)
      ans.at(a) = 1;
  }
  order.resize(n + 1);
  for (int i = 0; i < n + 1; i++) {
    order.at(i) = 1001001001;
  }
  for (int i = 0; i < G.at(1).size(); i++) {
    int cnt = 1;
    dfs(1, G.at(1).at(i), G, cnt);
  }
  bool ok = true;
  for (int i = 1; i < n + 1; i++) {
    if (ans.at(i) == 0)
      ok = false;
  }
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      cout << ans.at(i) << endl;
    }
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, M;
  cin >> N >> M;
  ll A, B;
  vector<vector<ll>> G(N);
  for (ll i = 0; i < M; i++) {
    cin >> A >> B;
    A--;
    B--;
    G[A].push_back(B);
    G[B].push_back(A);
  }
  vector<ll> dist(N, -1); // 未訪問に初期化
  queue<ll> q;

  dist[0] = 0;
  q.push(0);
  vector<ll> ans(N);

  while (!q.empty()) {
    ll v = q.front();
    q.pop();

    for (ll nv : G[v]) {
      if (dist[nv] != -1) {
        continue;
      }
      dist[nv] = dist[v] + 1;
      ans[nv] = v + 1;
      q.push(nv);
    }
  }

  for (ll i = 1; i < N; i++) {
    if (dist[i] == -1) {
      cout << "No" << endl;
      exit(0);
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  ll N, M;
  cin >> N >> M;
  ll A, B;
  vector<vector<ll>> G(M);
  for (ll i = 0; i < M; i++) {
    cin >> A >> B;
    A--;
    B--;
    G[A].push_back(B);
    G[B].push_back(A);
  }
  vector<ll> dist(N, -1); // 未訪問に初期化
  queue<ll> q;

  dist[0] = 0;
  q.push(0);
  vector<ll> ans(N);

  while (!q.empty()) {
    ll v = q.front();
    q.pop();

    for (ll nv : G[v]) {
      if (dist[nv] != -1) {
        continue;
      }
      dist[nv] = dist[v] + 1;
      ans[nv] = v + 1;
      q.push(nv);
    }
  }
  /*
  for (ll i=1; i<N; i++) {
      if (dist[i]==-1) {
          cout << "No" << endl;
          exit(0);
      }
  }
  */
  cout << "Yes" << endl;
  for (ll i = 1; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> void chmax(T &a, T b) { a > b ?: a = b; }
template <class T> void chmin(T &a, T b) { a < b ?: a = b; }

vector<int> g[100000];
int ans[100000], d[100000];

// void dfs(int u, int v, int dd) {
//     if (d[v] <= dd)
//         return;
//     d[v] = dd;
//     ans[v] = u;
//     for (auto t : g[v]) {
//         dfs(v, t, dd + 1);
//     }
// }

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

  fill(d, d + n, INF);
  d[0] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    auto t = q.top();
    int u = t.second;
    int dd = t.first;
    q.pop();

    for (auto v : g[u]) {
      if (d[v] > dd + 1) {
        d[v] = dd + 1;
        ans[v] = u;
        q.push(make_pair(dd + 1, v));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    if (ans[i] == INF) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> void chmax(T &a, T b) { a > b ?: a = b; }
template <class T> void chmin(T &a, T b) { a < b ?: a = b; }

vector<int> g[100000];
int ans[100000], d[100000];

void dfs(int u, int v, int dd) {
  if (d[v] <= dd)
    return;
  d[v] = dd;
  ans[v] = u;
  for (auto t : g[v]) {
    dfs(v, t, dd + 1);
  }
}

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

  fill(d, d + n, INF);
  dfs(0, 0, 0);

  for (int i = 0; i < n; ++i) {
    if (ans[i] == INF) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define N_MAX 100010
#define M_MAX 200010

const int INF = 1001001001;
vector<vector<int>> to(N_MAX);

int main() {
  int N, M;

  cin >> N >> M;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to.at(a).push_back(b);
    to.at(b).push_back(a);
  }

  queue<int> q;
  vector<int> dist(N, INF);
  vector<int> pre(N, -1);
  dist.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to.at(v)) {
      if (dist.at(u) != INF)
        continue;
      dist.at(u) = dist.at(v) + 1;
      pre.at(u) = v;
      q.push(u);
    }
  }

  cout << "Yes" << endl;
  rep(i, N) {
    if (i == 0)
      continue;
    int ans = pre.at(i);
    ans++;
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define N_MAX 100010
#define M_MAX 200010

int main() {
  int N, M;
  vector<vector<int>> dp(M_MAX);
  vector<vector<int>> G(M_MAX);
  vector<int> index(N_MAX, -1);

  int count = 0;

  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;

    if (a == 1) {
      dp.at(a).push_back(b);
      index.at(b) = 1;
      count++;
    } else if (b == 1) {
      dp.at(b).push_back(a);
      index.at(a) = 1;
      count++;
    } else {
      G.at(a).push_back(b);
      G.at(b).push_back(a);
    }
  }

  if (count == N - 1) {
    cout << "Yes" << endl;
    for (int j = 2; j <= N; j++) {
      cout << index.at(j) << endl;
    }
    return 0;
  }

  int i = 1;
  while (true) {
    for (int u : dp.at(i)) {
      for (int v : G.at(u)) {
        if (index.at(v) == -1) {
          index.at(v) = u;
          dp.at(i + 1).push_back(v);
          count++;
          if (count == N - 1) {
            cout << "Yes" << endl;
            for (int j = 2; j <= N; j++) {
              cout << index.at(j) << endl;
            }
            return 0;
          }
        }
      }
    }
    i++;
  }

  cout << "No" << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

const int INT_INF = 1e9;

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> range(n + 1, INT_INF);
  range[1] = 0;
  int a, b;
  vector<queue<int>> rin(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    rin[a].push(b);
    rin[b].push(a);
  }

  queue<int> q;
  vector<int> parent(n + 1);
  vector<bool> used(n + 1, false);
  used[1] = true;
  q.push(1);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    while (!rin[temp].empty()) {
      int i = rin[temp].front();
      rin[temp].pop();
      if (used[i] == false) {
        q.push(i);
        used[i] = true;
        parent[i] = temp;
        range[i] = min(range[i], range[temp] + 1);
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

const int INT_INF = 1e9;
const long long LONG_INF = 1e18;
const long long MOD = 1e9 + 7;
const double PI = 3.14159265358979323846;

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> range(n + 1, INT_INF);
  vector<vector<bool>> rinsetu(n + 1, vector<bool>(n + 1, false));
  range[1] = 0;
  int a, b;
  vector<queue<int>> rin(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    rinsetu[a][b] = true;
    rinsetu[b][a] = true;
    rin[a].push(b);
    rin[b].push(a);
  }

  queue<int> q;
  vector<int> parent(n + 1);
  vector<bool> used(n + 1, false);
  used[1] = true;
  q.push(1);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    while (!rin[temp].empty()) {
      int i = rin[temp].front();
      rin[temp].pop();
      if (used[i] == false) {
        q.push(i);
        used[i] = true;
        parent[i] = temp;
        range[i] = min(range[i], range[temp] + 1);
      }
    }

    /*
    for (int i = 1; i <= n; i++)
    {
        if (rinsetu[temp][i])
        {
            if (used[i] == false)
            {
                q.push(i);
                used[i] = true;
                parent[i] = temp;
                range[i] = min(range[i], range[temp] + 1);
            }
        }
    }
    */
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using itn = int;

int main(void) {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];

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

  vector<bool> v(n + 1, false);
  vector<int> ans(n + 1);
  queue<int> que;
  que.push(0);

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

    for (auto u : G[p]) {
      if (v[u] == true)
        continue;
      que.push(u);
      ans[u] = p;
      v[u] = true;
    }
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using itn = int;

int main(void) {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];

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

  vector<bool> v(n + 1, false);
  vector<int> ans(n + 1, -1);
  int cnt = 1;

  for (int i = 0; i < n; i++) {
    if (cnt == n)
      break;
    for (auto u : G[i]) {
      if (ans[u] == -1) {
        ans[u] = i + 1;
        cnt++;
      }
    }
  }

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

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;

// 最短経路問題はBFSが最適！
int main() {
  // 頂点数と辺数
  int n, m, a, b;
  cin >> n >> m;

  // グラフ入力受取 (ここでは無向グラフを想定)
  graph g(n + 1);
  REP(i, m) {
    cin >> a >> b;
    g[a].push_back(b); // aから辿れる頂点の配列
    g[b].push_back(a); // bから辿れる頂点の配列
  }

  // BFSのためのデータ構造
  vector<int> dist(n + 1, -1); // 始点からの距離。全頂点を「未訪問」に初期化。
  vector<int> pre(n + 1, -1); // 始点に向かう一つ先の点。
  queue<int> que; // 始点に近い順に訪問していくのでqueue。

  // 初期条件 (頂点1を初期ノードとする)
  dist[1] = 0;
  que.push(1);

  // BFS開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    // キューから先頭頂点を取り出す
    int v = que.front();
    que.pop();
    // vから辿れる頂点をすべて調べる
    for (int nv : g[v]) {
      if (dist[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない
      // 新たな頂点nvについて距離情報を更新してキューに追加する
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }

  cout << "Yes" << endl;
  FOR(i, 2, n) cout << pre[i] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

struct edge {
  int from; // 出発点
  int to;   // 到達点
  int cost; // 移動コスト
};

int main() {
  int n;              // 頂点の数
  int m;              // 辺の数
  int d[n + 1];       // その部屋に1から行くのにかかるコスト
  int prev[n + 1];    // 先の部屋の名前
  vector<edge> edges; // 移動の情報を保存する
  cin >> n >> m;

  fill(d, d + n, inf); // すべての頂点をINFにする
  d[1] = 0;            // 始点を0にする

  REP(i, m) {
    struct edge add;
    cin >> add.from;
    cin >> add.to;
    add.cost = 1;
    edges.push_back(add);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int)edges.size(); j++) {
      struct edge e = edges[j];

      if (d[e.to] >
          d[e.from] +
              e.cost) { // 移動した後のコストが小さいと、頂点のコストを更新
        d[e.to] = d[e.from] + e.cost;
        prev[e.to] = e.from;
        if (i == n - 1) {
          break;
        }
      }
    }
  }
  FOR(i, 2, n) {
    if (prev[i] == inf) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  FOR(i, 2, n) { cout << prev[i] << endl; }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
// #define MULTI_TEST

int n, m;
vector<int> p;
vector<vector<int>> to;
vector<int> ans;
vector<int> a;

void path(int par, int r, int d) {
  if (p[r])
    return;
  p[r] = 1;
  if (a[r] >= d) {
    a[r] = d;
    ans[r] = par;
  }
  for (int i : to[r]) {
    path(r, i, d + 1);
  }
  p[r] = 0;
}

void solve() {
  p.clear();
  ans.clear();
  to.clear();
  a.clear();
  cin >> n >> m;

  p.assign(n + 1, 0);
  to.assign(n + 1, vector<int>());
  ans.assign(n + 1, -1);
  a.assign(n + 1, INT_MAX);

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

  /*path(0, 1, 0);

  int poss = 1;
  for(int i=2; i<=n; i++) {
          if(ans[i] == -1) {
                  poss = 0;
                  break;
          }
  }*/

  queue<pair<int, int>> q;
  q.push(make_pair(0, 1));

  while (q.size()) {
    int i = q.front().second;
    int par = q.front().first;
    q.pop();
    if (p[i])
      continue;

    p[i] = 1;
    ans[i] = par;

    for (int t : to[i]) {
      q.push(make_pair(i, t));
    }
  }

  int poss = 1;
  for (int i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      poss = 0;
      break;
    }
  }

  if (poss) {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << "\\n";
    }
  } else {
    cout << "No\\n";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

#ifdef MULTI_TEST
  int t;
  cin >> t;
  while (t--) {
    solve();
    // cout << "\\n";
  }
#else
  solve();
// 	cout << "\\n";
#endif

  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
// #define MULTI_TEST

int n, m;
vector<int> p;
vector<vector<int>> to;
vector<int> ans;
vector<int> a;

void path(int par, int r, int d) {
  if (p[r])
    return;
  p[r] = 1;
  if (a[r] >= d) {
    a[r] = d;
    ans[r] = par;
  }
  for (int i : to[r]) {
    path(r, i, d + 1);
  }
  p[r] = 0;
}

void solve() {
  p.clear();
  ans.clear();
  to.clear();
  a.clear();
  cin >> n >> m;

  p.assign(n + 1, 0);
  to.assign(n + 1, vector<int>());
  ans.assign(n + 1, -1);
  a.assign(n + 1, INT_MAX);

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

  path(0, 1, 0);

  int poss = 1;
  for (int i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      poss = 0;
      break;
    }
  }

  if (poss) {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << "\\n";
    }
  } else {
    cout << "No\\n";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

#ifdef MULTI_TEST
  int t;
  cin >> t;
  while (t--) {
    solve();
    // cout << "\\n";
  }
#else
  solve();
// 	cout << "\\n";
#endif

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

#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int ans[100005], vis[100005];
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    for (int i = 0; i < v[temp].size(); i++) {
      if (0 == ans[v[temp][i]])
        ans[v[temp][i]] = temp;
      if (vis[v[temp][i]] == 0) {
        vis[v[temp][i]] = 1;
        q.push(v[temp][i]);
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  bfs();
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int dis[100005], ans[100005];
void dfs(int pos, int flag) {
  for (int i = 0; i < v[pos].size(); i++) {
    if (dis[v[pos][i]] == 0) {
      ans[v[pos][i]] = pos;
      dis[v[pos][i]] = flag;
    } else {
      if (dis[v[pos][i]] > flag) {
        ans[v[pos][i]] = pos;
        dis[v[pos][i]] = flag;
      }
    }
  }
  for (int i = 0; i < v[pos].size(); i++) {
    if (dis[v[pos][i]] >= flag) {
      dfs(v[pos][i], flag + 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, 1);
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}