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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  ll k = 0;
  for (; t != -1; t = prever[t]) {
    if (k == 2)
      break;
    path.push_back(t);
    k++;
  }

  return path;
}
int main() {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (ll i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(n, 0);
  cout << "Yes" << endl;
  for (int i = 0; i < n - 1; i++) {
    vector<int> ans = get_path(i + 1);
    cout << ans[1] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  reverse(path.begin(), path.end());
  return path;
}
int main() {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (ll i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(n, 0);
  cout << "Yes" << endl;
  for (int i = 0; i < n - 1; i++) {
    vector<int> ans = get_path(i + 1);
    cout << ans[ans.size() - 2] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;
vector<int> ans;
void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
        ans[e.to] = v;
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  reverse(path.begin(), path.end());
  return path;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  ans = vector<int>(n);
  dijkstra(n, 0);
  cout << "Yes" << endl;

  rep2(j, 1, n) { cout << ans[j] + 1 << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  reverse(path.begin(), path.end());
  return path;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(n, 0);
  cout << "Yes" << endl;
  rep2(j, 1, n) {

    vector<int> ans = get_path(j);

    cout << ans[ans.size() - 2] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

int n, m;
Graph G;

void solve() {
  vector<int> dist(n, -1);
  vector<int> prev(n, -1);
  queue<int> que;
  que.push(0);
  dist[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] == -1) {
        dist[nv] = dist[v] + 1;
        prev[nv] = v;
        que.push(nv);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i)
    cout << prev[i] + 1 << endl;
}

int main() {
  cin >> n >> m;
  G.assign(n, vector<int>());
  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);
  }
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

int n, m;
Graph G;

void solve() {
  vector<int> dist(n, -1);
  vector<int> prev(n, -1);
  queue<int> que;
  que.push(0);
  dist.at(0) = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (auto nv : G[v]) {
      if (dist[nv] == -1) {
        dist[nv] = dist[v] + 1;
        prev[nv] = v;
        que.push(nv);
      }
    }
  }

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

int main() {
  //
  cin >> n >> m;
  G.assign(n, vector<int>());

  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);
  }

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

#define _USE_MATH_DEFINES
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const int INF = 1001001001;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> to(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> q;
  vector<int> dist(N, INF);
  vector<int> pre(N, -1);
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] == INF) {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }

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


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M + 1), B(M + 1);
  for (int i = 1; i <= M; i++) {
    cin >> A[i] >> B[i];
  }
  vector<int> now(N), ans(N + 1, 0);
  int count = 1;
  now[0] = 1;
  int start;

  for (int i = 0; i < count; i++) {
    start = count;
    for (int j = start; j <= M; j++) {
      if (now[i] == A[j]) {
        if (ans[B[j]] == 0) {
          ans[B[j]] = now[i];
          now[count] = B[j];
          swap(A[count], A[j]);
          swap(B[count], B[j]);
          count++;
        }
      } else if (now[i] == B[j]) {
        if (ans[A[j]] == 0) {
          ans[A[j]] = B[j];
          now[count] = A[j];
          swap(A[count], A[j]);
          swap(B[count], B[j]);
          count++;
        }
      }
    }
  }

  if (count < N) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << ans[i] << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, m, a, b;
vector<vector<ll>> to(100005);
vector<ll> dist(100005), ans(100005);
queue<ll> q;
bool visited[100005] = {};

int main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    to[a].pub(b);
    to[b].pub(a);
  }
  q.push(0);
  visited[0] = true;
  dist[0] = 0;
  while (q.size()) {
    ll cur = q.front();
    q.pop();
    rep(i, sz(to[cur])) {
      if (visited[to[cur][i]])
        continue;
      dist[to[cur][i]] = dist[cur] + 1;
      ans[to[cur][i]] = cur;
      visited[to[cur][i]] = true;
      q.push(to[cur][i]);
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) cout << ans[i + 1] + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, m, a, b;
vector<vector<ll>> to(100005);
vector<ll> dst(100005, 1e6), ans(100005);

void dfs(ll cur, ll dist) {
  dst[cur] = dist;
  rep(i, sz(to[cur])) {
    if (dst[to[cur][i]] > dist + 1) {
      ans[to[cur][i]] = cur;
      dfs(to[cur][i], dist + 1);
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    to[a].pub(b);
    to[b].pub(a);
  }
  dfs(0, 0);
  cout << "Yes" << endl;
  rep(i, n - 1) cout << ans[i + 1] + 1 << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1001001001;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> to(N);
  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    to.at(A).push_back(B);
    to.at(B).push_back(A);
  }
  vector<int> dist(N, INF);
  queue<int> q;
  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]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, N) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

const int INF = 1001001001;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> to(N);
  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    to.at(A).push_back(B);
    to.at(B).push_back(A);
  }
  vector<int> dist(N, INF);
  queue<int> q;
  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]) {
      if (dist[u] != INF) {
        continue;
      }
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < M; i++) {
    cout << pre[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
double const pi = acos(double(-1));
const int maxn = 4e5 + 100;
using namespace std;
typedef long long ll;
struct node {
  ll to;
  ll next;
} edge[maxn + 5];
ll n, m, ans;
ll head[maxn + 5];
void addedge(ll u, ll v) {
  edge[ans].to = v;
  edge[ans].next = head[u];
  head[u] = ans++;
}
void init() {
  memset(head, -1, sizeof(head));
  ans = 0;
}
ll cnt = 0;
ll num[maxn + 5];
int vis[maxn + 5];
void bfs(int u) {
  queue<int> q;
  vis[u] = 1;
  q.push(u);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    // printf("%d ",k);
    for (int i = head[k]; ~i; i = edge[i].next) {
      int to = edge[i].to;
      if (vis[to] == -1) {
        q.push(to);
        vis[to] = k;
        cnt++;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  memset(vis, -1, sizeof(vis));
  bfs(1);
  // printf("\\n");
  if (cnt != n - 1) {
    cout << "No";
    return 0;
  } else
    cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << vis[i] << endl;
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
double const pi = acos(double(-1));
const int maxn = 2e5 + 100;
using namespace std;
typedef long long ll;
struct node {
  ll to;
  ll next;
} edge[maxn + 5];
ll n, m, ans;
ll head[maxn + 5];
void addedge(ll u, ll v) {
  edge[ans].to = v;
  edge[ans].next = head[u];
  head[u] = ans++;
}
void init() {
  memset(head, -1, sizeof(head));
  ans = 0;
}
ll cnt = 0;
ll num[maxn + 5];
int vis[maxn + 5];
void bfs(int u) {
  queue<int> q;
  vis[u] = 1;
  q.push(u);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    // printf("%d ",k);
    for (int i = head[k]; ~i; i = edge[i].next) {
      int to = edge[i].to;
      if (vis[to] == -1) {
        q.push(to);
        vis[to] = k;
        cnt++;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  memset(vis, -1, sizeof(vis));
  bfs(1);
  // printf("\\n");
  if (cnt != n - 1) {
    cout << "No";
    return 0;
  } else
    cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << vis[i] << endl;
  cout << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e6 + 10;
const ll MOD = 2019;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int dep[MAX];
int vis[MAX];
int par[MAX];
void dfs(int u) {
  queue<pair<int, int>> q;
  q.push(mp(u, 0));
  while (!q.empty()) {
    pair<int, int> from = q.front();
    q.pop();
    if (vis[from.first])
      continue;
    vis[from.first] = 1;

    for (auto v : adj[from.first]) {
      if (dep[v] >= dep[from.first] + 1) {
        par[v] = from.first;
        dep[v] = dep[from.first] + 1;
      }
      if (!vis[v])
        q.push(mp(v, dep[v]));
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    dep[i] = INT_MAX;
  dep[1] = 0;
  int cp = 0;
  dfs(1);
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      flag = false;
  }
  if (!flag)
    cout << "No";
  else {
    cout << "Yes"
         << "\\n";
    for (int i = 2; i <= n; i++)
      cout << par[i] << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e6 + 10;
const ll MOD = 2019;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int dep[MAX];
int vis[MAX];
int par[MAX];
void dfs(int u) {
  queue<pair<int, int>> q;
  q.push(mp(u, 0));
  while (!q.empty()) {
    pair<int, int> from = q.front();
    vis[from.first] = 1;
    q.pop();
    for (auto v : adj[from.first]) {
      if (dep[v] >= dep[from.first] + 1) {
        par[v] = from.first;
        dep[v] = dep[from.first] + 1;
      }
      if (!vis[v])
        q.push(mp(v, dep[v]));
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    dep[i] = INT_MAX;
  dep[1] = 0;
  int cp = 0;
  dfs(1);
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      flag = false;
  }
  if (!flag)
    cout << "No";
  else {
    cout << "Yes"
         << "\\n";
    for (int i = 2; i <= n; i++)
      cout << par[i] << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7 /*998244353*/;
const ll INF = 1LL << 60;
ll mod_pow(ll, ll, ll);
ll mod_fact(ll, ll);
ll mod_inv(ll, ll);
ll gcd(ll, ll);
ll lcm(ll, ll);
//
vector<vector<int>> graph;
void bfs(vector<int> &from, vector<bool> &searched) {
  queue<pair<int, int>> q;
  q.emplace(0, -1);
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (searched[p.first])
      continue;
    from[p.first] = p.second;
    searched[p.first] = true;
    for (auto to : graph[p.first]) {
      if (!searched[to])
        q.emplace(to, p.first);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  graph.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
  }
  vector<int> from(n);
  vector<bool> searched(n);
  bfs(from, searched);
  cout << "Yes"
       << "\\n";
  for (int i = 1; i < n; i++) {
    cout << from[i] + 1 << "\\n";
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7 /*998244353*/;
const ll INF = 1LL << 60;
ll mod_pow(ll, ll, ll);
ll mod_fact(ll, ll);
ll mod_inv(ll, ll);
ll gcd(ll, ll);
ll lcm(ll, ll);
//
int bfs(vector<vector<int>> &graph, vector<int> &from, vector<bool> searched) {
  queue<pair<int, int>> q;
  q.emplace(0, -1);
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (searched[p.first])
      continue;
    // cout << p.first << " " << p.second << " ";
    from[p.first] = p.second;
    searched[p.first] = true;
    for (auto to : graph[p.first]) {
      if (!searched[to]) {
        q.emplace(to, p.first);
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> graph(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
  }
  vector<int> from(n);
  vector<bool> searched(n);
  bfs(graph, from, searched);
  for (int i = 1; i < n; i++) {
    cout << from[i] + 1 << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

using Graph = vector<vector<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph road(n + 1);
  vector<int> shirube(n + 1);
  vector<bool> isDone(n + 1, false);

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

  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int num = q.front();
    q.pop();
    for (int v : road.at(num)) {
      if (isDone.at(v)) {
        continue;
      }

      shirube.at(v) = num;
      isDone.at(v) = true;
      q.push(v);
    }
  }

  bool isFinish = false;
  for (int i = 2; i < n + 1; i++) {
    if (i == n) {
      isFinish = true;
    }

    if (!isDone.at(i)) {
      break;
    }
  }

  if (isFinish) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      cout << shirube.at(i) << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> road(n + 1, vector<int>(n + 1));
  vector<int> shirube(n + 1);
  vector<bool> isDone(n + 1, false);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    road.at(a).at(b) = b;
    road.at(b).at(a) = a;
  }

  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int num = q.front();
    q.pop();
    for (int v : road.at(num)) {
      if (isDone.at(v)) {
        continue;
      }

      shirube.at(v) = num;
      isDone.at(v) = true;
      q.push(v);
    }
  }

  bool isFinish = false;
  for (int i = 2; i < n + 1; i++) {
    if (i == n) {
      isFinish = true;
    }

    if (!isDone.at(i)) {
      break;
    }
  }

  if (isFinish) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      cout << shirube.at(i) << endl;
    }
  } else {
    cout << "No" << endl;
  }
}