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

#include <bits/stdc++.h>
using namespace std;
/*
void showqueue(queue<int> q) {
        cout << "queue: " ;
        while (!q.empty()) {
                cout << q.front() << " " ;
                q.pop();
        }
        cout << endl;
        return;
}
*/

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

  vector<queue<int>> road(N);
  vector<int> output(N, -1);

  for (int i = 0; i < M; i++) {
    int temp_A, temp_B;
    cin >> temp_A >> temp_B;
    temp_A--;
    temp_B--;
    road[temp_A].push(temp_B);
    road[temp_B].push(temp_A);
  }

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

  while (!room_queue.empty()) {
    int temp_room = room_queue.front();
    queue<int> temp_room_queue = road[temp_room];
    while (!temp_room_queue.empty()) {
      int temp_room_new = temp_room_queue.front();
      if (output[temp_room_new] == -1) {
        output[temp_room_new] = temp_room;
        room_queue.push(temp_room_new);
        // cout << temp_room_new << " " << num << endl;
      }
      temp_room_queue.pop();
    }
    room_queue.pop();
  }

  // output
  // cout << endl;

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
void showqueue(queue<int> q) {
        cout << "queue: " ;
        while (!q.empty()) {
                cout << q.front() << " " ;
                q.pop();
        }
        cout << endl;
        return;
}
*/

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

  vector<queue<int>> road(M);
  vector<int> output(N, -1);

  for (int i = 0; i < M; i++) {
    int temp_A, temp_B;
    cin >> temp_A >> temp_B;
    temp_A--;
    temp_B--;
    road[temp_A].push(temp_B);
    road[temp_B].push(temp_A);
  }

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

  while (!room_queue.empty()) {
    int temp_room = room_queue.front();
    queue<int> temp_room_queue = road[temp_room];
    while (!temp_room_queue.empty()) {
      int temp_room_new = temp_room_queue.front();
      if (output[temp_room_new] == -1) {
        output[temp_room_new] = temp_room;
        room_queue.push(temp_room_new);
        // cout << temp_room_new << " " << num << endl;
      }
      temp_room_queue.pop();
    }
    room_queue.pop();
  }

  // output
  // cout << endl;

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

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

#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 - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

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

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

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

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

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {

  int64_t N, M;

  cin >> N >> M;

  vector<vector<int64_t>> root(N, vector<int64_t>(N));

  for (int64_t i = 0; i < M; i++) {
    int64_t a, b;
    cin >> a >> b;
    root[a - 1][b - 1] = 1;
    root[b - 1][a - 1] = 1;
  }

  queue<int64_t> a;
  a.push(0);
  vector<int64_t> check(N);
  vector<int64_t> arrange(N);
  check[0] = 1;
  int64_t l;
  int64_t count = 0;
  while (a.size() != 0) {

    l = a.front();

    for (int64_t i = 0; i < N; i++) {

      if (root[l][i] == 1 && check[i] == 0) {
        arrange[i] = l;
        a.push(i);
        check[i] = 1;
        count++;
      }
      if (count == N - 1) {
        cout << "Yes" << endl;
        for (int64_t i = 1; i < N; i++) {
          cout << arrange[i] + 1 << endl;
        }
        return 0;
      }
    }

    a.pop();
  }

  for (int64_t i = 0; i < N; i++) {
    if (check[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int64_t i = 1; i < N; i++) {
    cout << arrange[i] + 1 << endl;
  }

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

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

constexpr int MAX = 1e+9;

void dfs(int pos, int par, vector<pair<int, int>> &ans,
         vector<vector<int>> &G) {
  ans[pos].first = ans[par].first + 1;
  ans[pos].second = par;
  for (auto v : G[pos]) {
    if (ans[v].first > ans[pos].first)
      dfs(v, pos, ans, G);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  // (距離, そこに来た親) の順
  vector<pair<int, int>> ans(N, make_pair(MAX, 0));
  // ans[0].first = 0;
  // dfs(0, 0, ans, G);

  vector<int> d(N, MAX);
  d[0] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    auto top = q.front();
    q.pop();
    int curr = top.first;
    int par = top.second;
    if (curr != 0 and d[curr] != MAX)
      continue;
    d[curr] = d[par] + 1;
    ans[curr].second = par;
    for (auto v : G[curr]) {
      if (d[v] == MAX)
        q.push(make_pair(v, curr));
    }
  }

  cout << "Yes" << endl;
  FOR(i, 1, N) cout << ans[i].second + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

constexpr int MAX = 1e+9;

void dfs(int pos, int par, vector<pair<int, int>> &ans,
         vector<vector<int>> &G) {
  ans[pos].first = ans[par].first + 1;
  ans[pos].second = par;
  for (auto v : G[pos]) {
    if (ans[v].first > ans[pos].first)
      dfs(v, pos, ans, G);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  // (距離, そこに来た親) の順
  vector<pair<int, int>> ans(N, make_pair(MAX, 0));
  ans[0].first = 0;
  dfs(0, 0, ans, G);

  cout << "Yes" << endl;
  FOR(i, 1, N) cout << ans[i].second + 1 << endl;

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> room(m + 2);
  vector<int> dist(n + 1, -1);
  queue<int> que;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }
  // 部屋1から始める
  dist[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : room[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  // for(int i=1; i<dist.size(); i++) cout << "room :" << i << ", dist :" <<
  // dist[i] << endl;
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    int minDist = 10000000;
    int minRoom;
    for (int j = 0; j < (int)room[i].size(); j++) {
      // cout << "j :" << j << ", room :" << room[i][j] <<  ", dist :" <<
      // dist[room[i][j]] << endl;
      if (dist[room[i][j]] < minDist) {
        minDist = dist[room[i][j]];
        minRoom = room[i][j];
      }
    }
    cout << minRoom << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> room(m + 1);
  vector<int> dist(n + 1, -1);
  queue<int> que;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }
  // 部屋1から始める
  dist[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : room[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  // for(int i=1; i<dist.size(); i++) cout << "room :" << i << ", dist :" <<
  // dist[i] << endl;
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    int minDist = 10000000;
    int minRoom;
    for (int j = 0; j < (int)room[i].size(); j++) {
      // cout << "j :" << j << ", room :" << room[i][j] <<  ", dist :" <<
      // dist[room[i][j]] << endl;
      if (dist[room[i][j]] < minDist) {
        minDist = dist[room[i][j]];
        minRoom = room[i][j];
      }
    }
    cout << minRoom << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int mark[100005];
vector<int> m[100005];

int main() {
  int n, md;
  queue<int> q;

  cin >> n >> md;

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

  q.push(1);
  while (!q.empty()) {
    int val = q.front();
    q.pop();
    for (int i = 0; i < m[val].size(); i++) {
      int next = m[val][i];
      if (mark[next] == 0) {
        mark[next] = val;
        q.push(next);
      }
    }
  }

  bool flg = true;
  for (int i = 2; i <= n; i++) {
    if (mark[i] == 0)
      flg = false;
  }
  if (flg) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << mark[i] << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int mark[10005];
vector<int> m[10005];

int main() {
  int n, md;
  queue<int> q;

  cin >> n >> md;

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

  q.push(1);
  while (!q.empty()) {
    int val = q.front();
    q.pop();
    for (int i = 0; i < m[val].size(); i++) {
      int next = m[val][i];
      if (mark[next] == 0) {
        mark[next] = val;
        q.push(next);
      }
    }
  }

  bool flg = true;
  for (int i = 2; i <= n; i++) {
    if (mark[i] == 0)
      flg = false;
  }
  if (flg) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << mark[i] << endl;
  } else {
    cout << "No" << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int s[205000];
vector<int> v[205000];
int main() {
  int n, m;
  memset(s, -1, sizeof(s));
  cin >> n >> m;
  while (m--) {
    int u, v1;
    cin >> u >> v1;
    v[u].push_back(v1);
    v[v1].push_back(u);
  }
  queue<int> que;
  s[1] = 0;
  for (int i = 0; i < v[1].size(); i++) {
    que.push(v[1][i]);
    s[v[1][i]] = 1;
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < v[p].size(); i++) {
      int g = v[p][i];
      if (s[g] == -1) {
        s[g] = p;
        que.push(g);
      }
    }
  }
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (s[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << s[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int s[205000];
vector<int> v[100];
int main() {
  int n, m;
  memset(s, -1, sizeof(s));
  cin >> n >> m;
  while (m--) {
    int u, v1;
    cin >> u >> v1;
    v[u].push_back(v1);
    v[v1].push_back(u);
  }
  queue<int> que;
  s[1] = 0;
  for (int i = 0; i < v[1].size(); i++) {
    que.push(v[1][i]);
    s[v[1][i]] = 1;
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < v[p].size(); i++) {
      int g = v[p][i];
      if (s[g] == -1) {
        s[g] = p;
        que.push(g);
      }
    }
  }
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (s[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << s[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

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

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

int N, M;
vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<ll> Q;
  vector<ll> color(N, 0);
  Q.push(0);
  color[0] = 1;
  while (!Q.empty()) {
    ll u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      if (color[G[u][i]] == 0) {
        Q.push(G[u][i]);
        color[G[u][i]] = 1;
        ans[G[u][i]] = u;
      }
    }
    // cout << u << endl;
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}

signed main() {
  cin >> N >> M;
  G.resize(N);
  ans.resize(N);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  // cout << "OK" << endl;
  bfs();
  return 0;
}

Error: TLE

Fixed submission:

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

int N, M;
vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<ll> Q;
  vector<ll> color(N, 0);
  Q.push(0);
  color[0] = 1;
  while (!Q.empty()) {
    ll u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      if (color[G[u][i]] == 0) {
        Q.push(G[u][i]);
        color[G[u][i]] = 1;
        ans[G[u][i]] = u;
      }
    }
    // cout << u << endl;
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}

signed main() {
  cin >> N >> M;
  G.resize(N);
  ans.resize(N);
  for (int i = 0; i < N; i++)
    G[i].resize(N);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  // cout << "OK" << endl;
  bfs();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define acc(v) accumulate(v.begin(), v.end(), 0)

using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;

vector<int> g[100010];
pair<int, int> gc[100010];
queue<pair<int, int>> q;

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, n) { gc[i] = make_pair(INF, -1); }
  gc[0] = make_pair(0, 0);
  // for(int i : g[0]){
  //     q.push(make_pair(i,0));
  // }
  q.push(make_pair(0, -1));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int now = p.first;

    for (int i : g[now]) {
      if (i == p.second)
        continue;
      if (gc[i].first > gc[now].first + 1) {
        gc[i] = make_pair(gc[now].first + 1, now);
        q.push(make_pair(i, now));
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << gc[i + 1].second + 1 << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define acc(v) accumulate(v.begin(), v.end(), 0)

using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;

vector<int> g[100010];
pair<int, int> gc[100010];

void bfs(int now, int before) {
  for (int i : g[now]) {
    if (i == before)
      continue;
    if (gc[i].first > gc[now].first + 1) {
      gc[i] = make_pair(gc[now].first + 1, now);
      bfs(i, now);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, n) { gc[i] = make_pair(INF, -1); }
  gc[0] = make_pair(0, 0);
  bfs(0, 0);
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << gc[i + 1].second + 1 << endl; }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

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

  int dist[200001];
  for (int i = 1; i <= n; i++)
    dist[i] = -1;
  queue<int> q;

  dist[1] = 0;
  q.push(1);

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

    for (int node : graph[cur]) {
      if (dist[node] != -1)
        continue;
      dist[node] = cur; // 道しるべの更新
      q.push(node);
    }
  }

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

Error: -11

Fixed submission:

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

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

  int dist[200001];
  for (int i = 1; i <= n; i++)
    dist[i] = -1;
  queue<int> q;

  dist[1] = 0;
  q.push(1);

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

    for (int node : graph[cur]) {
      if (dist[node] != -1)
        continue;
      dist[node] = cur;
      q.push(node);
    }
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define PB push_back
#define POP pop_back()
#define PII pair<int, int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\\n'
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
vector<int> edg[200005];
int vis[200005];

void dfs() {
  queue<int> num;
  vis[1] = 1;
  num.push(1);
  while (num.size()) {
    int tmp = num.front();
    num.pop();
    int len = edg[tmp].size();
    for (int i = 0; i < len; i++) {
      if (vis[edg[tmp][i]] == 0) {
        vis[edg[tmp][i]] = tmp;
        num.push(edg[tmp][i]);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 200005; i++) {
    vis[i] = 0;
  }
  int ta, tb;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &ta, &tb);
    edg[ta].PB(tb);
    edg[tb].PB(ta);
  }
  dfs();
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      printf("No\\n");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++) {
    printf("%d\\n", vis[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define PB push_back
#define POP pop_back()
#define PII pair<int, int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\\n'
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
vector<int> edg[200005];
int lla[200005];
int vis[200005];
void dfs(int now, int la) {
  int len = edg[now].size();
  for (int i = 0; i < len; i++) {
    if (edg[now][i] == la)
      continue;
    if (vis[edg[now][i]] == 0) {
      lla[edg[now][i]] = now;
      vis[edg[now][i]] = vis[now] + 1;
      dfs(edg[now][i], now);
    } else {
      if (vis[edg[now][i]] > vis[now] + 1) {
        lla[edg[now][i]] = now;
        vis[edg[now][i]] = vis[now] + 1;
        dfs(edg[now][i], now);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 200005; i++) {
    vis[i] = 0;
  }
  int ta, tb;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &ta, &tb);
    edg[ta].PB(tb);
    edg[tb].PB(ta);
  }
  vis[1] = 1;
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      printf("No\\n");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++) {
    printf("%d\\n", lla[i]);
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <iomanip>
#include <math.h>
#include <queue>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

int main() {

  int N, M;
  cin >> N >> M;

  string ans = "Yes";

  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> flag(N + 1, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

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

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

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

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

  cout << ans << endl;

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

  cin >> ans;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <iomanip>;
#include <math.h>
#include <queue>;
#include <vector>;
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

int main() {

  int N, M;
  cin >> N >> M;

  string ans = "Yes";

  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> flag(N + 1, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

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

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

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

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

  cout << ans << endl;

  for (int i = 1; i < N; i++) {
    cout << flag[i];
  }

  cin >> ans;

  return 0;
}