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

#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define nl cout << "\\n"
#define pb push_back
#define S second
#define F first
#define pi = 3.14159265358979323846264338327950L;
typedef long long int ll;
vector<int> graph[200010];
vector<bool> vis(200010, false);
vector<int> dis(200010, -1);

int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //~ freopen("input.txt", "r", stdin);
  //~ freopen("output.txt", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  queue<pair<int, int>> q;
  q.push({1, 0});
  vis[1] = true;
  while (!q.empty()) {
    int v = q.front().first;
    int d = q.front().second;
    q.pop();
    dis[v] = d;
    for (int i : graph[v]) {
      if (!vis[i]) {
        vis[i] = true;
        q.push({i, v});
      }
    }
  }
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    if (dis[i] == -1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << dis[i] << endl;
    }
  } else
    cout << "No" << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define nl cout << "\\n"
#define pb push_back
#define S second
#define F first
#define pi = 3.14159265358979323846264338327950L;
typedef long long int ll;
vector<int> graph[200005];
vector<bool> vis(200005, false);
vector<int> dis(20005, -1);

int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //~ freopen("input.txt", "r", stdin);
  //~ freopen("output.txt", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  queue<pair<int, int>> q;
  q.push({1, 0});
  while (!q.empty()) {
    int v = q.front().first;
    int d = q.front().second;
    q.pop();
    vis[v] = true;
    dis[v] = d;
    for (int i : graph[v]) {
      if (!vis[i]) {
        q.push({i, v});
      }
    }
  }
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    if (dis[i] == -1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << dis[i] << endl;
    }
  } else
    cout << "No" << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF 1e+9
#define MAX_V 200002

struct edge {
  int to;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int mark[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + 1) {
        d[e.to] = d[v] + 1;
        mark[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge ea = {a};
    edge eb = {b};
    G[a].push_back(eb);
    G[b].push_back(ea);
  }
  dijkstra(0);
  rep(i, V) {
    if (d[i] == INF) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < V; i++) {
    cout << mark[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF 1e+9
#define MAX_V 20002

struct edge {
  int to;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int mark[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + 1) {
        d[e.to] = d[v] + 1;
        mark[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge ea = {a};
    edge eb = {b};
    G[a].push_back(eb);
    G[b].push_back(ea);
  }
  dijkstra(0);
  rep(i, V) {
    if (d[i] == INF) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < V; i++) {
    cout << mark[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  cin >> n >> m;
  int flag[n], ans[n];
  rep(i, n) flag[i] = 0;
  int a[m], b[m];
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  vector<vector<int>> nd(n);

  rep(i, m) {
    nd[a[i]].push_back(b[i]);
    nd[b[i]].push_back(a[i]);
  }

  queue<int> que;
  que.push(0);
  flag[0] = 1;
  while (que.empty() == 0) {
    int now = que.front();
    que.pop();
    rep(j, (int)nd[now].size()) {
      if (flag[nd[now][j]] == 0) {
        que.push(nd[now][j]);
        ans[nd[now][j]] = now + 1;
        flag[nd[now][j]] = 1;
      }
    }
  }

  cout << "Yes\\n";
  rep(k, n - 1) { cout << ans[k + 1] << endl; }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  int flag[n], ans[n];
  rep(i, n) {
    flag[i] = 0;
    ans[i] = -1;
  }
  int a[m], b[m];
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int d[n][1000000];
  int dd[n];
  rep(i, n) dd[i] = 0;
  rep(i, m) {
    d[a[i]][dd[a[i]]] = b[i];
    d[b[i]][dd[b[i]]] = a[i];
    dd[a[i]]++;
    dd[b[i]]++;
  }
  queue<int> que;
  que.push(0);
  flag[0] = 1;
  while (que.empty() == 0) {
    int now = que.front();
    que.pop();
    rep(j, dd[now]) {
      if (flag[d[now][j]] == 0) {
        que.push(d[now][j]);
        ans[d[now][j]] = now + 1;
        flag[d[now][j]] = 1;
      }
    }
  }
  rep(i, n) {
    if (ans[i] == -1) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  rep(k, n - 1) { cout << ans[k + 1] << endl; }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

const string YES = "Yes";
const string NO = "No";

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<vector<long long>> lis(N + 1);
  vector<long long> path(N + 1, LLONG_MAX);
  for (long long i = 0; i < M; i++) {
    lis[A[i]].push_back(B[i]);
    lis[B[i]].push_back(A[i]);
  }

  queue<long long> q;
  q.push(1);
  path[0] = 0;
  path[1] = 0;

  while (!q.empty()) {
    long long edge = q.front();
    // cout << edge << ": ";
    for (auto i : lis[edge]) {
      // cout << i << " ";
      if (path[i] == LLONG_MAX) {
        path[i] = edge;
        q.push(i);
      }
    }
    // cout << endl;
    q.pop();
  }

  if (find(path.begin(), path.end(), LLONG_MAX) == path.end()) {
    cout << YES << endl;
    for (long long i = 2; i <= N; i++) {
      cout << path[i] << endl;
    }
  } else {
    cout << NO << endl;
  }
}

// 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)
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;

const string YES = "Yes";
const string NO = "No";

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<vector<long long>> lis(N + 1);
  vector<long long> path(N + 1, LLONG_MAX);
  for (long long i = 0; i < N; i++) {
    lis[A[i]].push_back(B[i]);
    lis[B[i]].push_back(A[i]);
  }

  queue<long long> q;
  q.push(1);
  path[0] = 0;
  path[1] = 0;

  while (!q.empty()) {
    long long edge = q.front();
    // cout << edge << ": ";
    for (auto i : lis[edge]) {
      // cout << i << " ";
      if (path[i] == LLONG_MAX) {
        path[i] = edge;
        q.push(i);
      }
    }
    // cout << endl;
    q.pop();
  }

  if (find(path.begin(), path.end(), LLONG_MAX) == path.end()) {
    cout << YES << endl;
    for (long long i = 2; i <= N; i++) {
      cout << path[i] << endl;
    }
  } else {
    cout << NO << endl;
  }
}

// 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)
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;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> adj(N, vector<int>());
  // cout << "size of adj : " << adj.size() << endl;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    // cout << A-1 << B-1 << endl;
    adj.at(A - 1).push_back(B - 1);
    adj.at(B - 1).push_back(A - 1);
  }
  // cout << "Initialization ends." << endl;

  queue<int> bfs;
  vector<bool> ch(N, false);
  vector<int> ans(N, 0);
  ch.at(0) = true;
  bfs.push(0);
  while (!bfs.empty()) {
    int search = bfs.front();
    bfs.pop();
    for (auto x : adj.at(search)) {
      if (!ch.at(x)) {
        ch.at(x) = true;
        ans.at(x) = search;
        bfs.push(x);
      }
    }
  }

  for (auto &x : ans)
    x++;
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans.at(i) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> adj(M, vector<int>());
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    adj.at(A - 1).push_back(B - 1);
    adj.at(B - 1).push_back(A - 1);
  }

  queue<int> bfs;
  vector<bool> ch(N, false);
  vector<int> ans(N, 0);
  ch.at(0) = true;
  bfs.push(0);
  while (!bfs.empty()) {
    int search = bfs.front();
    bfs.pop();
    for (auto x : adj.at(search)) {
      if (!ch.at(x)) {
        ch.at(x) = true;
        ans.at(x) = search;
        bfs.push(x);
      }
    }
  }

  for (auto &x : ans)
    x++;
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << ans.at(i) << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> t(n, -1);
  queue<int> q;
  vector<int> pre(n);
  pre[0] = -1;
  q.push(0);
  t[0] = 1;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int u : x[p]) {
      if (t[u] == -1) {
        q.push(u);
        pre[u] = p;
        t[u] = 1;
      }
    }
  }
  for (i = 1; i < n; i++) {
    if (t[i] == -1) {
      break;
    }
  }
  if (i != n) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int j = 1; j < n; j++) {
      cout << pre[j] + 1 << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> t(n, 0);
  t[0] = 1;
  queue<int> q;
  vector<int> ans(n);
  vector<int> pre(n);
  ans[0] = 0;
  pre[0] = 0;
  int u = x[0].size();
  for (i = 0; i < u; i++) {
    pre[x[0][i]] = 0;
  }
  q.push(0);
  while (q.empty() == 0) {
    int p = q.front();
    q.pop();
    t[p] = 1;
    ans[p] = pre[p];
    int r = x[p].size();
    for (i = 0; i < r; i++) {
      q.push(x[p][i]);
      pre[x[p][i]] = p;
    }
  }
  cout << "Yes" << endl;
  for (i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> a[100000];

int main() {
  int n, m;
  cin >> n >> m;
  int l, r;
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    a[l].push_back(r);
    a[r].push_back(l);
  }
  vector<int> res(n + 1, 0);
  queue<int> p;
  p.push(1);
  while (!p.empty()) {
    int cur = p.front();
    p.pop();
    for (int x : a[cur]) {
      if (res[x] == 0) {
        res[x] = cur;
        p.push(x);
      }
    }
  }
  for (int i = 2; i < n + 1; i++) {
    if (res[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++) {
    cout << res[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m, vector<int>(2));
  for (int i = 0; i < m; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  vector<int> res(n + 1, 0);
  queue<int> p;
  p.push(1);
  while (!p.empty()) {
    int cur = p.front();
    p.pop();
    for (int i = 0; i < m; i++) {
      if (a[i][0] == cur && res[a[i][1]] == 0) {
        res[a[i][1]] = a[i][0];
        p.push(a[i][1]);
      } else if (a[i][1] == cur && res[a[i][0]] == 0) {
        res[a[i][0]] = a[i][1];
        p.push(a[i][0]);
      }
    }
  }
  for (int i = 2; i < n + 1; i++) {
    if (res[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++) {
    cout << res[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define MAX 100001
#define ll long long

queue<int> next_pos; // first どこにいくか　second どこからきたか
vector<int> root[MAX];
int dist[MAX];
int ans[MAX];

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

  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    root[A - 1].push_back(B - 1);
    root[B - 1].push_back(A - 1);
  }

  int pos = 0;
  next_pos.push(pos);
  for (int i = 0; i < MAX; ++i) {
    ans[i] = -1;
  }
  while (!next_pos.empty()) {
    pos = next_pos.front();
    next_pos.pop();
    for (auto itr = root[pos].begin(); itr != root[pos].end(); ++itr) {
      if (ans[*itr] == -1) {
        ans[*itr] = pos;
        next_pos.push(*itr);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define MAX 100001
#define ll long long
#ifndef M_PI
#define M_PI 3.14159265358979
#endif

#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

queue<pair<int, int>> next_pos; // first どこにいくか　second どこからきたか
bool footprint[MAX];
pair<int, int> root[MAX * 2]; // first　入口　seconde 出口
int ans[MAX];
int index[MAX * 2];
int isfinish(int N) {
  for (int i = 1; i < N + 1; ++i) {
    if (footprint[i] == false)
      return 0;
  }
  return 1;
}

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

  int N, M;
  cin >> N >> M;
  root[0].first = 0;
  root[0].second = 0;
  for (int i = 1; i < M + 1; ++i) {
    cin >> root[i].first >> root[i].second;
  }
  for (int i = 0; i < MAX; ++i) {
    footprint[i] = false;
    ans[i] = 0;
  }

  pair<int, int> pos(1, 1);
  next_pos.push(pos);
  while (true) {
    pos = next_pos.front();
    next_pos.pop();

    for (int i = 1; i < M + 1; ++i) {
      if (root[i].first == pos.first && footprint[root[i].second] == false) {

        pair<int, int> temp(root[i].second, pos.first);
        next_pos.push(temp);
        ans[root[i].second] = pos.first;
        footprint[root[i].second] = true;
      } else if (root[i].second == pos.first &&
                 footprint[root[i].first] == false) {
        pair<int, int> temp(root[i].first, pos.first);
        next_pos.push(temp);
        ans[root[i].first] = pos.first;
        footprint[root[i].first] = true;
      }
    }
    if (isfinish(N)) {
      cout << "Yes" << '\\n';
      for (int i = 2; i < N + 1; ++i) {
        cout << ans[i] << '\\n';
      }
      break;
    }
    if (next_pos.empty()) {
      cout << "No";
      break;
    }
  }

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

#include <algorithm>
#include <cmath>   //abs()かfabs()で少数絶対値
#include <cstdlib> //abs()で整数絶対値
#include <deque>
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using Graph = vector<vector<int>>;

int main() {
  int N, M;
  cin >> N >> M;
  Graph G(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a != 0 && b != 0) {
      G[a].push_back(b);
      G[b].push_back(a);
    } else if (a == 0) {
      G[a].push_back(b);
    } else {
      G[b].push_back(a);
    }
  }
  vector<int> mark(N);
  vector<int> dist(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);

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

    for (int next_v : G[v]) {
      if (dist[next_v] != -1)
        continue;
      mark[next_v] = v + 1;
      dist[next_v] = dist[v] + 1;
      que.push(next_v);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << mark[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>   //abs()かfabs()で少数絶対値
#include <cstdlib> //abs()で整数絶対値
#include <deque>
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using Graph = vector<vector<int>>;

vector<int> dis;
vector<int> mark;
void dfs(const Graph &G, int v) {
  for (auto next_v : G[v]) {
    /*        for(int i=0;i<4;i++){
                cout << dis[i] << " ";
            }
            cout << endl;*/
    if (dis[next_v] <= dis[v] && dis[next_v] != 0)
      continue;
    dis[next_v] = dis[v] + 1;
    mark[next_v] = v + 1;
    dfs(G, next_v);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  Graph G(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a != 0 && b != 0) {
      G[a].push_back(b);
      G[b].push_back(a);
    } else if (a == 0) {
      G[a].push_back(b);
    } else {
      G[b].push_back(a);
    }
  }
  dis.assign(N, 0);
  mark.assign(N, 0);
  dfs(G, 0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << mark[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define mod 1e9 + 7
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \\
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

const int N = 1e5 + 5;
int n, m;
vector<int> v[N];
int vis[N], ans[N];

void bfs(int src) {
  queue<int> q;
  q.push(src);
  while (sz(q)) {
    int t = q.front();
    q.pop();
    vis[t] = 1;
    for (auto i : v[t]) {
      if (vis[i] == 0) {
        q.push(i);
        if (ans[i] == -1)
          ans[i] = t;
        vis[i] = 1;
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  forn(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  memset(ans, -1, sizeof(ans));
  bfs(0);
  if (count(ans + 1, ans + n, -1))
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    forn(i, 1, n) cout << ans[i] + 1 << endl;
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define mod 1e9 + 7
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \\
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

const int N = 1e5 + 5;
int n, m;
vector<int> v[N];
int vis[N], ans[N];

void bfs(int src) {
  queue<int> q;
  q.push(src);
  while (sz(q)) {
    int t = q.front();
    // cout<<t<<endl;
    q.pop();
    vis[t] = 1;
    for (auto i : v[t]) {
      if (vis[i] == 0) {
        q.push(i);
        if (ans[i] == -1)
          ans[i] = t;
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  forn(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  memset(ans, -1, sizeof(ans));
  bfs(0);
  if (count(ans + 1, ans + n, -1))
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    forn(i, 1, n) cout << ans[i] + 1 << endl;
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}