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

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

using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> ans(n + 1, -1);
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  while (q.empty() != true) {
    int v = q.front();
    q.pop();
    for (int x : G[v]) {
      if (ans[x] == -1) {
        q.push(x);
        ans[x] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> G(m + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> ans(n + 1, -1);
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  while (q.empty() != true) {
    int v = q.front();
    q.pop();
    for (int x : G[v]) {
      if (ans[x] == -1) {
        q.push(x);
        ans[x] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
const int INF = 1001001001;
vector<int> to[100005]; // 行き先

int main(void) {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A;
    --B; // 部屋番号を1ずらす
    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]) { // 部屋vにつながる部屋をすべて調べる
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }

  // 道しるべ記入
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;
const int INF = 1001001001;
vector<int> to[10005]; // 行き先

int main(void) {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A;
    --B; // 部屋番号を1ずらす
    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]) { // 部屋vにつながる部屋をすべて調べる
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }

  // 道しるべ記入
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  int A, B;
  map<int, vector<int>> adj_map;

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

  for (int i = 0; i < N; i++) {
    vector<int> adj_rooms = adj_map[i];
    for (auto itr = adj_rooms.begin(); itr != adj_rooms.end(); itr++) {
    }
  }

  vector<int> direction(N);
  direction[0] = -1;
  for (int i = 1; i < N; i++) {
    direction[i] = 0;
  }

  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int curr_room = que.front();
    vector<int> adj_rooms = adj_map[curr_room];
    for (auto itr = adj_rooms.begin(); itr != adj_rooms.end(); itr++) {
      int room = *itr;
      if (direction[room] == 0) {
        direction[room] = curr_room + 1;
        que.push(room);
      }
    }
    que.pop();
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void solve(map<int, set<int>> adj_rooms, vector<bool> checked,
           vector<int> &direction, int cur_room) {
  set<int> adj_r = adj_rooms[cur_room];

  bool end_loop = true;

  for (auto itr = adj_r.begin(); itr != adj_r.end(); ++itr) {
    int selected_room = *itr;
    if (!checked[selected_room]) {
      end_loop = false;
      checked[selected_room] = true;
      direction[selected_room] = cur_room;
    }
  }

  if (end_loop)
    return;

  for (auto itr = adj_r.begin(); itr != adj_r.end(); ++itr) {
    if (*itr != cur_room) {
      solve(adj_rooms, checked, direction, *itr);
    }
  }
}

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

  int A, B;
  map<int, set<int>> adj_rooms;

  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    adj_rooms[A - 1].insert(B - 1);
    adj_rooms[B - 1].insert(A - 1);
  }

  vector<bool> checked(N);
  for (int i = 0; i < N; i++) {
    checked[i] = false;
  }

  vector<int> direction(N);

  checked[0] = true;
  solve(adj_rooms, checked, direction, 0);

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

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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取 (ここでは無向グラフを想定)
  Graph G(N + 1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // BFS のためのデータ構造
  vector<int> parent(N + 1, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

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

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      if (parent[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      parent[nv] = v;
      que.push(nv);
    }
  }
  bool finish = true;
  for (int v = 2; v < N + 1; ++v) {
    if (parent[v] == 0) {
      cout << "No";
      finish = false;
      break;
    }
  }
  if (finish) {
    cout << "Yes" << endl;

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 2; v < N + 1; ++v)
      cout << parent[v] << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
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;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // BFS のためのデータ構造
  vector<int> parent(N + 1, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

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

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      if (parent[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      parent[nv] = v;
      que.push(nv);
    }
  }

  // 結果出力 (各頂点の頂点 0 からの距離を見る)
  for (int v = 2; v < N + 1; ++v)
    cout << parent[v] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool visited[200000];
int n, m;
int mark[200000];
vector<vector<int>> adj;

int main() {
  scanf("%d %d", &n, &m);
  adj = vector<vector<int>>(n + 5);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  queue<int> q;
  q.push(1);
  memset(visited, 0, sizeof(visited));
  memset(mark, 0, sizeof(mark));
  mark[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int to : adj[v]) {
      if (!mark[to]) {
        mark[to] = v;
        q.push(to);
      }
    }
  }

  int c = count(mark + 1, mark + n + 1, 0);
  if (c)
    printf("No\\n");
  else {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", mark[i]);
    }
  }
}

Error: TLE

Fixed submission:

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

bool visited[200000];
int n, m;
int mark[200000];
vector<vector<int>> adj;

int main() {
  scanf("%d %d", &n, &m);
  adj = vector<vector<int>>(n + 5);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  queue<int> q;
  q.push(1);
  memset(visited, 0, sizeof(visited));
  memset(mark, 0, sizeof(mark));

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    visited[v] = true;
    for (int to : adj[v]) {
      if (visited[to])
        continue;
      if (!mark[to]) {
        mark[to] = v;
      }
      q.push(to);
    }
  }

  int c = count(visited + 1, visited + n + 1, false);
  if (c)
    printf("No\\n");
  else {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", mark[i]);
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define F first
#define S second
#define INF 1 << 28
using namespace std;

int n, m;
typedef pair<int, int> P;
vector<P> g[222222];
int ans[111111];
int ans2[111111];

void solve(int a) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(ans, ans + n + 1, INF);

  ans[a] = 0;
  que.push(P(0, a));

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    if (ans[p.S] < p.F)
      continue;
    for (long long int i = 0; i < g[p.S].size(); i++) {
      P pp = g[p.S][i];
      if (ans[pp.F] > ans[p.S] + pp.S) {
        ans[pp.F] = ans[p.S] + pp.S;
        ans2[pp.F] = p.S;
        que.push(P(ans[pp.F], pp.F));
      }
    }
  }
}

int main() {

  cin >> n >> m;

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

  solve(1);

  bool ok = true;
  for (int i = 2; i <= n; i++)
    if (ans[i] >= INF)
      ok = false;

  if (!ok)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << ans2[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
#define INF 1 << 28
using namespace std;

int n, m;
typedef pair<int, int> P;
vector<P> g[222222];
int ans[111111];
int ans2[111111];

int solve(int a) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(ans, ans + n + 1, INF);

  ans[a] = 0;
  que.push(P(0, a));

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    if (ans[p.S] < p.F)
      continue;
    for (int i = 0; i < g[p.S].size(); i++) {
      P pp = g[p.S][i];
      if (ans[pp.F] > ans[p.S] + pp.S) {
        ans[pp.F] = ans[p.S] + pp.S;
        ans2[pp.F] = p.S;
        que.push(P(ans[pp.F], pp.F));
      }
    }
  }
}

int main() {

  cin >> n >> m;

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

  solve(1);

  bool ok = true;
  for (int i = 2; i <= n; i++)
    if (ans[i] >= INF)
      ok = false;

  if (!ok)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << ans2[i] << endl;
  }
}
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 < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
// #define _GLIBCXX_DEBUG

signed main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> p(N + 1, vector<int>(0));
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    p[a].push_back(b);
    p[b].push_back(a);
  }

  vector<int> ans(N + 1, 0);
  ans[0] = 1;
  ans[1] = 1;

  queue<int> q;
  q.push(1);

  int k = 0;
  while (!q.empty()) {
    int n = q.front();
    q.pop();
    rep(i, p[n].size()) {
      int a = p[n][i];
      if (ans[a] == 0) {
        ans[a] = n;
        k++;
        q.push(a);
      } else {
        p[n].erase(p[n].begin() + i);
        i--;
      }
    }
    if (k == N - 1) {
      cout << "Yes" << endl;
      rep(i, N + 1) if (i >= 2) cout << ans[i] << endl;
      break;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
// #define _GLIBCXX_DEBUG

signed main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> p(N + 1, vector<int>(0));
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    p[a].push_back(b);
    p[b].push_back(a);
  }

  vector<int> ans(N + 1, 0);
  ans[0] = 1;

  queue<int> q;
  q.push(1);

  while (!q.empty()) {
    int n = q.front();
    q.pop();
    rep(i, p[n].size()) {
      int a = p[n][i];
      if (ans[a] == 0) {
        ans[a] = n;
        q.push(a);
      } else {
        p[n].erase(p[n].begin() + i);
        i--;
      }
    }
    if (count(all(ans), 0) == 0) {
      cout << "Yes" << endl;
      rep(i, N + 1) if (i >= 2) cout << ans[i] << endl;
      break;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
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--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(N, -1);
  vector<int> prev(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      prev[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int v = 1; v < N; ++v)
    cout << prev[v] + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
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;
}
using Graph = vector<vector<int>>;

vector<vector<int>> seen;
void dfs(const Graph &G, int v, int p, int count) {
  if (seen[v][1] > count || seen[v][0] == 0) {
    seen[v][0] = p;
    seen[v][1] = count;
  } else
    return;
  for (auto next_v : G[v]) {
    dfs(G, next_v, v, count + 1);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  Graph G(N + 1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  seen.assign(N + 1, vector<int>(2, 0));
  dfs(G, 1, 0, 0);
  for (int i = 2; i < N + 1; ++i) {
    if (seen[i][0] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; ++i) {
    cout << seen[i][0] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define M0D 998244353;
vector<int> G[100009];
int ans[100009];
queue<int> test;

void answww(int num, int sup) {
  test.pop();
  for (int j = 0; j < G[num].size(); j++) {
    if (ans[G[num][j]] == 0) {
      test.push(G[num][j]);
      ans[G[num][j]] = num;
    }
  }
  if (test.size() > 0)
    answww(test.front(), sup);
}

int main() {
  int N, M, sup = 1;
  cin >> N >> M;
  int A[M + 1], B[M + 1];
  for (int i = 0; i < N + 1; i++)
    ans[i] = 0;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i];
  for (int i = 1; i <= M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  test.push(1);
  ans[1] = 1;
  answww(test.front(), sup);
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M0D 998244353;
vector<int> G[100009];
int ans[100009];
queue<int> test;

void answww(int num, int sup) {
  test.pop();
  for (int j = 0; j < G[num].size(); j++) {
    if (ans[G[num][j]] == 0) {
      test.push(G[num][j]);
      ans[G[num][j]] = num;
    }
  }
  answww(test.front(), sup);
}

int main() {
  int N, M, sup = 1;
  cin >> N >> M;
  int A[M + 1], B[M + 1];
  for (int i = 0; i < N + 1; i++)
    ans[i] = 0;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i];
  for (int i = 1; i <= M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  test.push(1);
  ans[1] = 1;
  answww(test.front(), sup);
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
#define INF 1001001001
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repX(i, n, x) for (int i = x; i < (int)(n); ++i)
#define repBack(i, n) for (int i = n; i >= 0; --i)
#define dup(x, y) (((x) + (y)-1) / (y))
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;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  vector<int> P(N, INF);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue;

      dist[nv] = dist[v] + 1;
      P[nv] = v;
      que.push(nv);
    }
  }
  repX(i, N, 1) {
    if (P[i] == INF) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  repX(i, N, 1) { cout << P[i] + 1 << endl; }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
#define INF 1001001001
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repX(i, n, x) for (int i = x; i < (int)(n); ++i)
#define repBack(i, n) for (int i = n; i >= 0; --i)
#define dup(x, y) (((x) + (y)-1) / (y))
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;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  vector<int> P(N);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue;

      dist[nv] = dist[v] + 1;
      P[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  rep(i, N) { cout << P[i] << endl; }

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



#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define SortAsc(c) std::sort(c.begin(), c.end())
#define SortDesc(c) std::sort(c.rbegin(), c.rend())
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

void bfs(vector<vector<int>> &g, vector<int> &r, vector<int> &d) {
  r.resize(g.size());
  r.assign(g.size(), -1);
  d.resize(g.size());
  d.assign(g.size(), 0);

  queue<int> q;
  q.push(0);
  r.at(0) = 0;

  while (!q.empty()) {
    int c = q.front();
    q.pop();

    for (int next : g.at(c)) {
      // cout << c + 1 << " -> " << next + 1 << "候補" << endl;
      // 道標を置く
      if (r.at(next) < 0) {
        // cout << c + 1 << " -> " << next + 1 << endl;
        r.at(next) = c;
        q.push(next);
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

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

  vector<int> r;
  vector<int> d;
  bfs(g, r, d);

  bool allFound = true;
  for (int i = 1; i < n; i++) {
    if (r.at(i) < 0) {
      allFound = false;
    }
  }

  if (allFound) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
    return 0;
  }

  for (int i = 1; i < n; i++) {
    cout << r.at(i) + 1 << endl;
  }
}

Error: 0

Fixed submission:



#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define SortAsc(c) std::sort(c.begin(), c.end())
#define SortDesc(c) std::sort(c.rbegin(), c.rend())
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

void bfs(vector<vector<int>> &g, vector<int> &r) {
  r.resize(g.size());
  r.assign(g.size(), -1);
  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int c = q.front();
    q.pop();

    for (int i = 0; i < g.at(c).size(); i++) {
      int next = g.at(c).at(i);
      // 道標を置く
      if (r.at(next) < 0) {
        r.at(next) = c;
        q.push(next);
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

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

  vector<int> r;
  bfs(g, r);

  bool allFound = true;
  for (int i = 1; i < n; i++) {
    if (r.at(i) < 0) {
      allFound = false;
    }
  }

  if (allFound) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
    return 0;
  }

  for (int i = 1; i < n; i++) {
    cout << r.at(i) + 1 << endl;
  }
}