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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b)                                                           \\
  for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }

signed main() {
  int n, m;
  cin >> n >> m;
  vvi g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vi checked(n), p(n, -1);
  checked[0] = 1;
  queue<int> st;
  st.push(0);
  while (not st.empty()) {
    int now = st.front();
    st.pop();

    for (auto next : g[now]) {
      if (checked[next] == 0) {
        checked[next] = 1;
        p[next] = now;
        st.push(next);
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << p[i] + 1 << endl; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b)                                                           \\
  for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }

signed main() {
  int n, m;
  cin >> n >> m;
  vvi g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vi d(n, inf), p(n, -1);
  d[0] = 0;
  stack<int> st;
  st.push(0);
  while (not st.empty()) {
    int now = st.top();
    st.pop();

    for (auto next : g[now]) {
      if (d[next] > d[now] + 1) {
        d[next] = d[now] + 1;
        p[next] = now;
        st.push(next);
      }
    }
  }
  dump(d, p);
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << p[i] + 1 << endl; }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void Main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  rep(i, M) { cin >> A[i] >> B[i]; }
  vector<int> shirube(N + 1);
  vector<int> dist(N);
  rep(i, N) shirube[i] = 0;
  queue<int> q;
  set<int> visited;
  visited.insert(1);
  rep(i, M) {
    if (A[i] == 1) {
      shirube[B[i]] = 1;
      q.push(B[i]);
      visited.insert(B[i]);
    }
    if (B[i] == 1) {
      shirube[A[i]] = 1;
      q.push(A[i]);
      visited.insert(A[i]);
    }
  }
  vector<vector<int>> direction(N + 1);
  rep(i, M) {
    direction[A[i]].push_back(B[i]);
    direction[B[i]].push_back(A[i]);
  }
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    rep(i, direction[cur].size()) {
      if (visited.find(direction[cur][i]) == visited.end()) {
        if (shirube[direction[cur][i]] == 0) {
          q.push(direction[cur][i]);
          shirube[direction[cur][i]] = cur;
          visited.insert(direction[cur][i]);
        } else {
          cout << "No" << endl;
          return;
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << shirube[i] << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  Main();
  return 0;
}

Error: TLE

Fixed submission:

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

void Main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  rep(i, M) { cin >> A[i] >> B[i]; }
  vector<int> shirube(N + 1);
  vector<int> dist(N);
  rep(i, N) shirube[i] = 0;
  queue<int> q;
  set<int> visited;
  rep(i, M) {
    if (A[i] == 1) {
      shirube[B[i]] = 1;
      q.push(B[i]);
      visited.insert(B[i]);
    }
    if (B[i] == 1) {
      shirube[A[i]] = 1;
      q.push(A[i]);
      visited.insert(A[i]);
    }
  }
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    rep(i, M) {
      if (A[i] == cur) {
        if (visited.find(B[i]) == visited.end()) {
          if (shirube[B[i]] == 0) {
            q.push(B[i]);
            shirube[B[i]] = cur;
            visited.insert(B[i]);
          } else {
            cout << "No" << endl;
            return;
          }
        }
      }
      if (B[i] == cur) {
        if (visited.find(A[i]) == visited.end()) {
          if (shirube[A[i]] == 0) {
            q.push(A[i]);
            shirube[A[i]] = cur;
            visited.insert(A[i]);
          } else {
            cout << "No" << endl;
            return;
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << shirube[i] << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  Main();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); ++i)

int main() {
  cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> C(N);
  for (int i = 0; i < M; ++i) {
    int A, B;
    std::cin >> A >> B;
    A--;
    B--;
    C[A].emplace_back(B);
    C[B].emplace_back(A);
  }

  std::vector<int> next(N, -1);
  std::vector<int> flag(N, 0);

  std::queue<int> q;
  q.push(0);
  next[0] = 0;
  while (!q.empty()) {
    int const index = q.front();
    q.pop();
    if (flag[index])
      continue;
    flag[index] = 1;
    for (auto const n : C[index]) {
      if (next[n] == -1) {
        next[n] = index;
        q.push(n);
      }
    }
  }
  for (auto const n : next) {
    if (n == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::cout << "Yes\\n";
  for (int i = 1; i < N; ++i) {
    std::cout << next[i] + 1 << "\\n";
  }
  std::cout << std::flush;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); ++i)

int main() {
  cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> C(M);
  for (int i = 0; i < M; ++i) {
    int A, B;
    std::cin >> A >> B;
    A--;
    B--;
    C[A].emplace_back(B);
    C[B].emplace_back(A);
  }

  std::vector<int> next(N, -1);
  std::vector<int> flag(N, 0);

  std::queue<int> q;
  q.push(0);
  next[0] = 0;
  while (!q.empty()) {
    int const index = q.front();
    q.pop();
    if (flag[index])
      continue;
    flag[index] = 1;
    for (auto const n : C[index]) {
      if (next[n] == -1) {
        next[n] = index;
        q.push(n);
      }
    }
  }
  for (auto const n : next) {
    if (n == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::cout << "Yes\\n";
  for (int i = 1; i < N; ++i) {
    std::cout << next[i] + 1 << "\\n";
  }
  std::cout << std::flush;

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int N, M;
vector<int> A(200000), B(200000), ans(100000), visited(100000, 0);
bool judge = true;
queue<int> q;
vector<vector<int>> graph(200001, vector<int>(0));

void DFS(int now) {

  visited[0] = 1;
  rep(i, graph[now].size()) {
    if (visited[graph[now][i] - 1] == 0) {
      ans[graph[now][i] - 1] = now;
      visited[graph[now][i] - 1] = 1;
      q.push(graph[now][i]);
      // DFS(B[i]);
    }
  }
  return;
  // cout << q.size() <<endl;
}

int main() {

  cin >> N >> M;
  rep(i, M) {
    cin >> A[i] >> B[i];
    graph[A[i]].push_back(B[i]);
    graph[B[i]].push_back(A[i]);
  }
  DFS(1);

  while (1) {
    rep(i, q.size()) {
      // cout << q.front() <<endl;
      DFS(q.front());
      q.pop();
    }
    if (q.size() == 0)
      break;
  }

  rep(i, N) {
    if (visited[i] == 0) {
      judge = 0;
      break;
    }
  }

  if (judge) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << ans[i] << endl;
  } else
    cout << "No" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int N, M;
vector<int> A(200000), B(200000), ans(100000), visited(100000, 0);
bool judge = true;
queue<int> q;
void DFS(int now) {

  visited[0] = 1;
  rep(i, M) {
    if (now == A[i] && visited[B[i] - 1] == 0) {
      ans[B[i] - 1] = now;
      visited[B[i] - 1] = 1;
      q.push(B[i]);
      // DFS(B[i]);
    }
    if (now == B[i] && visited[A[i] - 1] == 0) {
      ans[A[i] - 1] = now;
      visited[A[i] - 1] = 1;
      q.push(A[i]);
      // DFS(A[i]);
    }
  }
  return;
  // cout << q.size() <<endl;
}

int main() {

  cin >> N >> M;
  rep(i, M) cin >> A[i] >> B[i];

  DFS(1);

  while (1) {
    rep(i, q.size()) {
      // cout << q.front() <<endl;
      DFS(q.front());
      q.pop();
    }
    if (q.size() == 0)
      break;
  }

  rep(i, N) {
    if (visited[i] == 0) {
      judge = 0;
      break;
    }
  }

  if (judge) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << ans[i] << endl;
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
vector<int> vv[200005];
bool seen[200005];
queue<int> q;
int from[200005];
void bfs(vector<int> &dist) {
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int node : vv[v]) {
      if (dist[node] != -1)
        continue;
      from[node] = v + 1;
      dist[node] = dist[v] + 1;
      q.push(node);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    x--;
    y--;
    vv[x].push_back(y);
    vv[y].push_back(x);
  }
  vector<int> dist(n, -1);
  dist[0] = 0;
  bfs(dist);
  bool flag = true;
  for (int i = 1; i < n; ++i) {
    if (dist[i] - 1 == dist[from[i] - 1])
      continue;
    flag = false;
    break;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; ++i)
      cout << from[i] << endl;
  } else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
vector<int> vv[200005];
bool seen[200005];
queue<int> q;
int from[200005];
void bfs(vector<int> &dist) {
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int node : vv[v]) {
      if (dist[node] != -1)
        continue;
      from[node] = v + 1;
      dist[node] = dist[v] + 1;
      q.push(node);
    }
  }
}
bool check(vector<int> dist, int n) {
  for (int i = 1; i < n; ++i) {
    int index = i;
    int cnt = 0;
    while (true) {
      cnt++;
      if (from[index] == 1)
        break;
      index = from[index] - 1;
    }
    if (cnt != dist[i])
      return false;
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    x--;
    y--;
    vv[x].push_back(y);
    vv[y].push_back(x);
  }
  vector<int> dist(n, -1);
  dist[0] = 0;
  bfs(dist);
  if (check(dist, n)) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; ++i) {
      cout << from[i] << endl;
    }
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> x(N);
  rep(i, M) {
    ll A, B;
    cin >> A >> B;
    x[A - 1].push_back(B - 1);
    x[B - 1].push_back(A - 1);
  }

  queue<ll> q;
  q.push(0);
  vector<ll> dis(N, -1);
  dis[0] = 0;
  ll pre[N];
  while (!q.empty()) {
    ll a = q.front();
    q.pop();
    for (ll next : x[a]) {
      if (dis[next] == -1) {
        q.push(next);
        dis[next] = dis[a] + 1;
        pre[next] = a + 1;
      }
    }
  }

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


Error: TLE

Fixed submission:

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<bool>> x(N, vector<bool>(N, false));
  rep(i, M) {
    ll A, B;
    cin >> A >> B;
    x[A - 1][B - 1] = true;
    x[B - 1][A - 1] = true;
  }

  queue<ll> q;
  q.push(0);
  vector<ll> dis(N, -1);
  dis[0] = 0;
  ll pre[N];
  while (!q.empty()) {
    ll a = q.front();
    q.pop();
    rep(i, N) {
      if (!x[a][i])
        continue;
      if (dis[i] != -1)
        continue;
      if (x[a][i] && dis[i] == -1) {
        q.push(i);
        dis[i] = dis[a] + 1;
        pre[i] = a + 1;
      }
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; i++) {
    cout << pre[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
// #define NMAX 1000000000
int N, M;
vvi V;

void solve() {

  // cout<<N<<" "<<M<<"\\n";
  // rp(i,0,N){
  // 	cout<<i<<": ";
  // 	for(auto j:V[i]){cout<<j<<" ";}
  // 	puts("");
  // }
  // puts("---");

  vi ans(N, -1);
  queue<int> q;
  q.push(0);
  while (q.size()) {
    int p = q.front();
    q.pop();
    for (auto c : V[p]) {
      if (c == 0)
        continue;
      if (ans[c] == -1) {
        // cout<<"p "<<p;
        // cout<<" -> c "<<c;
        // cout<<"\\n";

        ans[c] = p;
        q.push(c);
      }
    }
  }

  // rp(i, 0, N){cout<< ans[i] <<endl;}

  puts("Yes");
  rp(i, 1, N) {
    // cout<<i<<" ";///
    cout << ans[i] + 1 << endl;
  }
}

int main() {
  cin >> N >> M;
  V.resize(N);
  rp(i, 0, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    V[a].push_back(b);
    V[b].push_back(a);
  }
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
// #define NMAX 1000000000
int N, M;
vvi V;

void solve() {

  // cout<<N<<" "<<M<<"\\n";
  // rp(i,0,N){
  // 	cout<<i<<": ";
  // 	for(auto j:V[i]){cout<<j<<" ";}
  // 	puts("");
  // }
  // puts("---");

  vi ans(N, -1);
  queue<int> q;
  q.push(0);
  while (q.size()) {
    int p = q.front();
    q.pop();

    for (auto c : V[p]) {
      if (c == 0)
        continue;
      if (ans[c] == -1) {
        // cout<<"p "<<p;
        // cout<<" -> c "<<c;
        // cout<<"\\n";

        ans[c] = p;
        q.push(c);
      }
    }
  }

  // rp(i, 0, N){cout<< ans[i] <<endl;}

  puts("Yes");
  rp(i, 1, N) {
    // cout<<i<<" ";///
    cout << ans[i] + 1 << endl;
  }
}

int main() {
  cin >> N >> M;
  V.resize(M);
  rp(i, 0, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    V[a].push_back(b);
    V[b].push_back(a);
  }
  solve();
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> v[100001] = {};
vector<int> vs(100001, -1);

void bfs() {
  int s = 1;
  vs[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    int num = (int)v[t].size();
    for (int i = 0; i < num; i++) {
      if (vs[v[t][i]] == -1) {
        q.push(v[t][i]);
        vs[v[t][i]] = t;
      }
    }
  }
  return;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v[A].push_back(B);
    v[B].push_back(A);
  }
  bfs();
  for (int i = 2; i <= N; i++) {
    if (vs[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << vs[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

vector<int> v[10001] = {};
vector<int> vs(10001, -1);

void bfs() {
  int s = 1;
  vs[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    int num = (int)v[t].size();
    for (int i = 0; i < num; i++) {
      if (vs[v[t][i]] == -1) {
        q.push(v[t][i]);
        vs[v[t][i]] = t;
      }
    }
  }
  return;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v[A].push_back(B);
    v[B].push_back(A);
  }
  bfs();
  for (int i = 2; i <= N; i++) {
    if (vs[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << vs[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using Graph = vector<vector<int>>;

int main() {
  lint n, m;
  cin >> n >> m;
  vector<lint> a(m), b(m);
  REP(i, m) { cin >> a[i] >> b[i]; }
  lint V = n;
  vector<vector<int>> vec(n);
  // vector<queue<int>> vec(n);
  // REP(i,n)vec.push_back(queue<int>()); // add a queue
  REP(i, m) {
    vec[a[i] - 1].push_back(b[i] - 1);
    vec[b[i] - 1].push_back(a[i] - 1);
  }
  queue<int> q;
  q.push(0);
  vector<lint> used(n), dist(n), pre(n);
  REP(i, n) {
    lint now = q.front();
    q.pop();
    REP(i, SZ(vec[now])) {
      int x = vec[now][i];
      if (used[x] == 0) {
        q.push(x);
        used[x] = 1;
        dist[x] = i + 1;
        pre[x] = now;
      }
    }
    if (q.empty()) {
      break;
    }
  }

  cout << "Yes"
       << "\\n";
  FOR(i, 1, n) { cout << pre[i] + 1 << "\\n"; }

  return 0;
}

Error: TLE

Fixed submission:

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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  lint n, m;
  cin >> n >> m;
  vector<lint> a(m), b(m);
  REP(i, m) { cin >> a[i] >> b[i]; }
  lint V = n;
  vector<vector<int>> d(n, vector<int>(n));
  vector<queue<int>> vec(n);
  // REP(i,n)vec.push_back(queue<int>()); // add a queue
  REP(i, m) {
    vec[a[i] - 1].push(b[i] - 1);
    vec[b[i] - 1].push(a[i] - 1);
  }
  queue<int> q;
  q.push(0);
  vector<lint> used(n), dist(n), pre(n);
  REP(i, n) {
    lint now = q.front();
    q.pop();
    while (!vec[now].empty()) {
      int x = vec[now].front();
      vec[now].pop();
      if (used[x] == 0) {
        q.push(x);
        used[x] = 1;
        dist[x] = i + 1;
        pre[x] = now;
      }
    }
    if (q.empty()) {
      break;
    }
  }

  cout << "Yes"
       << "\\n";
  FOR(i, 1, n) { cout << pre[i] + 1 << "\\n"; }

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

#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  bool visited[n] = {false};
  int refs[n];
  vector<vector<int>> graph(n);
  int i, j, a, b;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
  }
  visited[0] = true;
  vector<int> v;
  v.push_back(0);
  for (i = 0; i < n; i++) {
    for (j = 0; j < graph[v[i]].size(); j++) {
      // cout << i << "," << graph[v[i]][j] << endl;
      if (!visited[graph[v[i]][j]]) {
        visited[graph[v[i]][j]] = true;
        v.push_back(graph[v[i]][j]);
        refs[graph[v[i]][j]] = v[i];
      }
    }
  }
  cout << "Yes" << endl;
  for (i = 1; i < n; i++) {
    cout << refs[i] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int cost[n];
  int refs[n];
  bool links[n][n];
  int i, j, a, b;
  for (i = 0; i < n; i++) {
    cost[i] = n + 1;
    refs[i] = -1;
    for (j = 0; j < n; j++)
      links[i][j] = false;
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    links[a - 1][b - 1] = true;
    links[b - 1][a - 1] = true;
  }
  cost[0] = 0;
  vector<int> v;
  v.push_back(0);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if ((links[v[i]][j]) && (cost[j] == n + 1)) {
        cost[j] = 0;
        v.push_back(j);
        refs[j] = v[i];
      }
    }
  }
  cout << "Yes" << endl;
  for (i = 1; i < n; i++) {
    cout << refs[i] + 1 << endl;
  }
  return 0;
}