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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    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 hr() { cerr << "--------------" << endl; }
const int INF = 1001001001;
const int MOD = 1000000007;
// const int MOD = 998244353;

vector<int> to[100005];
int dist[100005], ans[100005];

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

  int n, m;
  cin >> n >> m;
  rep(i, 0, 100005) { dist[i] = INF; }
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  queue<int> que;
  que.push(0);
  dist[0] = 0;
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      // printf("v:%d u:%d\\n", v + 1, u + 1);
      dist[u] = dist[v] + 1;
      ans[u] = v + 1;
      que.push(u);
    }
  }
  bool ok = true;
  rep(i, 0, n) if (dist[i] == INF) ok = false;
  if (!ok) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, 1, n) cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    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 hr() { cerr << "--------------" << endl; }
const int INF = 1001001001;
const int MOD = 1000000007;
// const int MOD = 998244353;

vector<int> to[100005];
int dist[100005], color[100005], ans[100005];

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

  int n, m;
  cin >> n >> m;
  rep(i, 0, 100005) { dist[i] = INF; }
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  queue<int> que;
  que.push(0);
  dist[0] = 0;
  int p = -1;
  while (que.size()) {
    int v = que.front();
    color[v] = 1;
    que.pop();
    for (int u : to[v]) {
      if (u == p)
        continue;
      if (color[u] != 0)
        continue;
      // printf("v:%d u:%d\\n", v + 1, u + 1);
      if (dist[v] + 1 < dist[u]) {
        dist[u] = dist[v] + 1;
        ans[u] = v + 1;
      }
      que.push(u);
    }
    p = v;
  }
  bool ok = true;
  rep(i, 0, n) if (color[i] == 0) ok = false;
  if (!ok) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, 1, n) cout << ans[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  int A, B;
  vector<vector<int>> aisle(200001);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    aisle[A].push_back(B);
    aisle[B].push_back(A);
  }

  int dis[1000001], dir[100001]; // 距離と方向

  for (int i = 1; i < N + 1; i++) {
    dis[i] = 100000;
    dir[i] = 1;
  }
  dis[1] = 0;

  vector<int> roomlist;
  vector<int> roomlist_cp;

  roomlist.push_back(1);

  while (roomlist.size()) { // 更新できなくなるまで
    roomlist_cp = roomlist;
    roomlist.clear();
    for (int j : roomlist_cp) { // 部屋について
      for (int k : aisle[j]) {  // 通路について
        if (dis[k] > dis[j] + 1) {
          dis[k] = dis[j] + 1;
          dir[k] = j;
          roomlist.push_back(k);
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  int A[200000], B[200000];
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  int dis[100000], dir[100000]; // 距離と方向

  for (int i = 0; i < N; i++) {
    dis[i] = 100000;
    dir[i] = 0;
  }
  dis[0] = 0;

  vector<int> roomlist;
  vector<int> roomlist_cp;

  roomlist.push_back(0);

  for (int i = 0; i < N; i++) { // 距離について
    roomlist_cp = roomlist;
    roomlist.clear();
    for (int j = 0; j < roomlist_cp.size(); j++) { // 部屋について
      for (int k = 0; k < M; k++) {                // 通路について
        if (A[k] - 1 == roomlist_cp[j] && dis[B[k] - 1] > i + 1) {
          dis[B[k] - 1] = i + 1;
          dir[B[k] - 1] = A[k] - 1;
          roomlist.push_back(B[k] - 1);
        } else if (B[k] - 1 == roomlist_cp[j] && dis[A[k] - 1] > i + 1) {
          dis[A[k] - 1] = i + 1;
          dir[A[k] - 1] = B[k] - 1;
          roomlist.push_back(A[k] - 1);
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << dir[i] + 1 << 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 < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}
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;
}

vector<int> G[100010];

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[b].pb(a), G[a].pb(b);
  }
  vector<int> dist(n, -1);
  queue<int> q;
  q.push(0);
  dist[0] = 0;

  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto p : G[now]) {
      if (dist[p] != -1)
        continue;
      dist[p] = dist[now] + 1;
      q.push(p);
    }
  }
  puts("Yes");
  for (int i = 1; i < n; i++) {
    int rec = dist[i];
    for (auto p : G[i]) {
      if (rec == dist[p] + 1) {
        cout << p + 1 << endl;
        break;
      }
    }
  }

  // show(dist);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}
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<ll> ans(2 * 1e5, INF);

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(m);
  ans.resize(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  queue<int> q;
  q.push(0);
  ans[0] = 0;

  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto p : G[now]) {
      if (ans[p] != INF)
        continue;
      ans[p] = now;
      q.push(p);
    }
  }
  puts("Yes");
  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;
typedef long long ll;
ll n;
int visited[200005], high[200005];
typedef struct ANode {
  long long adjvex;
  struct ANode *nextarc;
} ArcNode;
typedef struct Vnode {
  int count;
  ArcNode *firstarc;
} VNode;
typedef struct node {
  VNode adjlist[100005];
} AdjGraph;
void add(AdjGraph *G, ll u, ll v) {
  ArcNode *p;
  p = (ArcNode *)malloc(sizeof(ArcNode));
  p->adjvex = v;
  p->nextarc = G->adjlist[u].firstarc;
  G->adjlist[u].firstarc = p;
  p = (ArcNode *)malloc(sizeof(ArcNode));
  p->adjvex = u;
  p->nextarc = G->adjlist[v].firstarc;
  G->adjlist[v].firstarc = p;
}
void BFS(AdjGraph *G) {
  ll w, i;
  ArcNode *p;
  queue<long long> qu;
  for (i = 1; i <= n; i++)
    visited[i] = 0;
  visited[1] = 1;
  qu.push(1);
  while (qu.size()) {
    w = qu.front();
    qu.pop();
    p = G->adjlist[w].firstarc;
    while (p != NULL) {
      if (visited[p->adjvex] == 0) {
        visited[p->adjvex] = 1;
        high[p->adjvex] = w;
        qu.push(p->adjvex);
      }
      p = p->nextarc;
    }
  }
}
int main() {
  ll a, b, m;
  AdjGraph *G;
  G = (AdjGraph *)malloc(sizeof(AdjGraph));
  cin >> n >> m;
  for (ll i = 1; i <= m; i++) {
    cin >> a >> b;
    add(G, a, b);
  }
  for (ll i = 1; i <= n; i++)
    high[i] = 0;
  BFS(G);
  for (ll i = 1; i <= n; i++)
    if (!visited[i]) {
      cout << "No" << endl;
      return 0;
    }
  cout << "Yes" << endl;
  for (ll i = 2; i <= n; i++) {
    cout << high[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;

const int MAXN = 1e5 + 5;
const int MAXM = 2e5 + 5;

int tot;
bool vis[MAXN];
int head[MAXN];
int to[MAXM << 1];
int nxt[MAXN << 1];
int bfscnt[MAXN];
bool flag = true;

int n, m;

void add(int x, int y) {

  nxt[++tot] = head[x];
  head[x] = tot;
  to[tot] = y;
}

void inp() {

  for (int i = 1; i <= m; i++) {

    int x, y;
    scanf("%d %d", &x, &y);
    add(x, y);
    add(y, x);
  }
}

void bfs() {

  queue<int> q;
  q.push(1);
  bfscnt[1] = 1;
  vis[1] = true;
  int cnt = 0;

  while (!q.empty()) {

    int x = q.front();
    q.pop();
    cnt++;
    for (int i = head[x]; i; i = nxt[i]) {
      if (vis[to[i]])
        continue;
      vis[to[i]] = true;

      bfscnt[to[i]] = x;
      q.push(to[i]);
    }
  }
  if (cnt != n)
    flag = false;
}

void debug() {

  for (int i = 1; i <= n; i++)
    printf("%d ", bfscnt[i]);
}

int main() {

  while (~scanf("%d %d", &n, &m)) {
    inp();

    bfs();
    if (flag) {
      puts("Yes");
      for (int i = 2; i <= n; i++) {
        if (i == 2)
          printf("%d", bfscnt[i]);
        else
          printf("\\n%d", bfscnt[i]);
      }
    } else
      puts("No");
    tot = 0;
    flag = true;
    memset(bfscnt, 0, sizeof(bfscnt));
    memset(nxt, 0, sizeof(nxt));
    memset(head, 0, sizeof(head));
    memset(vis, false, sizeof(vis));
    puts("");
  }

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

#include <cstdio>
#include <deque>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
const int MAX = 1e5 + 10;
vector<int> v[MAX];
deque<int> q;
int d[MAX];
int vis[MAX];
void bfs() {
  while (q.size()) {

    int x = q.front();
    q.pop_front();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (!vis[y]) {
        if (d[y] == 999999999)
          d[y] = x;
        q.push_back(y);
        vis[y] = 1;
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int hhn, hn;
    scanf("%d%d", &hhn, &hn);
    v[hhn].push_back(hn);
    v[hn].push_back(hhn);
  }
  d[1] = 0;
  vis[1] = 1;
  for (int i = 2; i <= n; i++) {
    d[i] = 999999999;
    vis[i] = 0;
  }
  q.push_back(1);
  bfs();
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (d[i] == 999999999) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << d[i] << endl;
  } else
    cout << "No" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
ll n, m;
struct node {
  ll from, to;
} road[500005];
ll num;
ll flag[500005], first[500005];
queue<int> q;
void add(ll u, ll v) {
  road[++num].from = v;
  road[num].to = first[u]; // u
  first[u] = num;
}
int bfs(ll tv, ll c) {
  flag[tv] = c;
  q.push(tv);
  while (!q.empty()) {
    ll tmp = q.front();
    q.pop();
    for (ll i = first[tmp]; i != 0; i = road[i].to) {
      if (flag[road[i].from] == 0) {
        q.push(road[i].from);
        flag[road[i].from] = tmp;
      }
    }
  }
}
int main() {
  FAST_IO;
  cin >> n >> m;
  ll u, v;
  ll f = 1;
  for (ll i = 1; i <= m; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  bfs(1, 1);
  for (ll i = 1; i <= n; i++) {
    if (flag[i] == 0)
      f = 0;
  }
  if (f == 0)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (ll i = 2; i <= n; i++) {
      cout << flag[i] << endl;
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> parent;
vector<ll> depth;
vector<vector<ll>> graph;
ll n;
using P = pair<ll, ll>;
void bfs() {
  queue<P> que;
  for (ll i = 0; i < n; i++) {
    depth[i] = 1145141919810;
  }
  depth[0] = 0;
  que.push(P(0, depth[0]));
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (ll i = 0; i < graph[p.first].size(); i++) {
      if (p.second + 1 < depth[graph[p.first][i]]) {

        parent[graph[p.first][i]] = p.first + 1;
        depth[graph[p.first][i]] = p.second + 1;
        que.push(P(graph[p.first][i], depth[graph[p.first][i]]));
      }
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll m;
  cin >> n >> m;
  graph.resize(n);
  parent.resize(n, 0);
  depth.resize(n);
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }
  bfs();
  cout << "Yes" << endl;
  for (ll i = 1; i < n; i++)
    cout << parent[i] << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n;
vector<ll> path;
vector<ll> depth;
vector<bool> visited;
bool can;
vector<vector<ll>> graph;
void dfs(ll parent, ll now, ll deep) {
  if (visited[now] == true && depth[now] == deep) {
    return;
  }
  if (visited[now] && depth[now] < deep)
    return;
  if (visited[now] && depth[now] > deep) {
    depth[now] = deep;
    path[now] = parent;
    depth[now] = deep;
  }
  visited[now] = true;
  depth[now] = deep;
  path[now] = parent;
  for (ll i = 0; i < graph[now].size(); i++) {
    dfs(now, graph[now][i], depth[now] + 1);
  }
}
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll m;
  can = true;
  cin >> n >> m;
  path.resize(n, 1145141919);
  graph.resize(n);
  visited.assign(n, false);
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }
  visited[0] = true;
  depth.resize(n, 0);
  ll cnt = 0;
  for (ll i = 0; i < graph[0].size(); i++) {
    dfs(0, graph[0][i], 1);
  }
  cout << (can ? "Yes" : "No") << "\\n";
  if (can) {
    for (ll i = 1; i < n; i++)
      cout << path[i] + 1 << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void search(vector<vector<int>> &g, vector<int> &nxt, queue<int> &q) {
  int tar = q.front();
  q.pop();
  for (int s : g[tar]) {
    if (nxt[s] == -1) {
      q.push(s);
      nxt[s] = tar;
    }
  }
  if (q.size() < 1)
    return;
  else
    search(g, nxt, q);
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(0));
  int n1, n2;
  for (int i = 0; i < m; i++) {
    cin >> n1 >> n2;
    g[n1 - 1].push_back(n2 - 1);
    g[n2 - 1].push_back(n1 - 1);
  }

  vector<int> nxt(n, -1);
  nxt[0] = 0;
  queue<int> q;
  q.push(0);
  search(g, nxt, q);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << nxt[i] + 1 << endl;
  }
}


Error: TLE

Fixed submission:

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

void search(vector<vector<int>> g, vector<int> &nxt, queue<int> &q) {
  int tar = q.front();
  q.pop();
  for (int s : g[tar]) {
    if (nxt[s] == -1) {
      q.push(s);
      nxt[s] = tar;
    }
  }
  if (q.size() < 1)
    return;
  else
    search(g, nxt, q);
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(0));
  int n1, n2;
  for (int i = 0; i < m; i++) {
    cin >> n1 >> n2;
    g[n1 - 1].push_back(n2 - 1);
    g[n2 - 1].push_back(n1 - 1);
  }

  vector<int> nxt(n, -1);
  nxt[0] = 0;
  queue<int> q;
  q.push(0);
  search(g, nxt, q);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << nxt[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

int main(void) {
  int N, M;
  std::cin >> N;
  std::cin >> M;
  std::vector<int> mark(N + 1, -1), front(1, 1), nfront;
  std::vector<std::vector<int>> path(N + 1);
  std::vector<int>::iterator imin;
  int temp1, temp2;
  for (int c = 0; c < M; ++c) {
    std::cin >> temp1;
    std::cin >> temp2;
    path[temp1].push_back(temp2);
    path[temp2].push_back(temp1);
  }
  while (front.size() != 0) {
    for (int c : front) {
      for (int d = 0; d < path[c].size(); ++d) {
        if (mark[path[c][d]] == -1) {
          mark[path[c][d]] = c;
          nfront.push_back(path[c][d]);
        }
      }
    }
    front = nfront;
    nfront.clear();
  }
  imin = std::min_element(mark.begin() + 1, mark.end());
  if (*imin == -1) {
    std::cout << "No" << std::endl;
    return 0;
  }
  std::cout << "Yes" << std::endl;
  for (int c = 2; c <= N; ++c) {
    std::cout << mark[c] << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int N, M;

void debug(const std::vector<int> &vec) {
  for (int c = 1; c <= N; ++c) {
    std::cout << vec[c] << std::endl;
  }
}

int main(void) {
  std::cin >> N;
  std::cin >> M;
  std::vector<int> mark(N + 1, -1), front(1, 1), nfront;
  std::vector<std::vector<int>> path(M + 1);
  std::vector<int>::iterator imin;
  int temp1, temp2;
  for (int c = 0; c < M; ++c) {
    std::cin >> temp1;
    std::cin >> temp2;
    path[temp1].push_back(temp2);
    path[temp2].push_back(temp1);
  }
  while (front.size() != 0) {
    for (int c : front) {
      for (int d = 0; d < (int)path[c].size(); ++d) {
        if (mark[path[c][d]] == -1) {
          mark[path[c][d]] = c;
          nfront.push_back(path[c][d]);
        }
      }
    }
    front = nfront;
    nfront.clear();
  }
  imin = std::min_element(mark.begin() + 1, mark.end());
  if (*imin == -1) {
    std::cout << "No" << std::endl;
    return 0;
  }
  std::cout << "Yes" << std::endl;
  for (int c = 2; c <= N; ++c) {
    std::cout << mark[c] << std::endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

static const ll INFTY = (1 << 21);

static const int MAX = 100000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

ll n, m;
vector<ll> p;
vector<P> adj[MAX];
vector<ll> d;

void dijkstra() {
  priority_queue<P> PQ;
  int color[n];
  int d[n];
  rep(i, n) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    P f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        p[v] = u;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

  rep(i, n) {
    if (i == 0)
      continue;
    if (p[i] == -1) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;

  rep(i, n) {
    if (i == 0)
      continue;
    cout << p[i] + 1 << endl;
  }
}

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

  p = vector<ll>(n, -1);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(make_pair(b, 1));
    adj[b].push_back(make_pair(a, 1));
  }

  dijkstra();

  return 0;
}

Error: 0

Fixed submission:

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

static const ll INFTY = (1 << 21);

ll n, m;
vector<ll> p;
vector<vector<bool>> e;
vector<ll> d;

void bfs(ll s) {
  queue<ll> q;
  q.push(s);
  d[s] = 0;
  ll u;
  while (q.empty() == false) {
    u = q.front();
    q.pop();
    rep(v, n) {
      if (e[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      p[v] = u;
      q.push(v);
    }
  }

  rep(i, n) {
    if (i == 0)
      continue;
    if (p[i] == -1) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;

  rep(i, n) {
    if (i == 0)
      continue;
    cout << p[i] + 1 << endl;
  }
}

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

  p = vector<ll>(m, -1);
  e = vector<vector<bool>>(n);
  rep(i, n) { e[i] = vector<bool>(n, false); }
  d = vector<ll>(n, INFTY);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a][b] = true;
    e[b][a] = true;
  }

  bfs(0);

  return 0;
}