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

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

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

const ll MAX_N = 100005;
const ll INF = 1e10;
ll N, M;
vector<ll> G[MAX_N];
ll d[MAX_N];
ll pre[MAX_N];

void dijkstra(ll s) {
  fill(d, d + N, INF);
  fill(pre, pre + N, -1);
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (ll i = 0; i < G[v].size(); i++) {
      ll u = G[v][i];
      if (d[u] > d[v] + 1) {
        d[u] = d[v] + 1;
        que.push(P(d[u], u));
        pre[u] = v;
      }
    }
  }
}

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

  dijkstra(0);

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

Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

const ll MAX_N = 100005;
const ll INF = 1e10;
ll N, M;
vector<ll> G[MAX_N];
ll d[MAX_N];
bool used[MAX_N];
ll pre[MAX_N];

void dijkstra(ll s) {
  fill(d, d + N, INF);
  fill(used, used + N, false);
  fill(pre, pre + N, -1);
  d[s] = 0;
  while (true) {
    ll v = -1;
    for (ll u = 0; u < N; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;

    for (ll i = 0; i < G[v].size(); i++) {
      ll u = G[v][i];
      if (d[u] > d[v] + 1) {
        d[u] = d[v] + 1;
        pre[u] = v;
      }
    }
  }
}

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

  dijkstra(0);

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

#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

int c[100005];
vector<int> ab[100005];
queue<int> qu;

signed main(void) {
  int n, m, a, b;
  cin >> n >> m;
  reps(i, 1, m + 1) {
    cin >> a >> b;
    ab[a].push_back(b);
    ab[b].push_back(a);
  }
  qu.push(1);
  c[1] = 1;
  while (!qu.empty()) {
    a = qu.front();
    qu.pop();
    for (int nx : ab[a])
      if (!c[nx])
        qu.push(nx), c[nx] = a;
  }
  reps(i, 1, n + 1) if (!c[i]) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  reps(i, 2, n + 1) cout << c[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

int c[2][100005];
vector<int> ab[100005];

void next(int now, int cnt) {
  cnt++;
  int s = ab[now].size();
  auto it = ab[now].begin();
  rep(i, s) {
    if (c[0][*it] == 0 || c[0][*it] > cnt) {
      c[0][*it] = cnt;
      c[1][*it] = now;
      next(*it, cnt);
    }
    it++;
  }
}

signed main(void) {
  int n, m, a, b;
  cin >> n >> m;
  reps(i, 1, m + 1) {
    cin >> a >> b;
    ab[a].insert(ab[a].end(), b);
    ab[b].insert(ab[b].end(), a);
  }
  next(1, 0);
  reps(i, 1, n + 1) if (c[0][i] == 0) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  reps(i, 2, n + 1) cout << c[1][i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); ++i)

using namespace std;

int main() {
  ll N, M, count, m;

  cin >> N >> M;

  vector<ll> A(M), B(M), X(N, -1), v;
  vector<vector<ll>> Y(N);

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

  rep(i, M) {
    Y[A[i] - 1].push_back(i);
    Y[B[i] - 1].push_back(i);
  }

  v.push_back(1); // 出口まで近い部屋から順に入れていく箱
  X[0] = 0;
  count = 1; // vがいくつ埋まったか
  rep(s, N) {
    if (count == N)
      break;
    rep(i, Y[v[s] - 1].size()) {
      if (count == N)
        break;
      m = Y[v[s] - 1][i];
      if (A[m] == v[s]) {
        if (X[B[m] - 1] == -1) {
          v.push_back(B[m]);
          X[B[m] - 1] = v[s];
          count++;
        }
      } else if (B[m] == v[s]) {
        if (X[A[m] - 1] == -1) {
          v.push_back(A[m]);
          X[A[m] - 1] = v[s];
          count++;
        }
      }
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); ++i)

using namespace std;

int main() {
  ll N, M, count, m;

  cin >> N >> M;

  vector<ll> A(M), B(M), X(N, -1), v;
  vector<vector<ll>> Y(N);

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

  rep(i, N) {
    rep(j, M) {
      if (A[j] == i + 1)
        Y[i].push_back(j);
      if (B[j] == i + 1)
        Y[i].push_back(j);
    }
  }

  v.push_back(1); // 出口まで近い部屋から順に入れていく箱
  X[0] = 0;
  count = 1; // vがいくつ埋まったか
  rep(s, N) {
    if (count == N)
      break;
    rep(i, Y[v[s] - 1].size()) {
      if (count == N)
        break;
      m = Y[v[s] - 1][i];
      if (A[m] == v[s]) {
        if (X[B[m] - 1] == -1) {
          v.push_back(B[m]);
          X[B[m] - 1] = v[s];
          count++;
        }
      } else if (B[m] == v[s]) {
        if (X[A[m] - 1] == -1) {
          v.push_back(A[m]);
          X[A[m] - 1] = v[s];
          count++;
        }
      }
    }
  }

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

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int V, d[200001], r[200001], z[200001];
vector<int> G[200001];
void bfs(int s) {
  rep(i, V + 1) d[i] = 0, r[i] = 0;
  queue<int> que;
  que.push(s);
  r[s] = 1;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (r[nv])
        continue;
      r[nv]++;
      z[nv] = v;
      d[nv] = d[v] + 1;
      que.push(nv);
    }
  }
};
int main() {
  int n, m, a, b;
  cin >> n >> m;
  rep(i, m) cin >> a >> b, G[a].push_back(b), G[b].push_back(a);
  bfs(1);
  cout << "Yes\\n";
  rep(i, n - 1) cout << z[i + 2] << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int V, d[200001], r[200001], z[200001];
vector<int> G[200001];
int bfs(int s) {
  rep(i, V + 1) d[i] = 0, r[i] = 0;
  queue<int> que;
  que.push(s);
  r[s] = 1;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (r[nv])
        continue;
      r[nv]++;
      z[nv] = v;
      d[nv] = d[v] + 1;
      que.push(nv);
    }
  }
};
int main() {
  int n, m, a, b;
  cin >> n >> m;
  rep(i, m) cin >> a >> b, G[a].push_back(b), G[b].push_back(a);
  bfs(1);
  cout << "Yes\\n";
  rep(i, n - 1) cout << z[i + 2] << "\\n";
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vec(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec.at(a).push_back(b);
    vec.at(b).push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : vec.at(v)) {
      if (dist.at(u) != INF)
        continue;
      dist.at(u) = dist.at(v) + 1;
      pre.at(u) = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    cout << ++pre.at(i) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool check[100005] = {};
int before[100005];

bool kiemtra(int u, int j, const vector<int> ds[]) {
  for (auto it = ds[u].begin(); it != ds[u].end(); it++) {
    if (*it == j)
      return true;
  }
  return false;
}

void BFS(int i, int n, vector<int> ds[]) {
  queue<int> q;
  q.push(i);
  check[i] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int j = 1; j <= n; j++) {
      if (!check[j] && kiemtra(u, j, ds)) {
        q.push(j);
        before[j] = u;
        check[j] = true;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  vector<int> ds[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    ds[x].push_back(y);
    ds[y].push_back(x);
  }

  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!check[i]) {
      cnt++;
      BFS(i, n, ds);
    }
    if (cnt == 2) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << before[i] << endl;

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

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

using namespace std;

vector<int> bfs(vector<vector<int>> adj, vector<int> visited, int r) {
  queue<int> que;
  que.push(r);
  visited[r] = 1;

  while (!que.empty()) {
    int s = que.front();
    que.pop();
    // cout<<s<<endl;
    for (int i = 0; i < adj[s].size(); i++) {
      if (visited[adj[s][i]] == 0) {
        que.push(adj[s][i]);
        visited[adj[s][i]] = s;
      }
    }
  }

  return visited;
}

int main() {
  int n, m;

  cin >> n >> m;

  vector<vector<int>> adj(n + 1);
  vector<int> visited(n + 1, 0);

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

  visited = bfs(adj, visited, 1);

  for (int i = 2; i <= n; i++) {
    if (visited[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

vector<int> bfs(vector<vector<int>> adj, vector<int> visited, int r) {
  queue<int> que;
  que.push(r);
  visited[r] = 1;

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

    for (int i = 0; i < adj[s].size(); i++) {
      if (adj[s][i] == 1 && visited[i] == 0) {
        que.push(i);
        visited[i] = s;
      }
    }
  }

  return visited;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> adj(n + 1, vector<int>(n + 1, 0));
  vector<int> visited(n + 1, 0);

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

  visited = bfs(adj, visited, 1);

  bool flag = true;
  ;
  for (int i = 2; i <= n; i++) {
    if (visited[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N + 1);
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    G.at(A).push_back(B);
    G.at(B).push_back(A);
  }
  vector<int> ans(N + 1, INT_MAX);
  vector<int> dist(N + 1, INT_MAX);
  dist.at(1) = 0;
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    for (auto &x : G.at(p)) {
      if (dist.at(x) > dist.at(p) + 1) {
        dist.at(x) = dist.at(p) + 1;
        ans.at(x) = p;
        que.push(x);
      }
    }
  }
  for (int i = 2; i <= N; ++i) {
    if (ans.at(i) == INT_MAX) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i) {
    cout << ans.at(i) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N + 1);
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    G.at(A).push_back(B);
    G.at(B).push_back(A);
  }
  vector<int> dist(N + 1, INT_MAX);
  dist.at(1) = 0;
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    for (auto &x : G.at(p)) {
      if (dist.at(x) > dist.at(p) + 1) {
        dist.at(x) = dist.at(p) + 1;
        que.push(x);
      }
    }
  }

  vector<vector<int>> V(N + 1);
  for (int i = 1; i <= N; ++i) {
    if (dist.at(i) == INT_MAX) {
      cout << "No" << endl;
      exit(0);
    }
    V.at(dist.at(i)).push_back(i);
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i) {
    int d = dist.at(i);
    for (auto &x : V.at(d - 1)) {
      if (find(G.at(i).begin(), G.at(i).end(), x) != G.at(i).end()) {
        cout << x << endl;
        break;
      }
    }
  }

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

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

void func(vector<int> &pos, vector<vector<int>> &r, vector<int> &vis,
          vector<int> &dis, vector<int> &sign) {
  vector<int> next;
  for (int i = 0; i < pos.size(); ++i) {
    vis[pos[i]] = 1;
    for (int j = 0; j < r[pos[i]].size(); ++j) {
      if (vis[r[pos[i]][j]] == 0) {
        next.push_back(r[pos[i]][j]);
        vis[r[pos[i]][j]] = 1;
      }
      if (dis[r[pos[i]][j]] > dis[pos[i]] + 1) {
        dis[r[pos[i]][j]] = dis[pos[i]] + 1;
        sign[r[pos[i]][j]] = pos[i];
      }
    }
  }
  if (next.size() > 0) {
    func(next, r, vis, dis, sign);
  }
}

void Main() {
  int n, m;
  cin >> n >> m;
  // n 部屋の数
  // m 通路の数
  int a, b;
  vector<vector<int>> r(n + 1, vector<int>());
  vector<int> pos(1, 1);           // いま探索の起点にする部屋
  vector<int> vis(n + 1, 0);       // 訪れたかどうか
  vector<int> dis(n + 1, n + 100); // 最短距離
  vector<int> sign(n + 1, 0);      // 最短の隣の部屋
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }

  dis[1] = 0;
  func(pos, r, vis, dis, sign);

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

int main(int argc, char **argv) {
  Main();
  return 0;
}


Error: 0

Fixed submission:

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

void func(int prev, int pos, int len, vector<vector<int>> &r, vector<int> &vis,
          vector<int> &dis, vector<int> &sign) {
  if (vis[pos] == 1 && dis[pos] < len) {
    return;
  } else {
    vis[pos] = 1;
    dis[pos] = len;
    sign[pos] = prev;
  }
  for (vector<int>::iterator i = r[pos].begin(); i != r[pos].end(); ++i) {
    if (vis[*i] == 0 || dis[*i] > len + 1) {
      func(pos, *i, len + 1, r, vis, dis, sign);
    }
  }
}

void Main() {
  int n, m;
  cin >> n >> m;
  // n 部屋の数
  // m 通路の数
  int a, b;
  vector<vector<int>> r(n + 1, vector<int>());
  vector<int> vis(n + 1, 0);       // 訪れたかどうか
  vector<int> dis(n + 1, n + 100); // 最短距離
  vector<int> sign(n + 1, 0);      // 最短の隣の部屋
  vis[1] = 1;
  for (int i = 0; i < m; i++) {
    dis[i + 1] = n + 10;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }

  func(-1, 1, 0, r, vis, dis, sign);

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

int main(int argc, char **argv) {
  Main();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  int N, M;
  cin >> N >> M;
  Graph<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, 1e9);
  vector<int> ans(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  ans[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < 1e9)
        continue;
      dist[nv] = dist[v] + 1;
      ans[nv] = v;
      que.push(nv);
    }
  }

  bool flag = true;
  for (int i = 0; i < N; ++i) {
    if (ans[i] == -1) {
      flag = false;
      break;
    }
  }

  if (!flag)
    cout << "No" << '\\n';
  else {
    cout << "Yes" << '\\n';
    for (int i = 1; i < N; ++i)
      cout << ans[i] + 1 << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

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

  vector<int> dist(N, 1e9);
  vector<int> ans(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  ans[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < 1e9)
        continue;
      dist[nv] = dist[v] + 1;
      ans[nv] = v;
      que.push(nv);
    }
  }

  bool flag = true;
  for (int i = 0; i < N; ++i) {
    if (ans[i] == -1) {
      flag = false;
      break;
    }
  }

  if (!flag)
    cout << "No" << '\\n';
  else {
    cout << "Yes" << '\\n';
    for (int i = 1; i < N; ++i)
      cout << ans[i] + 1 << '\\n';
  }

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

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
using namespace std;

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

  vector<vector<int>> next(N, vector<int>(0));

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    next[a].PB(b);
    next[b].PB(a);
  }

  vector<bool> done(N, false);
  done[0] = true;
  queue<int> will;
  will.push(0);

  vector<int> ans(N, -1);

  while (will.empty() == false) {
    int v = will.front();
    will.pop();

    for (int i = 0; i < next[v].size(); i++) {
      if (done[next[v][i]] == true)
        continue;
      else {
        ans[next[v][i]] = v;
        done[next[v][i]] = true;
        will.push(next[v][i]);
      }
    }
  }

  cout << "Yes" << endl;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
using namespace std;

int main() {
  lint N, M;
  cin >> N >> M;

  vector<vector<lint>> next(N, vector<lint>(0));

  for (lint i = 0; i < M; i++) {
    lint a, b;
    cin >> a >> b;
    a--;
    b--;
    next[a].PB(b);
    next[b].PB(a);
  }

  vector<bool> done(N, false);
  done[0] = true;
  vector<lint> will(1, 0);

  vector<lint> ans(N, -1);

  while (will.size() > 0) {
    lint v = will[0];
    reverse(ALL(will));
    will.pop_back();
    reverse(ALL(will));
    for (lint i = 0; i < next[v].size(); i++) {
      if (done[next[v][i]] == true)
        continue;
      else {
        ans[next[v][i]] = v;
        done[next[v][i]] = true;
        will.PB(next[v][i]);
      }
    }
  }

  cout << "Yes" << endl;

  for (lint i = 1; i <= N - 1; i++) {
    cout << ans[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int nextr[100010];
vector<int> rt[100100];
int record[100010];

int main() {
  int N, M;
  cin >> N >> M;
  /*
  for (int i =1;i<N+1;i++){
      for(int j=1;j<N+1;j++){
          route[i][j]=0;
      }
  }*/
  for (int i = 1; i < N + 1; i++)
    nextr[i] = record[i] = 0;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    //  route[A[i]][B[i]]= route[B[i]][A[i]]=1;
    rt[A[i]].push_back(B[i]);
    rt[B[i]].push_back(A[i]);
  }
  queue<int> q;
  q.push(1);
  record[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (const auto &e : rt[cur]) {
      if (record[e] == 0) {
        nextr[e] = cur;
        record[e] = 1; // このタイミングで訪問済みにする
        q.push(e);
      }
    }

    /*        for(int i=1;i<N+1;i++){
               if(route[cur][i]==1 && record[i]==0){
                   nextr[i]=cur;
                   record[i]=1;//このタイミングで訪問済みにする
                   q.push(i);
               }
            }
            */
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++) {
    cout << nextr[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int nextr[100010];
vector<int> rt[10010];
int record[100010];

int main() {
  int N, M;
  cin >> N >> M;
  /*
  for (int i =1;i<N+1;i++){
      for(int j=1;j<N+1;j++){
          route[i][j]=0;
      }
  }*/
  for (int i = 1; i < N + 1; i++)
    nextr[i] = record[i] = 0;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    //  route[A[i]][B[i]]= route[B[i]][A[i]]=1;
    rt[A[i]].push_back(B[i]);
    rt[B[i]].push_back(A[i]);
  }
  queue<int> q;
  q.push(1);
  record[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (const auto &e : rt[cur]) {
      if (record[e] == 0) {
        nextr[e] = cur;
        record[e] = 1; // このタイミングで訪問済みにする
        q.push(e);
      }
    }

    /*        for(int i=1;i<N+1;i++){
               if(route[cur][i]==1 && record[i]==0){
                   nextr[i]=cur;
                   record[i]=1;//このタイミングで訪問済みにする
                   q.push(i);
               }
            }
            */
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++) {
    cout << nextr[i] << endl;
  }
  return 0;
}