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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9;
const long long LINF = 1e18;

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

  vector<vector<int>> graph(N, vector<int>());
  for (int i = 0; i < M; i++) {
    int Ai, Bi;
    cin >> Ai >> Bi;
    graph[Ai - 1].push_back(Bi - 1);
    graph[Bi - 1].push_back(Ai - 1);
  }

  vector<int> stamp(N, -1);

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

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    if (stamp[p.first] != -1) {
      continue;
    }

    stamp[p.first] = p.second;

    for (size_t i = 0; i < graph[p.first].size(); i++) {
      if (stamp[graph[p.first][i]] == -1) {
        q.push(pair<int, int>(graph[p.first][i], p.first));
      }
    }
  }

  bool isComplete = true;
  for (int i = 0; i < N; i++) {
    if (stamp[i] == -1) {
      isComplete = false;
      break;
    }
  }

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

  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9;
const long long LINF = 1e18;

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

  vector<vector<bool>> graph(N, vector<bool>(N, false));
  for (int i = 0; i < M; i++) {
    int Ai, Bi;
    cin >> Ai >> Bi;
    graph[Ai - 1][Bi - 1] = true;
    graph[Bi - 1][Ai - 1] = true;
  }

  vector<int> stamp(N, -1);

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

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    if (stamp[p.first] != -1) {
      continue;
    }

    stamp[p.first] = p.second;

    for (int i = 0; i < N; i++) {
      if (graph[p.first][i] && stamp[i] == -1) {
        q.push(pair<int, int>(i, p.first));
      }
    }
  }

  bool isComplete = true;
  for (int i = 0; i < N; i++) {
    if (stamp[i] == -1) {
      isComplete = false;
      break;
    }
  }

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

  } else {
    cout << "No" << endl;
  }

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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int tot, head[maxn];
struct E {
  int to, next;
} edge[maxn << 1];
void add(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int n, m, ans[maxn], cnt = 0;
void bfs(int x) {
  ans[x] = 1;
  cnt += 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (!ans[v]) {
        cnt += 1;
        ans[v] = now;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  mem(head, -1);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs(1);
  if (cnt != n) {
    puts("No");
    return 0;
  }
  puts("Yes");
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5000;
int tot, head[maxn];
struct E {
  int to, next;
} edge[maxn << 1];
void add(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int n, m, ans[maxn], cnt = 0;
void bfs(int x) {
  ans[x] = 1;
  cnt += 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (!ans[v]) {
        cnt += 1;
        ans[v] = now;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  mem(head, -1);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs(1);
  if (cnt != n) {
    puts("No");
    return 0;
  }
  puts("Yes");
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int n, m, a, b;
set<int> adj[(int)1e5 + 5];

void sol() {
  queue<int> q;
  q.push(1);
  vector<int> signpost((int)1e5 + 5, 1e9);

  while (!q.empty()) {
    int here = q.front();
    q.pop();
    for (auto there : adj[here]) {
      if (signpost[there] == 1e9) {
        signpost[there] = here;
        q.push(there);
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i) {
    cout << signpost[i] << "\\n";
  }
}

int main() {
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].emplace(b);
    adj[b].emplace(a);
  }
  sol();
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, m, a, b;
set<int> adj[(int)1e5 + 5];

void BellMan() {
  vector<int> check(n, 0);
  vector<int> dist((int)1e5 + 5, 1e9);
  check[1] = 1;
  dist[1] = 0;
  vector<int> signpost((int)1e5 + 5, 1e9);

  for (int i = 1; i <= n; ++i) {
    for (int here = 1; here <= n; ++here) {
      for (auto there : adj[here]) {
        if (dist[there] > dist[here] + 1) {
          dist[there] = dist[here] + 1;
          signpost[there] = here;
        }
      }
    }
  }
  for (int i = 2; i <= n; ++i) {
    if (dist[i] >= 1e6) {
      cout << "No";
      return;
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i) {
    cout << signpost[i] << "\\n";
  }
}

int main() {
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].emplace(b);
    adj[b].emplace(a);
  }
  BellMan();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> G;
vector<int> ans;
vector<int> visited;

void bfs() {
  queue<pair<int, int>> que;
  que.emplace(-1, 0);
  while (!que.empty()) {
    int from = que.front().first;
    int cur = que.front().second;
    que.pop();

    ans[cur] = from;

    for (auto to : G[cur]) {
      if (!visited[to]) {
        que.emplace(cur, to);
        visited[to] = 1;
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;

  visited.resize(n, 0);
  G.resize(n);
  ans.resize(n, 0);
  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);
  }

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

Error: TLE

Fixed submission:

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

vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<pair<int, int>> que;
  que.emplace(-1, 0);
  while (!que.empty()) {
    int from = que.front().first;
    int cur = que.front().second;
    que.pop();

    if (ans[cur] == -10) {
      ans[cur] = from;
    }

    for (auto to : G[cur]) {
      if (ans[to] == -10) {
        que.emplace(cur, to);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;

  G.resize(n);
  ans.resize(n, -10);
  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);
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vvl = vector<vector<ll>>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; ++i)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define DBG(a, b, c, d)                                                        \\
  cout << a << " " << b << " " << c << " " << d << " " << endl
const ll MOD = 1000000007;
const double PI = acos(-1);
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||
// map<int,int>mp

int n, m;
int ans;
// string s;
signed main() {
  acc_io;
  cin >> n >> m;
  vi TO[100005];
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    TO[a].pb(b);
    TO[b].pb(a);
  }
  queue<int> q;
  q.push(0);
  vi pre(n, -1);
  vi cnt(n);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : TO[v]) {
      if (pre[u] >= 0)
        continue;
      pre[u] = v;
      cnt[u] = cnt[v] + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) {
    ans = pre[i] + 1;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vvl = vector<vector<ll>>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; ++i)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define DBG(a, b, c, d)                                                        \\
  cout << a << " " << b << " " << c << " " << d << " " << endl
const ll MOD = 1000000007;
const double PI = acos(-1);
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||
// map<int,int>mp

int n, m;
int ans;
// string s;
signed main() {
  acc_io;
  cin >> n >> m;
  vvi TO(n, vi(n));
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    TO.at(a).pb(b);
    TO.at(b).pb(a);
  }
  queue<int> q;
  q.push(0);
  vi pre(n, -1);
  vi cnt(n);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : TO[v]) {
      if (pre[u] >= 0)
        continue;
      pre[u] = v;
      cnt[u] = cnt[v] + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) {
    ans = pre[i] + 1;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long

int main() {
  int n, m;
  int x, y;
  int i, j;
  int q;
  int count = 0;

  cin >> n >> m;
  vector<vector<int>> a(n + 1, vector<int>(0));
  vector<int> b;
  vector<int> c;
  vector<int> h(n + 1);
  for (i = 0; i <= n; i++)
    h[i] = 0;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (i = 0; i < a[1].size(); i++) {
    c.push_back(a[1][i]);
    h[a[1][i]] = 1;
    count++;
  }
  h[1] = 1;
  while (count < n - 1) {
    // cout << count << '\\n';
    if (c.size() == 0)
      break;
    for (q = 0; q < c.size(); q++) {
      i = c[q];
      for (j = 0; j < a[i].size(); j++) {
        // cout << j << '\\n';
        if (h[a[i][j]] != 0)
          continue;
        b.push_back(a[i][j]);
        h[a[i][j]] = i;
        count++;
      }
    }
    c.clear();
    for (j = 0; j < b.size(); j++)
      c.push_back(b[j]);
    b.clear();
  }

  if (count >= n - 1) {
    cout << "Yes" << '\\n';
    for (i = 2; i <= n; i++)
      cout << h[i] << '\\n';
  } else {
    cout << "No";
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define ll long long

int main() {
  int n, m;
  int x, y;
  int i, j;
  int q;
  int count = 0;

  cin >> n >> m;
  vector<vector<int>> a(m + 1);
  vector<int> b;
  vector<int> c;
  vector<int> h(n + 1);
  vector<int> s(n + 1);
  for (i = 0; i <= n; i++)
    h[i] = 0;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (i = 0; i < a[1].size(); i++) {
    c.push_back(a[1][i]);
    h[a[1][i]] = 1;
    count++;
  }
  h[1] = 1;
  while (count < n - 1) {
    // cout << count << '\\n';
    if (c.size() == 0)
      break;
    for (q = 0; q < c.size(); q++) {
      i = c[q];
      for (j = 0; j < a[i].size(); j++) {
        // cout << j << '\\n';
        if (h[a[i][j]] != 0)
          continue;
        b.push_back(a[i][j]);
        h[a[i][j]] = i;
        count++;
      }
    }
    c.clear();
    for (j = 0; j < b.size(); j++)
      c.push_back(b[j]);
    b.clear();
  }

  if (count == n - 1) {
    cout << "Yes" << '\\n';
    for (i = 2; i <= n; i++)
      cout << h[i] << '\\n';
  } else {
    cout << "No";
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI 3.14159265358979323846264338327950
vector<int> adj[100001];
int lvl[100001] = {0}, parent[100001];
void bfs(int x) {
  queue<int> q;
  q.push(x);
  while (q.size()) {
    x = q.front();
    q.pop();
    for (int i = 0; i < adj[x].size(); i++) {
      if (lvl[adj[x][i]] > lvl[x] + 1) {
        lvl[adj[x][i]] = lvl[x] + 1;
        parent[adj[x][i]] = x;
        q.push(adj[x][i]);
      }
    }
  }
}
signed main() {
  SPEED;
  int n, m;
  cin >> n >> m;
  for (int i = 2; i < 100001; i++) {
    lvl[i] = 1e18;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  bfs(1);
  for (int i = 2; i <= n; i++) {
    if (lvl[i] == 1e18) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI 3.14159265358979323846264338327950
vector<int> adj[100001];
int lvl[100001] = {0}, parent[100001];
void dfs(int x) {
  for (int i = 0; i < adj[x].size(); i++) {
    if (lvl[adj[x][i]] > lvl[x] + 1) {
      lvl[adj[x][i]] = lvl[x] + 1;
      parent[adj[x][i]] = x;
      dfs(adj[x][i]);
    }
  }
}
signed main() {
  SPEED;
  int n, m;
  cin >> n >> m;
  for (int i = 2; i < 100001; i++) {
    lvl[i] = 1e18;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    if (lvl[i] == 1e18) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> r(n + 1);
  vector<int> answer(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r.at(a).push_back(b);
    r.at(b).push_back(a);
  }
  vector<bool> checked(n + 1, false);
  queue<int> q;
  q.push(1);
  checked.at(1) = true;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : r.at(t)) {
      if (!checked.at(i)) {
        answer.at(i) = t;
        checked.at(i) = true;
        q.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << answer.at(i) << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> r(m + 1);
  vector<int> answer(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r.at(a).push_back(b);
    r.at(b).push_back(a);
  }
  vector<bool> checked(n + 1, false);
  queue<int> q;
  q.push(1);
  checked.at(1) = true;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : r.at(t)) {
      if (!checked.at(i)) {
        answer.at(i) = t;
        checked.at(i) = true;
        q.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << answer.at(i) << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> room(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    room[A - 1].push_back(B - 1);
    room[B - 1].push_back(A - 1);
  }
  vector<int> dest(N);
  queue<int> que;
  vector<int> dist(N, -1);
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    int s = que.front();
    que.pop();
    for (int i : room[s]) {
      if (dist[i] == -1) {
        dist[i] = dist[s] + 1;
        dest[i] = s;
        que.push(i);
      }
    }
  }

  bool flag = true;
  for (int i : dist) {
    if (i == -1)
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << dest[i] + 1 << endl;
  } else
    cout << "No" << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> room(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    room[A - 1].push_back(B - 1);
    room[B - 1].push_back(A - 1);
    cout << "ok" << endl;
  }
  vector<int> dest(N);
  queue<int> que;
  vector<int> dist(N, -1);
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    cout << que.size() << endl;
    int s = que.front();
    que.pop();
    for (int i : room[s]) {
      if (dist[i] == -1) {
        dist[i] = dist[s] + 1;
        dest[i] = s;
        que.push(i);
      }
    }
  }

  bool flag = true;
  for (int i : dist) {
    if (i == -1)
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 1; i < M; i++)
      cout << dest[i] + 1 << endl;
  } else
    cout << "No" << endl;

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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, m;

// BFS
vector<vector<int>> g;
const int N = 3e5;
int used[N];
int mitis[N];

void bfs(int i, int c) {
  REP(i, N) {
    used[i] = -1;
    mitis[i] = -1;
  }
  used[i] = c;
  queue<int> que;
  que.push(i);
  int id;
  // col[i] = c;
  while (!que.empty()) {
    // cout << que.front() << " "; // 先頭要素を参照する
    id = que.front();
    int lootlen = used[id];
    for (auto to : g[id]) {
      if (used[to] == -1) {
        que.push(to);
        used[to] = lootlen + 1;
        mitis[to] = id;
      }
    }
    que.pop(); // 先頭要素を削除
  }
  return;
}

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

  int flag = 0;
  for (ll i = 1; i < n; i++) {
    if (mitis[i] == -1)
      flag = 1;
  }
  if (flag == 1) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;

  for (ll i = 1; i < n; i++) {
    cout << (mitis[i] + 1) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, m;

// BFS
vector<vector<int>> g;
const int N = 1e5;
int used[N];
int mitis[N];

void bfs(int i, int c) {
  REP(i, N) {
    used[i] = -1;
    mitis[i] = -1;
  }
  used[i] = c;
  queue<int> que;
  que.push(i);
  int id;
  // col[i] = c;
  while (!que.empty()) {
    // cout << que.front() << " "; // 先頭要素を参照する
    id = que.front();
    int lootlen = used[id];
    for (auto to : g[id]) {
      if (used[to] == -1) {
        que.push(to);
        used[to] = lootlen + 1;
        mitis[to] = id;
      }
    }
    que.pop(); // 先頭要素を削除
  }
  return;
}

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

  int flag = 0;
  for (ll i = 1; i < n; i++) {
    if (mitis[i] == -1)
      flag = 1;
  }
  if (flag == 1) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;

  for (ll i = 1; i < n; i++) {
    cout << (mitis[i] + 1) << endl;
  }

  return 0;
}