alexjercan/bugnet huggingface dataset:
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 ll = long long;
using namespace std;
const int INF = 1e9;

struct edge {
  int to;
  int cost;
};
using P = pair<int, int>;
vector<vector<edge>> G;
vector<int> d;
vector<int> prever;
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d.begin(), d.end(), INF);
  fill(prever.begin(), prever.end(), -1);
  d[s] = 0;

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

vector<int> get_path(int t) {
  vector<int> path;
  int cnt = 0;
  for (; t != -1 && cnt != 2; t = prever[t]) {
    path.push_back(t);
    ++cnt;
  }
  return path;
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  d.resize(n);
  prever.resize(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;

    G[a].push_back({b, 1});
    G[b].push_back({a, 1});
  }

  dijkstra(0);
  vector<int> ans(n);
  bool flag = true;
  rep(i, n) {
    if (d[i] == INF) {
      flag = false;
      break;
    } else {
      vector<int> tmp = get_path(i);
      ans[i] = tmp[1] + 1;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    rep(i, n - 1) cout << ans[i + 1] << "\\n";
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

struct edge {
  int to;
  int cost;
};
using P = pair<int, int>;
vector<vector<edge>> G;
vector<int> d;
vector<int> prever;
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d.begin(), d.end(), INF);
  fill(prever.begin(), prever.end(), -1);
  d[s] = 0;

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

vector<int> get_path(int t) {
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }
  return path;
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  d.resize(n);
  prever.resize(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;

    G[a].push_back({b, 1});
    G[b].push_back({a, 1});
  }

  dijkstra(0);
  vector<int> ans(n);
  bool flag = true;
  rep(i, n) {
    if (d[i] == INF) {
      flag = false;
      break;
    } else {
      vector<int> tmp = get_path(i);
      ans[i] = tmp[1] + 1;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    rep(i, n - 1) cout << ans[i + 1] << "\\n";
  } else {
    cout << "No" << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<vector<int>> e(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].emplace_back(b);
    e[b].emplace_back(a);
  }
  vector<int> nx(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto &&i : e[t]) {
      if (nx[i] != -1) {
        continue;
      }
      nx[i] = t;
      q.push(i);
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    if (nx[i] == -1) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << nx[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<vector<int>> e(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].emplace_back(b);
    e[b].emplace_back(a);
  }
  vector<int> nx(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto &&i : e[t]) {
      if (nx[i] != -1) {
        continue;
      }
      nx[i] = t;
      q.push(i);
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    if (nx[i] == -1) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << nx[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const string YES = "Yes";

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<long long> G[N + 1];
  for (int i = 0; i < M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }

  vector<int> vec(N + 1);
  queue<int> queue;

  vec[1] = -1;
  int count = 0;
  queue.push(1);
  while (!queue.empty()) {
    int v = queue.front();
    queue.pop();
    for (int u : G[v]) {
      // printf("%d -> %d\\n", v, u);
      if (!vec[u]) {
        vec[u] = v;
        count += 1;
        queue.push(u);
      }
    }
  }
  if (count == N - 1) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << vec[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

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

const string YES = "Yes";

typedef pair<long long, long long> Pair;

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<long long> G[N + 1];
  for (int i = 0; i < M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }

  vector<int> vec(N + 1);
  queue<int> queue;
  set<Pair> seen;

  vec[1] = -1;
  int count = 0;
  queue.push(1);
  while (!queue.empty()) {
    int v = queue.front();
    queue.pop();
    for (int u : G[v]) {
      if (seen.count(Pair(u, v)) || seen.count(Pair(v, u))) {
        continue;
      }
      seen.insert(Pair(v, u));
      // printf("%d -> %d\\n", v, u);
      if (!vec[u]) {
        vec[u] = v;
        count += 1;
      }
      queue.push(u);
    }
  }
  if (count == N - 1) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << vec[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define SIZE(a) int((a).size())
const int INF = INT_MAX;
const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  vvll g(n, vll(0));
  vb v(n, false);
  vll ans(n, -1);
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<P> q;
  v[0] = true;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    P tmp = q.front();
    q.pop();
    ll x = tmp.first, y = tmp.second;
    ans[x] = y + 1;
    for (ll i : g[x]) {
      if (v[i])
        continue;
      q.push(make_pair(i, x));
      v[i] = true;
    }
  }
  bool ok = true;
  rep(i, 1, n) {
    if (ans[i] == -1) {
      ok = false;
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    rep(i, 1, n) { cout << ans[i] << endl; }
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define SIZE(a) int((a).size())
const int INF = INT_MAX;
const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  vvb g(n, vb(n, false));
  vb v(n, false);
  vll ans(n, -1);
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a][b] = true;
    g[b][a] = true;
  }
  queue<P> q;
  v[0] = true;
  rep(i, 0, n) {
    if (g[0][i]) {
      q.push(make_pair(i, 0));
      g[i][0] = false;
      g[0][i] = false;
      v[i] = true;
    }
  }
  while (!q.empty()) {
    P tmp = q.front();
    q.pop();
    ll x = tmp.first, y = tmp.second;
    ans[x] = y + 1;
    rep(i, 0, n) {
      if (v[i])
        continue;
      if (!g[x][i])
        continue;
      q.push(make_pair(i, x));
      v[i] = true;
    }
  }
  bool ok = true;
  rep(i, 1, n) {
    if (ans[i] == -1) {
      ok = false;
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    rep(i, 1, n) { cout << ans[i] << endl; }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
static const int INF = 100001;

int main() {
  int N, M, a, b;
  cin >> N >> M;
  int D[N];
  int B[N];
  vector<int> G[N];
  for (int i = 0; i < N; i++) {
    D[i] = INF;
    B[i] = -1;
  }

  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  queue<int> Q;
  D[0] = 0;
  Q.push(0);
  int u;

  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (D[v] != INF)
        continue;
      D[v] = D[u] + 1;
      B[v] = u;
      Q.push(v);
    }
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
static const int INF = 1000000;

int main() {
  int N, M, a, b;
  cin >> N >> M;
  int D[N];
  int B[N];
  int L[N][N];
  for (int i = 0; i < N; i++) {
    D[i] = INF;
    B[i] = -1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      L[i][j] = 0;
    }
  }

  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    L[a][b] = 1;
    L[b][a] = 1;
  }
  queue<int> Q;
  D[0] = 0;
  Q.push(0);
  int u;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int v = 0; v < N; v++) {
      if (L[u][v] == 0)
        continue;
      if (D[v] != INF)
        continue;
      D[v] = D[u] + 1;
      B[v] = u;
      Q.push(v);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << B[i] + 1 << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

// You tube解説放送
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;

vector<int> to[100005];

int main() {
  int n, m;
  cin >> n >> m;
  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);
  }
  // dist と preを求める BFS
  vector<int> dist(n, -1); // キョリ
  vector<int> pre(n, -1);  // 直前の頂点
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int u : to[now]) {
      if (dist[u] != -1)
        continue;
      dist[u] = dist[now] + 1;
      pre[u] = now;
      que.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  vector<int> ans(n - 1);
  for (int i = 0; i < n - 1; i++) {
    ans[i] = -1;
  }
  queue<int> temp;
  temp.push(1);
  while (temp.size() != 0) {
    for (int i = 0; i < m; i++) {
      if (a[i] == temp.front() && ans[b[i] - 2] == -1) {
        ans[b[i] - 2] = temp.front();
        temp.push(b[i]);
      } else if (b[i] == temp.front() && ans[a[i] - 2] == -1) {
        ans[a[i] - 2] = temp.front();
        temp.push(a[i]);
      }
    }
    temp.pop();
  }
  int cnt = 0;
  for (int i = 0; i < n - 1; i++) {
    if (ans.at(i) == -1)
      cnt++;
  }
  if (cnt == 0) {
    cout << "Yes" << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << ans.at(i) << endl;
    }
  } else {
    cout << "No" << endl;
  }

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> graph(n + 1, vector<int>(0));
  graph.at(0) = {1};
  graph.at(1) = {0};
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    graph.at(x).push_back(y);
    graph.at(y).push_back(x);
  }
  vector<int> ans(n + 1, -1);
  ans.at(0) = 0;
  ans.at(1) = 0;
  queue<int> search;
  search.push(1);
  while (search.size() > 0) {
    int a = search.front();
    // cout << ans.at(0) << ans.at(1) << ans.at(2) << ans.at(3) << ans.at(4) <<
    // endl;
    vector<int> b = graph.at(a);
    // cout << b.size() << endl;
    for (int i = 0; i < b.size(); i++) {
      if (ans.at(b.at(i)) == -1) {
        ans.at(b.at(i)) = a;
        search.push(b.at(i));
      }
    }
    search.pop();
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans.at(i) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> ab(m, vector<int>(2));
  for (size_t i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    ab.at(i).at(0) = x;
    ab.at(i).at(1) = y;
  }
  sort(ab.begin(), ab.end());
  vector<int> p(n + 1, -1);
  p.at(0) = 0;
  p.at(1) = 0;
  queue<int> search;
  search.push(1);
  while (ab.size() >= 1 && search.size() >= 1) {
    int x = search.front();
    for (int i = ab.size() - 1; i >= 0; i--) {
      int y;
      if (ab.at(i).at(0) == x)
        y = ab.at(i).at(1);
      else if (ab.at(i).at(1) == x)
        y = ab.at(i).at(0);
      else
        continue;
      ab.erase(ab.begin() + i);
      if (p.at(y) != -1)
        continue;
      search.push(y);
      p.at(y) = x;
    }
    search.pop();
  }
  for (size_t i = 2; i <= n; i++) {
    if (p.at(i) == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (size_t i = 2; i <= n; i++)
    cout << p.at(i) << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
int dis[200010];
int main() {
  int n, m;
  while (cin >> n >> m) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      dis[i] = n + 10;
    }
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    queue<pair<int, int>> q;
    q.push(mk(0, 1));
    pair<int, int> tmp;
    while (!q.empty()) {
      tmp = q.front();
      q.pop();
      if (dis[tmp.sc] <= tmp.fs) {
        continue;
      }
      dis[tmp.sc] = tmp.fs;
      for (auto it : a[tmp.sc]) {
        q.push(mk(tmp.fs + 1, it));
      }
    }
    // for(int i=1; i<=n; ++i){
    // 	b[dis[i]].push_back(i);
    // }
    printf("Yes\\n");
    for (int i = 1; i <= n; ++i) {
      for (auto it : a[i]) {
        if (dis[it] < dis[i]) {
          printf("%d\\n", it);
          break;
        }
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
int dis[200010];
int main() {
  int n, m;
  while (cin >> n >> m) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      dis[i] = n + 10;
    }
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    queue<pair<int, int>> q;
    q.push(mk(0, 1));
    pair<int, int> tmp;
    while (!q.empty()) {
      tmp = q.front();
      q.pop();
      if (dis[tmp.sc] < tmp.fs) {
        continue;
      }
      dis[tmp.sc] = tmp.fs;
      for (auto it : a[tmp.sc]) {
        q.push(mk(tmp.fs + 1, it));
      }
    }
    // for(int i=1; i<=n; ++i){
    // 	b[dis[i]].push_back(i);
    // }
    printf("Yes\\n");
    for (int i = 1; i <= n; ++i) {
      for (auto it : a[i]) {
        if (dis[it] < dis[i]) {
          printf("%d\\n", it);
          break;
        }
      }
    }
  }
  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;

using ll = long long int;
using P = pair<int, int>;

int main() {
  // Input
  int N, M;
  vector<int> A, B;

  cin >> N >> M;
  A = vector<int>(M);
  B = vector<int>(M);
  rep(i, M) cin >> A[i] >> B[i];

  // Process
  vector<vector<int>> nexts(N, vector<int>());
  rep(i, M) {
    nexts[A[i] - 1].emplace_back(B[i] - 1);
    nexts[B[i] - 1].emplace_back(A[i] - 1);
  }

  vector<int> ans(N, -1);
  queue<int> room_queue;
  ans[0] = 0;
  room_queue.push(0);

  while (!room_queue.empty()) {
    int room = room_queue.front();
    room_queue.pop();

    for (int i : nexts[room]) {
      if (ans[i] == -1) {
        room_queue.push(i);
        ans[i] = room + 1;
      }
    }
  }

  // Output
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] << endl;

  return 0;
}


Error: -11

Fixed submission:

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

using namespace std;

using ll = long long int;
using P = pair<int, int>;

int main() {
  // Input
  int N, M;
  vector<int> A, B;

  cin >> N >> M;
  A = vector<int>(M);
  B = vector<int>(M);
  rep(i, M) cin >> A[i] >> B[i];

  // Process
  list<P> links;
  rep(i, M) links.push_back(make_pair(A[i], B[i]));

  vector<int> ans(N, -1);
  queue<int> room_queue;
  ans[0] = 0;
  room_queue.push(1);

  while (!room_queue.empty()) {
    int room = room_queue.front();
    room_queue.pop();

    for (auto it = links.begin(); it != links.end(); ++it) {
      P link = *it;
      if (link.first == room) {
        if (ans[link.second - 1] == -1) {
          room_queue.push(link.second);
          ans[link.second - 1] = room;
        }
        links.erase(it);
      } else if (link.second == room) {
        if (ans[link.first - 1] == -1) {
          room_queue.push(link.first);
          ans[link.first - 1] = room;
        }
        links.erase(it);
      }
    }
  }

  int cnt = N;
  rep(i, N) if (ans[i] != -1) cnt--;

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

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

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

void bfs(vector<vector<int>> &G, vector<int> &dist, vector<int> &prev,
         int start) {
  dist[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int next_v : G[v]) {
      if (dist[next_v] != -1)
        continue;
      dist[next_v] = dist[v] + 1;
      prev[next_v] = v;
      q.push(next_v);
    }
  }
}

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

  vector<int> dist(N, -1), prev(N, -1);
  bfs(G, dist, prev, 0);

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

Error: 0

Fixed submission:

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

void bfs(vector<vector<int>> &G, vector<int> &dist, vector<int> &prev,
         int start) {
  dist[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int next_v : G[v]) {
      if (dist[next_v] != -1)
        continue;
      dist[next_v] = dist[v] + 1;
      prev[next_v] = v;
      q.push(next_v);
    }
  }
}

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

  vector<int> dist(N, -1), prev(N, -1);
  bfs(G, dist, prev, 0);

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

#include <bits/stdc++.h>
#define ll long long int
#define pr pair<ll, ll>
#define infinity (ll)4e18
#define tp tuple<ll, ll, ll>
using namespace std;
long long int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m, a, b, node, dist, parent;
  cin >> n >> m;
  vector<ll> v[n + 1];
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ll par[n + 1], vis[n + 1];
  fill(par, par + n + 1, infinity);
  fill(vis, vis + n + 1, 0);
  queue<tp> q;
  q.emplace(1, 0, -1);
  vis[1] = 1;
  while (!q.empty()) {
    tp x = q.front();
    q.pop();
    tie(node, dist, parent) = x;
    if (par[node] == infinity)
      par[node] = parent;
    for (ll i = 0; i < (ll)v[node].size(); i++) {
      if (par[v[node][i]] == infinity && vis[v[node][i]] == 0) {
        q.emplace(v[node][i], dist + 1, node);
        vis[v[node][i]] = 1;
      }
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (par[i] == infinity) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (ll i = 2; i <= n; i++)
    cout << par[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pr pair<ll, ll>
#define infinity (ll)4e18
#define tp tuple<ll, ll, ll>
using namespace std;
long long int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m, a, b, node, dist, parent;
  cin >> n >> m;
  vector<ll> v[n + 1];
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ll par[n + 1];
  fill(par, par + n + 1, infinity);
  queue<tp> q;
  q.emplace(1, 0, -1);
  while (!q.empty()) {
    tp x = q.front();
    q.pop();
    tie(node, dist, parent) = x;
    if (par[node] == infinity)
      par[node] = parent;
    for (ll i = 0; i < (ll)v[node].size(); i++) {
      if (par[v[node][i]] == infinity)
        q.emplace(v[node][i], dist + 1, node);
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (par[i] == infinity) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (ll i = 2; i <= n; i++)
    cout << par[i] << endl;
  return 0;
}