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

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1000005;
int head[MAX], ver[MAX], next1[MAX], a[MAX];
int cnt = 0;
int n, m;
void add(int u, int v) {
  ver[cnt] = v;
  next1[cnt] = head[u];
  head[u] = cnt++;
}
bool bfs(int x) {
  queue<int> q;
  q.push(x);
  a[x] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = next1[i]) {
      int v = ver[i];
      if (a[v] == -1) {
        a[v] = u;
        q.push(v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (a[i] == -1) {
      return false;
    }
  }
  return true;
}
int main(void) {
  cin >> n >> m;
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(a, -1, sizeof(a));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }

  if (!bfs(1)) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << a[i] << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 100005;
int head[MAX], ver[MAX], next1[MAX], a[MAX];
int cnt = 0;
int n, m;
void add(int u, int v) {
  ver[cnt] = v;
  next1[cnt] = head[u];
  head[u] = cnt++;
}
bool bfs(int x) {
  queue<int> q;
  q.push(x);
  a[x] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = next1[i]) {
      int v = ver[i];
      if (a[v] == -1) {
        a[v] = u;
        q.push(v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (a[i] == -1) {
      return false;
    }
  }
  return true;
}
int main(void) {
  cin >> n >> m;
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(a, -1, sizeof(a));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }

  if (!bfs(1)) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << a[i] << endl;
    }
  }

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

#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[MAXm << 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;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define MAX 490000
#define max(a, b) ((a) < (b) ? (b) : (a))
using namespace std;
int to[MAX];
int head[MAX];
int nxt[MAX];
int v[MAX];
int sign[MAX];
int tot;
void Add(int fir, int sec) {
  nxt[++tot] = head[fir];
  to[tot] = sec;
  head[fir] = tot;
}

void bfs(int node) {
  // v[node] = 1;
  queue<int> q;
  q.push(node);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    v[x] = 1;
    for (int i = head[x]; i != -1; i = nxt[i]) {
      int y = to[i];
      if (v[y] == 1) {
        continue;
      }
      if (sign[y] == 0)
        sign[y] = x;
      q.push(y);
      // printf("%d sign is %d\\n", y, x);
    }
  }
}

int main() {
  int N, M;
  while (cin >> N >> M) {
    tot = 0;
    memset(nxt, -1, sizeof(nxt));
    // memset(v, 0, sizeof(v));
    // memset(head, 0, sizeof(head));
    // // memset(to, 0, sizeof(to));
    // memset(sign, 0, sizeof(sign));
    int fir, sec;
    for (int i = 1; i <= M; i++) {
      scanf("%d%d", &fir, &sec);
      Add(fir, sec);
      Add(sec, fir);
    }
    bfs(1);
    int flag = 0;
    for (int i = 2; i <= N; i++) {
      if (!sign[i]) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
      for (int i = 2; i <= N; i++)
        printf("%d\\n", sign[i]);
      // cout << sign[i] << endl;
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1001001001;
int main() {
  lint n, m;

  cin >> n >> m;
  vector<int> to[100005]; // 二次元配列
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b); // 道筋をtoに入れる
    to[b].push_back(a); // v[][]のi番目の末尾に入れる.つまり二次元配列
  }
  // cout << to[2][1] << endl;
  queue<int> q; // 先入先出
  vector<int> dist(n, INF); // 最小の距離を求めるから初期値はINFに設定しておく
  vector<int> pre(n, -1); // 直前の頂点.0があるから初期値は−１
  dist[0] = 0;            // 頂点０から０へのの距離は０
  q.push(0);
  while (!q.empty()) {    // qが空でない間
    int v = q.front();    // vにqの先頭を入れる
    q.pop();              // 先頭を消す
    for (int u : to[v]) { // ex.)to[0] = [1,2,3] uに1,2,3が順に入る
      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;
    cout << ++pre[i] << endl; // インクリメント忘れずに
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1001001001;
int main() {
  lint n, m;

  cin >> n >> m;
  vector<int> to[m]; // 二次元配列
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b); // 道筋をtoに入れる
    to[b].push_back(a); // v[][]のi番目の末尾に入れる.つまり二次元配列
  }
  // cout << to[2][1] << endl;
  queue<int> q; // 先入先出
  vector<int> dist(n, INF); // 最小の距離を求めるから初期値はINFに設定しておく
  vector<int> pre(n, -1); // 直前の頂点.0があるから初期値は−１
  dist[0] = 0;            // 頂点０から０へのの距離は０
  q.push(0);
  while (!q.empty()) {    // qが空でない間
    int v = q.front();    // vにqの先頭を入れる
    q.pop();              // 先頭を消す
    for (int u : to[v]) { // ex.)to[0] = [1,2,3] uに1,2,3が順に入る
      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;
    cout << ++pre[i] << endl; // インクリメント忘れずに
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define endl '\\n'
#define mod 1000000007
#define inf 1000000000000000000
const double pi = acos(-1);
const int sz = 100002;
int n, m, w, h, s;
vector<int> graph[sz];
vector<ll> d, p;
vector<bool> vis;
void bfs(int i) {
  vis[i] = true;
  queue<int> q;
  q.push(i);
  d[i] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : graph[u]) {
      if (!vis[v]) {
        d[v] = d[u] + 1;
        vis[v] = true;
        p[v] = u;
        q.push(v);
      }
    }
  }
}
int main() {
  io cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].pb(b);
    graph[b].pb(a);
  }
  d.assign(n + 1, inf);
  p.assign(n + 1, -1);
  vis.assign(n + 1, false);
  bfs(1);
  bool can = true;
  for (int i = 1; i <= n; i++) {
    if (d[i] == inf) {
      can = false;
      break;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << p[i] << endl;
    }
  } else
    cout << "No" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define endl '\\n'
#define mod 1000000007
#define inf 1000000000000000000
const double pi = acos(-1);
const int sz = 100002;
int n, m, w, h, s;
vector<pair<int, int>> adj[sz];
void dijkstra(int s, vector<ll> &d, vector<ll> &p) {
  d.assign(n + 1, inf);
  p.assign(n + 1, -1);

  d[s] = 0;
  using pii = pair<ll, ll>;
  priority_queue<pii, vector<pii>, greater<pii>> q;
  q.push({0, s});
  while (!q.empty()) {
    int v = q.top().second;
    int d_v = q.top().first;
    q.pop();
    if (d_v != d[v])
      continue;

    for (auto edge : adj[v]) {
      int to = edge.first;
      int len = edge.second;

      if (d[v] + len < d[to]) {
        d[to] = d[v] + len;
        p[to] = v;
        q.push({d[to], to});
      }
    }
  }
}
int restore_path(int t, vector<ll> p) { return p[t]; }
int main() {
  io cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb({b, 1});
    adj[b].pb({a, 1});
  }
  vector<ll> d, p;
  dijkstra(1, d, p);
  bool can = true;
  for (int i = 1; i <= n; i++) {
    if (d[i] == inf) {
      can = false;
      break;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << restore_path(i, p) << endl;
    }
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int n;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
// vector<int> g[200020];
/*
vector<ll> g[200020];
ll s[200020];
bool used[200020];
ll ans[200020];
*/
vector<ll> g[200005];
ll s[200005];
bool used[200005];
ll ans[200005];
ll depth[200005];
ll par[200005];
void bfs(queue<ll> que) {}
const int INF = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  for (ll i = 0; i < N; i++) {
    ans[i] = 1e9;
  }
  fill(depth, depth + 200005, INF);
  depth[0] = 0;
  for (ll i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<ll> que;
  que.push(0);
  // bfs(que);
  while (!que.empty()) {
    int x;
    x = que.front();
    que.pop();
    for (auto y : g[x]) {
      if (depth[y] > depth[x] + 1) {
        depth[y] = depth[x] + 1;
        que.push(y);
        par[y] = x;
      }
    }
  }
  puts("Yes");
  for (ll i = 1; i < N; i++) {
    printf("%lld\\n", par[i] + 1);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int n;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
// vector<int> g[200020];
/*
vector<ll> g[200020];
ll s[200020];
bool used[200020];
ll ans[200020];
*/
vector<ll> g[200005];
ll s[200005];
bool used[200005];
ll ans[200005];
ll depth[200005];
ll par[200005];
void bfs(queue<ll> que) {
  while (!que.empty()) {
    int x;
    x = que.front();
    que.pop();
    for (auto y : g[x]) {
      if (depth[y] > depth[x] + 1) {
        depth[y] = depth[x] + 1;
        que.push(y);
        par[y] = x;
      }
    }
    bfs(que);
  }
}
const int INF = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  for (ll i = 0; i < N; i++) {
    ans[i] = 1e9;
  }
  fill(depth, depth + 200005, INF);
  depth[0] = 0;
  for (ll i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<ll> que;
  que.push(0);
  bfs(que);
  puts("Yes");
  for (ll i = 1; i < N; i++) {
    printf("%lld\\n", par[i] + 1);
  }

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

#include <bits/stdc++.h>
#define vi vector<int>
#define pb push_back
#define Y "Yes"
using namespace std;

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

  int n, m, a, b;

  cin >> n >> m;
  vi ans(n + 1, 0);
  vector<vi> v(n + 1, vi());

  while (m--) {
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }

  queue<int> q;

  q.push(1);

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

    while (!v[f].empty()) {
      int e = v[f].back();
      v[f].pop_back();
      if (ans[e] == 0) {
        q.push(e);
        ans[e] = f;
      }
    }
  }

  cout << Y << '\\n';

  for (int i = 2; i <= n; i++)
    cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define pb push_back
#define Y "Yes"
using namespace std;

int n, m, a, b, ans[10001];
vi v[10001];

void bfs(int u) {
  queue<int> q;

  q.push(u);

  while (!q.empty()) {

    int f = q.front();
    q.pop();

    while (!v[f].empty()) {
      int e = v[f].back();
      v[f].pop_back();
      if (ans[e] == 0) {
        q.push(e);
        ans[e] = f;
      }
    }
  }
}

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

  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  cout << Y << '\\n';
  bfs(1);

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

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

int main() {
  int N, M, a, b, p, nxt;
  cin >> N >> M;
  queue<int> q, next_pos;
  vector<queue<int>> G(N);
  vector<int> done(N, 0);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    G[a - 1].emplace(b - 1);
    G[b - 1].emplace(a - 1);
  }

  q.emplace(0);
  done[0] = 1;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    next_pos = G[p];
    while (!next_pos.empty()) {
      nxt = next_pos.front();
      next_pos.pop();
      if (done[nxt] == 0) {
        done[nxt] = p + 1;
        q.emplace(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << done[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M, a, b, p, nxt;
  cin >> N >> M;
  queue<int> q, next_pos;
  vector<queue<int>> G(M);
  vector<int> done(N, 0);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    G[a - 1].emplace(b - 1);
    G[b - 1].emplace(a - 1);
  }

  q.emplace(0);
  done[0] = 1;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    next_pos = G[p];
    while (!next_pos.empty()) {
      nxt = next_pos.front();
      next_pos.pop();
      if (done[nxt] == 0) {
        done[nxt] = p + 1;
        q.emplace(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << done[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> G[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);
  }
  vector<int> ans(N, -1);
  queue<int> que;

  ans[0] = 0;
  que.push(0);

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

    for (auto nv : G[v]) {
      if (ans[nv] != -1)
        continue;
      que.push(nv);
      ans[nv] = v;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int v = 1; v < N; ++v)
    cout << ans[v] + 1 << "\\n";

  /*for (int i = 1; i < N; i++)
  {
      if (dist[i] == -1)
      {
          cout << "No" << "\\n";
          return 0;
      }

  }
  cout << "Yes" << "\\n";
  for (int v = 1; v < N; ++v){
      cout << key[v] + 1 << "\\n";
  }*/
  /*
  if (find(dist.begin(), dist.end(), -1) != dist.end()){
              cout << "No" << "\\n";
  }else{
              cout << "Yes" << "\\n";
      for (int v = 1; v < N; ++v){
          cout << key[v] + 1 << "\\n";
      }
  }*/
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> G[M];
  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);
  }
  vector<int> ans(N, -1);
  queue<int> que;

  ans[0] = 0;
  que.push(0);

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

    for (int nv : G[v]) {
      if (ans[nv] != -1)
        continue;
      que.push(nv);
      ans[nv] = v;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int v = 1; v < N; ++v)
    cout << ans[v] + 1 << "\\n";

  /*for (int i = 1; i < N; i++)
  {
      if (dist[i] == -1)
      {
          cout << "No" << "\\n";
          return 0;
      }

  }
  cout << "Yes" << "\\n";
  for (int v = 1; v < N; ++v){
      cout << key[v] + 1 << "\\n";
  }*/
  /*
  if (find(dist.begin(), dist.end(), -1) != dist.end()){
              cout << "No" << "\\n";
  }else{
              cout << "Yes" << "\\n";
      for (int v = 1; v < N; ++v){
          cout << key[v] + 1 << "\\n";
      }
  }*/
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n = 0, m = 0;

  int inputsize = 0, cache1 = 0, cache2 = 0;
  std::cin >> n >> m;
  std::vector<std::vector<int>> paths(n + 1);
  while (inputsize < m) {
    std::cin >> cache1 >> cache2;
    paths[cache1].push_back(cache2);
    paths[cache2].push_back(cache1);
    inputsize++;
  }

  std::queue<int> search_queue;
  search_queue.push(1);

  std::vector<int> guide(n + 1);
  for (auto &e : guide) {
    e = 0;
  }
  guide[1] = 1;

  int searched = 0;

  while (!search_queue.empty()) {
    auto searching = search_queue.front();
    for (int i = 0; i < paths[searching].size(); i++) {
      int adding = paths[searching][i];

      if (guide[adding] == 0) {
        search_queue.push(adding);
        guide[adding] = searching;
      }
    }
    searched++;
    search_queue.pop();
  }

  if (searched == n) {
    std::cout << "Yes\\n";
    for (int i = 2; i < guide.size(); i++) {
      std::cout << guide[i] << "\\n";
    }
  } else {
    std::cout << "No\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using path_type = std::vector<std::pair<int, int>>;

int main() {
  int n = 0, m = 0;
  path_type paths(m);
  int inputsize = 0, cache1 = 0, cache2 = 0;
  std::cin >> n >> m;
  while (inputsize < m) {
    std::cin >> cache1 >> cache2;
    paths.emplace_back(cache1, cache2);
    inputsize++;
  }

  std::vector<int> search_queue = {1};
  std::vector<bool> queueds(n + 1);
  queueds[1] = true;

  std::vector<int> guide(n + 1);
  for (auto &e : guide) {
    e = 0;
  }
  while (!search_queue.empty()) {
    auto searching = search_queue.front();
    for (int i = 0; i < paths.size(); i++) {
      auto path = paths[i];
      int adding = -1;
      if (path.first == searching) {
        adding = path.second;
      } else if (path.second == searching) {
        adding = path.first;
      } else
        continue;

      if (adding >= 0) {
        if (!queueds[adding]) {
          search_queue.push_back(adding);
          queueds[adding] = true;
        }

        if (guide[adding] == 0) {
          guide[adding] = searching;
        }
        paths.erase(paths.begin() + i);
        i--;
      }
    }
    search_queue.erase(search_queue.begin());
  }

  if (std::count(queueds.begin(), queueds.end(), true) == n) {
    std::cout << "Yes\\n";
    for (int i = 2; i < guide.size(); i++) {
      std::cout << guide[i] << "\\n";
    }
  } else {
    std::cout << "No\\n";
  }

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

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<queue<int>> ca(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ca[a].push(b);
    ca[b].push(a);
  }
  vector<bool> vis(N, false);
  queue<int> que;
  que.push(0);
  vector<int> ans(N, -1);
  while (!que.empty()) {
    int u = que.front();
    // cout << u << "  ";
    que.pop();
    vis[u] = true;
    int n = ca[u].size();
    for (int i = 0; i < n; i++) {
      int v = ca[u].front();
      // cout << v << ' ';
      ca[u].pop();
      // ca[u].push(v);
      if (vis[v])
        continue;
      if (ans[v] == -1)
        ans[v] = u;
      que.push(v);
    }
    // cout << endl;
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<queue<int>> ca(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ca[a].push(b);
    ca[b].push(a);
  }
  vector<bool> vis(N, false);
  queue<int> que;
  que.push(0);
  vector<int> ans(N, -1);
  while (!que.empty()) {
    int u = que.front();
    // cout << u << "  ";
    que.pop();
    vis[u] = true;
    int n = ca[u].size();
    for (int i = 0; i < n; i++) {
      int v = ca[u].front();
      // cout << v << ' ';
      ca[u].pop();
      ca[u].push(v);
      if (vis[v])
        continue;
      if (ans[v] == -1)
        ans[v] = u;
      que.push(v);
    }
    // cout << endl;
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, 100005);
  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] != 100005)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, 100005);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u = 0; u < n; v++) {
      if (dist[u] != 100005)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}