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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a) * (a)
#define nl "\\n"
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;
vector<vi> adj;

const double pi = 3.14159265359;
const int MAXN = 100005;
ll mod = 1000000007;
vi id;
vector<bool> vis;
void bfs(int parent) {
  queue<int> q;
  vis[parent] = true;
  q.push(parent);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto y : adj[x]) {
      if (!vis[y]) {
        q.push(y);
        vis[y] = true;
        id[y] = x;
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  vis.resize(n + 1, false);
  id.resize(n + 1, -1);
  id[1] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bfs(1);
  for (int i = 2; i < n; i++) {
    if (id[i] == -1) {
      cout << "No" << nl;
      return 0;
    }
  }
  cout << "Yes" << nl;
  for (int i = 2; i <= n; i++) {
    cout << id[i] << nl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a) * (a)
#define nl "\\n"
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;
vector<vi> adj;

const double pi = 3.14159265359;
const int MAXN = 100005;
ll mod = 1000000007;
vi id, vis;
int dfs(int parent) {
  vis[parent] = true;
  for (auto child : adj[parent]) {
    if (!vis[child]) {
      id[child] = parent;
      dfs(child);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  vis.resize(n + 1);
  id.resize(n + 1, -1);
  id[1] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1);
  for (int i = 2; i < n; i++) {
    if (id[i] == -1) {
      cout << "No" << nl;
      return 0;
    }
  }
  cout << "Yes" << nl;
  for (int i = 2; i <= n; i++) {
    cout << id[i] << nl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    --a;
    --b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }

  vector<bool> visit(N, false);
  queue<int> q;
  unordered_map<int, int> route;

  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();

    for (int nv : G.at(v)) {
      if (!visit.at(nv)) {
        q.push(nv);
        visit.at(nv) = true;
        route.insert(make_pair(nv, v));
      }
    }
  }

  if (!all_of(visit.begin(), visit.end(), [](bool f) { return f; })) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  REPI(i, 1, N) {
    // cout << i << ":" << route.at(i).size() << endl;
    cout << route.at(i) + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(M);
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    --a;
    --b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }

  vector<bool> visit(N, false);
  queue<int> q;
  unordered_map<int, int> route;

  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();

    for (int nv : G.at(v)) {
      if (!visit.at(nv)) {
        q.push(nv);
        visit.at(nv) = true;
        route.insert(make_pair(nv, v));
      }
    }
  }

  if (!all_of(visit.begin(), visit.end(), [](bool f) { return f; })) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  REPI(i, 1, N) {
    // cout << i << ":" << route.at(i).size() << endl;
    cout << route.at(i) + 1 << endl;
  }

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

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define TRAV(i, a) for (auto &i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  int m;
  std::cin >> m;
  std::vector<VI> g(n);
  FOR(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  std::queue<int> que;
  que.push(0);
  VI prev(n, -1);
  prev[0] = 0;
  while (SZ(que)) {
    int cur = que.front();
    que.pop();
    TRAV(ch, g[cur]) {
      if (prev[ch] == -1) {
        prev[ch] = cur;
        que.push(ch);
      }
    }
  }

  std::cout << "Yes\\n";
  REP(i, 1, n) std::cout << prev[i] + 1 << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define TRAV(i, a) for (auto &i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  int m;
  std::cin >> m;
  std::vector<VI> g(n);
  FOR(i, n) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  std::queue<int> que;
  que.push(0);
  VI prev(n, -1);
  prev[0] = 0;
  while (SZ(que)) {
    int cur = que.front();
    que.pop();
    TRAV(ch, g[cur]) {
      if (prev[ch] == -1) {
        prev[ch] = cur;
        que.push(ch);
      }
    }
  }

  std::cout << "Yes\\n";
  REP(i, 1, n) std::cout << prev[i] + 1 << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const double PI = acos(-1);

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

  queue<int> q;
  vector<int> dist(n, INT_MAX);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (const auto &u : al[v]) {
      if (dist[u] != INT_MAX)
        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] + 1;
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

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

enum state { New, Working, Done };

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

  vector<state> status(n);
  vector<int> distance(n, INT_MAX);
  distance[0] = 0;
  int key = 0;
  status[0] = Working;

  while (true) {
    int minv = INT_MAX;
    int buff = key;
    for (int i = 0; i < n; i++) {
      if (status[i] == Working && distance[i] < minv) {
        minv = distance[i];
        key = i;
      }
    }

    if (minv == INT_MAX)
      break;
    status[buff] = Done;

    for (int i = 0; i < n; i++) {
      if (array[key][i] != -1 && status[i] != Done) {
        if (distance[key] + array[key][i] < distance[i]) {
          distance[i] = distance[key] + array[key][i];
          status[i] = Working;
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (distance[i] == INT_MAX)
      cout << "No" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << "Yes" << endl;
    else
      cout << distance[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> to;

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

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

  vector<bool> done(n, false);
  vector<int> ans(n, -1);

  while (!que.empty()) {
    int id = que.front();
    que.pop();
    done[id] = true;

    for (int i = 0; i < to[id].size(); i++) {
      int nx = to[id][i];
      if (done[nx])
        continue;
      if (ans[nx] == -1)
        ans[nx] = id;
      que.push(nx);
      done[nx] = true;
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> to;

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

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

  vector<bool> done(n, false);
  vector<int> ans(n, -1);

  while (!que.empty()) {
    int id = que.front();
    que.pop();
    done[id] = true;

    for (int i = 0; i < to[id].size(); i++) {
      int nx = to[id][i];
      if (done[nx])
        continue;
      if (ans[nx] == -1)
        ans[nx] = id;
      que.push(nx);
    }
  }

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

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

#define NAME "D"
#define show(x) cout << (#x) << " is " << (x) << endl
#define ll long long
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define len length()

const int maxn = 2e5;

vector<vector<int>> adj;

int main() {
  // ifstream cin(NAME".inp");
  // ofstream cout(NAME".out");
  int n, m;
  cin >> n >> m;
  adj = vector<vector<int>>(n + 1);
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  vector<int> ans(n + 1, -1);
  vector<int> done(n + 1, 0);

  queue<int> q;

  q.push(1);

  while (!q.empty()) {
    u = q.front();
    q.pop();
    if (done[u])
      continue;
    done[u] = 1;
    for (auto v : adj[u]) {
      if (!done[v]) {
        if (ans[v] == -1)
          ans[v] = u;
        q.push(v);
      }
    }
  }

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


Error: TLE

Fixed submission:

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

#define NAME "D"
#define show(x) cout << (#x) << " is " << (x) << endl
#define ll long long
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define len length()

const int maxn = 2e5;

vector<vector<int>> adj;

int main() {
  // ifstream cin(NAME".inp");
  // ofstream cout(NAME".out");
  int n, m;
  cin >> n >> m;
  adj = vector<vector<int>>(n + 1);
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  vector<int> ans(n + 1, -1);
  vector<int> done(n + 1, 0);

  queue<int> q;

  done[1] = 1;
  q.push(1);

  while (!q.empty()) {
    u = q.front();
    q.pop();
    done[u] = 1;
    for (auto v : adj[u]) {
      if (!done[v]) {
        if (ans[v] == -1)
          ans[v] = u;
        q.push(v);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << 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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> adjList(n);

  // vector<int>a(m, 0);
  REP(i, m) {
    ll first, second = 0;
    cin >> first >> second;
    adjList[first - 1].push_back(second - 1);
    adjList[second - 1].push_back(first - 1);
  }

  // vector<ll> depth(n, INF);
  vector<ll> prev(n, 0);
  vector<bool> visited(n, false);
  // depth[0] = 0;

  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll front = q.front();
    q.pop();

    for (auto i : adjList[front]) {
      if (!visited[i]) {
        q.push(i);
        prev[i] = front + 1;
        visited[i] = true;
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << prev[i] << endl; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;

  int adjList[n][n];
  REP(i, n) REP(j, n) adjList[i][j] = 0;

  // vector<int>a(m, 0);
  REP(i, m) {
    ll first, second = 0;
    cin >> first >> second;
    adjList[first - 1][second - 1] = 1;
    adjList[second - 1][first - 1] = 1;
  }

  vector<ll> depth(n, INF);
  vector<ll> prev(n, INF);
  vector<bool> visited(n, false);
  depth[0] = 0;

  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll front = q.front();
    q.pop();

    REP(i, n) {
      if (adjList[front][i] == 1) {
        if (!visited[i]) {
          q.push(i);
          // if (depth[i] > depth[front]+1) {
          //     depth[i] = depth[front]+1;
          //     prev[i] = front+1;
          // }
          prev[i] = front + 1;
          // adjList[front][i] = 0;
          // adjList[i][front] = 0;
          visited[i] = true;
        }
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << prev[i] << endl; }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<vector<int>> Graph;
Graph G(100010);
vector<int> par(100010);
int room[100010] = {};
void init(int N) { // 最初は全てが根であるとして初期化
  for (int i = 0; i < N; i++)
    par[i] = i;
}

int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  if (par[x] == x)
    return x;
  return par[x] = root(par[x]);
}

void unite(int x, int y) { // xとyの木を併合
  int rx = root(x);        // xの根をrx
  int ry = root(y);        // yの根をry
  if (rx == ry)
    return; // xとyの根が同じ(=同じ木にある)時はそのまま
  if (ry < rx) {
    par[rx] = ry;
  } else {
    par[ry] = rx;
  }
}

bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
  int rx = root(x);
  int ry = root(y);
  return rx == ry;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  init(n + 1);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 2; i <= n; i++) {
    if (!same(1, i)) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    rep(i, G[c].size()) {
      if (room[G[c][i]] == 0) {
        room[G[c][i]] = c;
        q.push(G[c][i]);
      }
    }
  }
  for (int i = 2; i <= n; i++)
    cout << room[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<vector<int>> Graph;
Graph G(100010);
vector<int> par(100010);
int room[10010] = {};
void init(int N) { // 最初は全てが根であるとして初期化
  for (int i = 0; i < N; i++)
    par[i] = i;
}

int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  if (par[x] == x)
    return x;
  return par[x] = root(par[x]);
}

void unite(int x, int y) { // xとyの木を併合
  int rx = root(x);        // xの根をrx
  int ry = root(y);        // yの根をry
  if (rx == ry)
    return; // xとyの根が同じ(=同じ木にある)時はそのまま
  if (ry < rx) {
    par[rx] = ry;
  } else {
    par[ry] = rx;
  }
}

bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
  int rx = root(x);
  int ry = root(y);
  return rx == ry;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  init(n + 1);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 2; i <= n; i++) {
    if (!same(1, i)) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    rep(i, G[c].size()) {
      if (room[G[c][i]] == 0) {
        room[G[c][i]] = c;
        q.push(G[c][i]);
      }
    }
  }
  for (int i = 2; i <= n; i++)
    cout << room[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> v[100005];
int c[100005];

int main() {
  int n, m;
  cin >> n >> m;
  queue<pair<int, int>> q;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[b].push_back(a);
    v[a].push_back(b);
  }
  for (int u : v[1])
    q.push(make_pair(u, 1));
  while (!q.empty()) {
    if (c[q.front().first] == 0) {
      c[q.front().first] = q.front().second;
      for (int u : v[q.front().first]) {
        if (u != q.front().second && c[u] == 0) {
          q.push(make_pair(u, q.front().first));
        }
      }
    }
    q.pop();
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << c[i] << endl;
}

Error: TLE

Fixed submission:

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

vector<int> v[100005];
int c[100005];

int main() {
  int n, m;
  cin >> n >> m;
  queue<pair<int, int>> q;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[b].push_back(a);
    v[a].push_back(b);
  }
  for (int u : v[1])
    q.push(make_pair(u, 1));
  while (!q.empty()) {
    if (c[q.front().first] == 0)
      c[q.front().first] = q.front().second;
    for (int u : v[q.front().first]) {
      if (u != q.front().second && c[u] == 0) {
        q.push(make_pair(u, q.front().first));
      }
    }
    q.pop();
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << c[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+9
#define MAX_V 100000

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int y[MAX_V];
int d[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;

    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        y[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    edge e = {b - 1, 1};
    G[a - 1].push_back(e);
    e = {a - 1, 1};

    G[b - 1].push_back(e);
  }
  dijkstra(0);
  cout << "Yes" << endl;

  for (int i = 1; i < V; ++i) {
    if (d[i] != INF)
      cout << y[i] + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+9
#define MAX_V 10

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int y[MAX_V];
int d[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;

    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        y[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    edge e = {b - 1, 1};
    G[a - 1].push_back(e);
    e = {a - 1, 1};

    G[b - 1].push_back(e);
  }
  dijkstra(0);
  cout << "Yes" << endl;

  for (int i = 1; i < V; ++i) {
    if (d[i] != INF)
      cout << y[i] + 1 << endl;
  }
}