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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 500100
int n, m, cnt[MAXN], a[MAXN], dp[MAXN], flag[MAXN], cnt2;
struct node {
  int v;
  node *next;
} pool[2 * MAXN], *h[MAXN];
void addedge(int u, int v) {
  node *p = &pool[++cnt2], *q = &pool[++cnt2];
  p->v = v;
  p->next = h[u];
  h[u] = p;
  q->v = u;
  q->next = h[v];
  h[v] = q;
}
queue<int> Q;
void bfs() {
  Q.push(1);
  int u, v;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    flag[u] = 0;
    for (node *p = h[u]; p; p = p->next) {
      if (dp[p->v] == 0 || cnt[p->v] > cnt[u] + 1) {
        dp[p->v] = u;
        cnt[p->v] = cnt[u] + 1;
        if (flag[p->v] == 0)
          Q.push(p->v), flag[p->v] = 1;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int u, v;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
  }
  dp[1] = 1;
  bfs();
  for (int i = 2; i <= n; i++)
    if (dp[i] == 0) {
      printf("No\\n");
      return 0;
    }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++)
    printf("%d\\n", dp[i]);

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 100100
int n, m, cnt[MAXN], a[MAXN], dp[MAXN], flag[MAXN], cnt2;
struct node {
  int v;
  node *next;
} pool[2 * MAXN], *h[MAXN];
void addedge(int u, int v) {
  node *p = &pool[++cnt2], *q = &pool[++cnt2];
  p->v = v;
  p->next = h[u];
  h[u] = p;
  q->v = u;
  q->next = h[v];
  h[v] = q;
}
queue<int> Q;
void bfs() {
  Q.push(1);
  int u, v;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    flag[u] = 0;
    for (node *p = h[u]; p; p = p->next) {
      if (dp[p->v] == 0 || cnt[p->v] > cnt[u] + 1) {
        dp[p->v] = u;
        cnt[p->v] = cnt[u] + 1;
        if (flag[p->v] == 0)
          Q.push(p->v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int u, v;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
  }
  bfs();
  for (int i = 1; i <= n; i++)
    if (dp[i] == 0) {
      printf("No\\n");
      return 0;
    }
  for (int i = 2; i <= n; i++)
    printf("%d\\n", dp[i]);

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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int n, m;
vector<vector<int>> g;
vector<int> ans, len;
vector<bool> visited;

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

  cin >> n >> m;
  g.resize(n);
  visited.resize(n, false);
  len.resize(n, 1e9);
  ans.resize(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    g[a].push_back(b);
    if (a != 0)
      g[b].push_back(a);
    else {
      len[b] = 1;
      ans[b] = 1;
    }
  }

  queue<int> q;
  vector<int> shortest_route(n, 1e9);
  q.push(0);
  len[0] = 0;
  shortest_route[0] = 0;
  while (!q.empty()) {
    int z = q.front();
    q.pop();
    visited[z] = true;
    for (auto x : g[z]) {
      if (visited[x])
        continue;
      if (shortest_route[x] > shortest_route[z] + 1) {
        shortest_route[x] = shortest_route[z] + 1;
        ans[x] = z;
        q.push(x);
      }
    }
  }

  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int n, m;
vector<vector<int>> g;
vector<int> ans, len;

void dfs(int node) {
  for (auto gg : g[node]) {
    if (len[gg] > len[node]) {
      len[gg] = len[node] + 1;
      ans[gg] = node;
    } else
      continue;
    dfs(gg);
  }
}

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

  cin >> n >> m;
  g.resize(n);
  len.resize(n, 1e9);
  ans.resize(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    g[a].push_back(b);
    if (a != 0)
      g[b].push_back(a);
  }

  len[0] = 0;
  dfs(0);

  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<vector<int>> G(100030);

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  int level[100030] = {};
  queue<int> Q;
  Q.push(1);
  while (Q.size()) {
    int v = Q.front();
    Q.pop();
    for (auto i : G[v]) {
      if (level[i] == 0) {
        Q.push(i);
        level[i] = v;
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i)
    cout << level[i] << '\\n';
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<vector<int>> G;
vector<bool> u;
vector<int> level(100005, 0);

void bfs(int x) {
  queue<int> Q;
  Q.push(x);
  u[x] = true;
  level[x] = x;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    //      u[v]=true;
    for (auto i : G[v]) {
      if (!u[i]) {
        level[i] = v;
        Q.push(i);
        u[i] = true;
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(m);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  u.resize(n + 1, 0);
  bfs(1);
  for (int i = 1; i <= n; ++i) {
    if (u[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= n; ++i)
    cout << level[i] << '\\n';
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

bool vis[100005];
pair<int, int> dist[100005];
const int INF = 1e9;

void bfs(vector<vector<int>> graph) {
  dist[0].first = 0;
  vis[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : graph[v]) {
      if (!vis[u]) {
        vis[u] = true;
        dist[u].first = dist[v].first + 1;
        dist[u].second = v + 1;
        q.push(u);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> cave(n, vector<int>());
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    cave[u - 1].push_back(v - 1);
    cave[v - 1].push_back(u - 1);
  }
  bfs(cave);
  // cout << "hmm";
  bool possible = true;
  /*for (int i = 0; i < n; ++i) {
      if (!vis[i]) {
          possible = false;
          break;
      }
  }*/
  if (possible) {
    cout << "Yes\\n";
    for (int i = 1; i < n; ++i) {
      cout << dist[i].second << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

bool vis[10005];
pair<int, int> dist[10005];
const int INF = 1e9;

void bfs(vector<vector<int>> graph) {
  dist[0].first = 0;
  vis[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : graph[v]) {
      if (!vis[u]) {
        vis[u] = true;
        dist[u].first = dist[v].first + 1;
        dist[u].second = v + 1;
        q.push(u);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> cave(n, vector<int>());
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    cave[u - 1].push_back(v - 1);
    cave[v - 1].push_back(u - 1);
  }
  bfs(cave);
  // cout << "hmm";
  bool possible = true;
  /*for (int i = 0; i < n; ++i) {
      if (!vis[i]) {
          possible = false;
          break;
      }
  }*/
  if (possible) {
    cout << "Yes\\n";
    for (int i = 1; i < n; ++i) {
      cout << dist[i].second << '\\n';
    }
  }

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

#include <iostream>
#include <queue>
#include <vector>
#define INF 1000000000
using namespace std;

// to[u] = v : uからvへの道がある
vector<int> to[100001];

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

  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  queue<int> q;

  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < to[v].size(); i++) {
      int u = to[v][i];
      if (dist[u] != INF) {
        continue;
      }
      dist[u] = dist[v] + 1;
      q.push(u);
      pre[u] = v;
    }
  }

  cout << "Yes" << endl;

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

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define INF 1e+9
#define MAX_V 10

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  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;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e = {b, c};
    G[a].push_back(e);
  }
  dijkstra(0);
  for (int i = 0; i < V; ++i) {
    if (d[i] != INF)
      cout << d[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void YN(bool flg) {
  if (flg)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void Yn(bool flg) {
  if (flg)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void yn(bool flg) {
  if (flg)
    cout << "yes" << endl;
  else
    cout << "no" << endl;
}

const int INF = 1001001001;
vi v[100005];

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

  queue<int> q;
  q.push(0);

  vi seen(n, INF);
  vi sign(n, -1);
  seen[0] = 0;
  while (!q.empty()) {
    int prev = q.front();
    q.pop();
    for (int u : v[prev]) {
      if (seen[u] != INF)
        continue;
      seen[u] = seen[prev] + 1;
      sign[u] = prev;
      q.push(u);
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << sign[i] + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void YN(bool flg) {
  if (flg)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void Yn(bool flg) {
  if (flg)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void yn(bool flg) {
  if (flg)
    cout << "yes" << endl;
  else
    cout << "no" << endl;
}

const int INF = 1001001001;

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

  queue<int> q;
  q.push(0);

  vi seen(n, INF);
  vi sign(n, -1);
  seen[0] = 0;
  while (!q.empty()) {
    int prev = q.front();
    q.pop();
    for (int u : v[prev]) {
      if (seen[u] != INF)
        continue;
      seen[u] = seen[prev] + 1;
      sign[u] = prev;
      q.push(u);
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << sign[i] + 1 << 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;
  int a, b;
  vector<int> samp;
  vector<vector<int>> v(n + 1, samp);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vector<bool> visited(n, false);
  queue<int> level;
  level.push(1);
  visited[1] = true;
  int si = 0;
  int le = 0;
  vector<int> res(n + 1);
  while (!level.empty()) {
    si = level.size();
    while (si--) {
      int x = level.front();
      level.pop();
      int c = v[x].size();
      for (int j = 0; j < c; j++) {
        if (!visited[v[x][j]]) {
          visited[v[x][j]] = true;
          level.push(v[x][j]);
          res[v[x][j]] = x;
        }
      }
    }
    le++;
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++) {
    cout << res[i] << endl;
  }
}

Error: 0

Fixed submission:

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

// ifstream cin ("a.txt");
// ofstream cout ("a.out");

int main() {
  int n, m;
  int a, b;
  cin >> n >> m;
  vector<int> samp;
  vector<vector<int>> conn(n + 1, samp);
  // load adajency matrix
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    conn[a].push_back(b);
    conn[b].push_back(a);
  }
  bool visited[n + 1];
  for (int i = 0; i < n + 1; i++) {
    visited[i] = false;
  }
  queue<int> level;
  level.push(1);
  visited[1] = true;
  int si = 0;
  int le = 0;
  vector<int> res(n);

  while (!level.empty()) {
    si = level.size();
    while (si--) {
      int x = level.front();
      level.pop();

      int c = conn[x].size();
      for (int j = 0; j < c; j++) {
        if (!visited[conn[x][j]]) {
          visited[conn[x][j]] = true;
          level.push(conn[x][j]);
          res[conn[x][j]] = x;
        }
      }
    }

    le++;
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++) {
    cout << res[i] << endl;
  }
}
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;
using P = pair<int, int>;
const int INF = 1001001001;

vector<int> to[100005];

int main() {
  int N, M;
  cin >> N >> M;
  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    to[A].push_back(B);
    to[B].push_back(A);
  }

  queue<int> q;
  vector<int> dist(N, INF); // distance 今回はいらない
  vector<int> pre(N, -1);   // 直前の頂点prev
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }

  cout << "Yes" << endl;
  rep(i, N) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取 (ここでは無向グラフを想定)
  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // 頂点 0 をスタートとした探索
  seen.assign(N, false); // 全頂点を「未訪問」に初期化
  dfs(G, 0);

  // t に辿り着けるかどうか
  if (seen[0])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
vector<int> G[100010];
queue<pair<int, int>> que;
int pre[100010];
void bfs(int s) {
  que.push({s, 0});
  while (que.size()) {
    auto p = que.front();
    que.pop();
    if (pre[p.first] != -1)
      continue;
    pre[p.first] = p.second;
    for (int v : G[p.first]) {
      if (v == p.second)
        continue;
      que.push({v, p.first});
    }
  }
}

int main() {
  int i, n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (i = 0; i < n; i++) {
    pre[i] = -1;
  }
  bfs(0);
  cout << "Yes" << endl;
  for (i = 1; i < n; i++) {
    cout << pre[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
vector<int> G[100010];
queue<pair<int, int>> que;
int pre[100010];
void bfs(int s) {
  que.push({s, 0});
  while (que.size()) {
    auto p = que.front();
    que.pop();
    if (pre[p.first] != -1)
      continue;
    pre[p.first] = p.second;
    for (int v : G[p.first]) {
      if (v == p.second)
        continue;
      que.push({v, p.first});
    }
  }
}

int main() {
  int i, n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
    pre[i] = -1;
  }
  bfs(0);
  cout << "Yes" << endl;
  for (i = 1; i < n; i++) {
    cout << pre[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>;

const int INF = 1001001001;
vector<int> to[100005];

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

  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rep(i, m) { cin >> a[i] >> b[i]; }

  vector<int> root(n, -1);
  root[0] = 1;
  int not_visit = n - 1;
  queue<int> q;
  q.push(1);
  int current;
  int count = 0;
  while (not_visit > 0 && count < n) {
    current = q.front();
    q.pop();
    for (int i = 0; i < m; i++) {
      if (a[i] == current && root[b[i] - 1] == -1) {
        q.push(b[i]);
        root[b[i] - 1] = current;
        not_visit--;
      } else if (b[i] == current && root[a[i] - 1] == -1) {
        q.push(a[i]);
        root[a[i] - 1] = current;
        not_visit--;
      }
    }
    count++;
  }

  if (count == n) {
    cout << "No" << endl;
  }

  else {
    cout << "Yes" << endl;
    rep(i, n - 1) cout << root[i + 1] << endl;
  }

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

#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using Graph = vector<vector<int>>;

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

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

  bool flag = false;
  rep(i, n) {
    if (dist[i] == -1)
      flag = true;
  }

  if (flag)
    cout << "No" << endl;
  else {
    rep(i, n) {
      if (i == 0)
        cout << "Yes" << endl;
      else
        cout << dist[i] + 1 << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> ab(m * 2, 0);
  rep(i, m) {
    cin >> ab[i * 2];
    cin >> ab[i * 2 + 1];
    ab[i * 2]--;
    ab[i * 2 + 1]--;
  }
  vector<ll> precheck(n, 0);
  precheck[0] = 1;
  vector<ll> check(n, 0);
  check[0] = 1;
  bool flag = false;

  rep(x, n) {
    rep(i, m) {
      if (check[ab[i * 2]] == 0 && check[ab[i * 2 + 1]] != 0) {
        precheck[ab[i * 2]] = ab[i * 2 + 1] + 1;
      } else if (check[ab[i * 2 + 1]] == 0 && check[ab[i * 2]] != 0) {
        precheck[ab[i * 2 + 1]] = ab[i * 2] + 1;
      }
    }
    flag = true;
    rep(i, n) {
      check[i] = precheck[i];
      if (check[i] == 0) {
        flag = false;
      }
    }
    if (flag)
      break;
  }

  if (flag) {
    cout << "Yes" << endl;
    rep(i, n - 1) { cout << check[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;

vector<int> adj[100000];
int n, m, a, b, dist[100000], par[100000];
bool proc[100000];

void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  fill(dist, dist + 100000, 1e9);
  fill(proc, proc + 100000, 0);
  priority_queue<pair<int, int>> q;
  q.push({0, 0});
  dist[0] = 0;
  while (!q.empty()) {
    int cur = q.top().second;
    q.pop();
    if (proc[cur])
      continue;
    proc[cur] = 1;
    for (int nbr : adj[cur]) {
      if (dist[cur] + 1 < dist[nbr]) {
        dist[nbr] = dist[cur] + 1;
        par[nbr] = cur;
        q.push({-dist[nbr], nbr});
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << par[i] + 1 << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> adj[100000];
int n, m, a, b, dist[100000], par[100000];

void dfs(int cur = 0, int curDist = 0, int curPar = 0) {
  if (curDist >= dist[cur])
    return;
  dist[cur] = curDist;
  par[cur] = curPar;
  for (int i : adj[cur]) {
    dfs(i, curDist + 1, cur);
  }
}

void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  fill(dist, dist + 100000, 1e9);
  dfs();
  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << par[i] + 1 << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}