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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> parent(n, -1);
  vector<vector<int>> route(n);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    route[a].emplace_back(b);
    route[b].emplace_back(a);
  }
  queue<int> que;
  que.push(0);
  parent[0] = INFI;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    if (route[now].empty())
      continue;
    int size_route = (int)route[now].size();
    rep(i, size_route) {
      int next = route[now][i];
      if (parent[next] == -1) {
        parent[next] = now;
        que.push(next);
      }
    }
  }

  rep(i, n) {
    if (parent[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent[i] + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> parent(n, -1);
  vector<vector<int>> route(m);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    route[a].emplace_back(b);
    route[b].emplace_back(a);
  }
  queue<int> que;
  que.push(0);
  parent[0] = INFI;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    if (route[now].empty())
      continue;
    int size_route = (int)route[now].size();
    rep(i, size_route) {
      int next = route[now][i];
      if (parent[next] == -1) {
        parent[next] = now;
        que.push(next);
      }
    }
  }

  rep(i, n) {
    if (parent[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent[i] + 1 << endl;
  }

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

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define mod 1000000007;
using namespace std;
vector<ll> adj[1000000];
bool v[1000000];
ll arr[1000000];
queue<ll> num;
void bfs(ll s, vector<ll> adj[]) {
  if (v[s] == true)
    return;
  v[s] = true;
  for (ll i = 0; i < adj[s].size(); i++) {
    if (v[adj[s][i]] == 0 && arr[adj[s][i]] == 0) {
      arr[adj[s][i]] = s;
      num.push(adj[s][i]);
    }
  }
  num.pop();
  if (num.size() == 0)
    return;
  bfs(num.front(), adj);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  ll x, y;
  for (ll i = 1; i <= m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  num.push(1);
  bfs(1, adj);
  for (ll i = 2; i <= n; i++) {
    if (v[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (ll i = 2; i <= n; i++)
    cout << arr[i] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define mod 1000000007;
using namespace std;
vector<ll> adj[1000000];
bool v[1000000];
ll arr[1000000];
queue<ll> num;
void bfs(ll s, vector<ll> adj[]) {
  if (v[s] == true)
    return;
  v[s] = true;
  for (int i = 0; i < adj[s].size(); i++) {
    if (v[adj[s][i]] == 0 && arr[adj[s][i]] == 0) {
      arr[adj[s][i]] = s;
      num.push(adj[s][i]);
    }
  }
  num.pop();
  bfs(num.front(), adj);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  ll x, y;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  num.push(1);
  bfs(1, adj);
  for (int i = 2; i <= n; i++) {
    if (v[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 2; i <= n; i++)
    cout << arr[i] << "\\n";

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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef priority_queue<string> priquest;
typedef priority_queue<string, vector<string>, greater<string>> rpriquest;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

const ll big = 1000000007;
int exp(int x, int y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (exp(x, y - 1) * x);
  int t = exp(x, y / 2);
  return (t * t);
}
void chmin(ll a, ll b) {
  if (a > b)
    a = b;
  return;
}

void bfs(int n, vector<vector<int>> &road, vector<int> &ans, vector<ll> &dis) {
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int s = que.front();
    que.pop();
    for (int v : road[s]) {
      if (dis[v] <= dis[s] + 1)
        continue;
      dis[v] = dis[s] + 1;
      ans[v] = s;
      que.push(v);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> road(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  vector<int> ans(n, -1);
  vector<ll> dis(n, 1000000000000000);
  dis[0] = 0;
  bfs(n, road, ans, dis);
  cout << "Yes" << endl;
  rep2(i, 1, n) cout << ans[i] + 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef priority_queue<string> priquest;
typedef priority_queue<string, vector<string>, greater<string>> rpriquest;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

const ll big = 1000000007;
int exp(int x, int y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (exp(x, y - 1) * x);
  int t = exp(x, y / 2);
  return (t * t);
}
void chmin(ll a, ll b) {
  if (a > b)
    a = b;
  return;
}

void dfs(int n, int s, vector<vector<int>> &road, vector<int> &ans,
         vector<ll> &dis) {
  for (int v : road[s]) {
    if (dis[v] <= dis[s] + 1)
      continue;
    else {
      dis[v] = dis[s] + 1;
      ans[v] = s;
      dfs(n, v, road, ans, dis);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> road(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  vector<int> ans(n, -1);
  vector<ll> dis(n, 1000000000000000);
  dis[0] = 0;
  dfs(n, 0, road, ans, dis);
  cout << "Yes" << endl;
  rep2(i, 1, n) cout << ans[i] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

const int e = 2e5 + 69;

vector<ll> adj[e];
ll par[e];
bool vs[e];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++) {
    vs[i] = true;
  }
  queue<ll> q;
  q.push(1);
  vs[1] = false;
  while (q.size() != 0) {
    ll cur = q.front();
    q.pop();
    for (auto j : adj[cur]) {
      if (vs[j] == true) {
        /*if(par[j] != 0 && par[j] != cur){
            cout << "No";
            return 0;
        }*/
        par[j] = cur;
        vs[j] = false;
        q.push(j);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (par[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 2; i <= n; i++) {
    cout << par[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

const int e = 2e5 + 69;

vector<ll> adj[e];
bool vs[e];
ll des[e];
ll trace[e];
ll minn = 1e18;
vector<ll> cur;

bool bfs(ll x, ll par) {
  // memset(vs, true, sizeof(vs));
  for (auto j : cur) {
    vs[j] = true;
    // cout << j <<" ";
  }
  // cout << "\\n";
  cur.clear();
  vs[x] = false;
  queue<pair<ll, ll>> q;
  q.push({x, 0});
  cur.pb(x);
  while (q.size() != 0) {
    pair<ll, ll> f = q.front();
    q.pop();
    if (f.first == 1) {
      ll pos = 1;
      while (pos != par) {
        des[trace[pos]] = pos;
        pos = trace[pos];
      }
      return true;
    }
    for (auto j : adj[f.first]) {
      if (vs[j] == true) {
        cur.pb(j);
        vs[j] = false;
        trace[j] = f.first;
        /*if(des[j] != -1){
            ll pos = j;
            while(pos != par){
                des[trace[pos]] = pos;
                pos = trace[pos];
            }
            return true;
        }*/
        q.push({j, f.second + 1});
      }
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++)
    vs[i] = true, des[i] = -1;
  vector<ll> kq;
  for (int i = 2; i <= n; i++) {
    if (des[i] != -1) {
      kq.pb(des[i]);
      continue;
    }
    minn = 1e18;
    // cur.pb(i);
    if (bfs(i, i))
      kq.pb(des[i]);
    else {
      cout << "No";
      return 0;
    }
    // cout << "\\n";
  }
  cout << "Yes"
       << "\\n";
  for (auto j : kq) {
    cout << j << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

//  main.cpp
//  ABC168D

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1001001001;
int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> r[100005];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n + 1, INF); // 入り口からの距離
  dist.at(1) = 0;               // 入り口の洞窟
  q.push(1);
  vector<int> pre(n + 1, -1); // 直前の洞窟番号
  while (!q.empty()) {
    int v = q.front(); // 先頭の洞窟番号
    q.pop();
    for (int u : r[v]) {
      if (dist[u] == INF) {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << pre.at(i) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

//  main.cpp
//  ABC168D

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1001001001;
int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> r[m + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n + 1, INF); // 入り口からの距離
  dist.at(1) = 0;               // 入り口の洞窟
  q.push(1);
  vector<int> pre(n + 1, -1); // 直前の洞窟番号
  while (!q.empty()) {
    int v = q.front(); // 先頭の洞窟番号
    q.pop();
    for (int u : r[v]) {
      if (dist[u] == INF) {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << pre.at(i) << endl;
  }

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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
queue<int> U;
vector<int> dist;
vector<int> d(101010);

void dfs(const Graph &G, int v) {
  U.push(v);
  dist[v] = 0;
  while (!U.empty()) {
    int v_2 = U.front();
    U.pop();
    for (auto next_v : G[v_2]) {
      if (dist[next_v] == -1) {
        U.push(next_v);
        dist[next_v] = dist[v_2] + 1;
        d[next_v] = v_2 + 1;
        // cout << next_v << endl; //通った場所を出力(確認用)
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n + 1);
  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);
  }
  // 頂点 s をスタートとした探索
  dist.assign(n + 1, -1); // 全頂点を「未訪問」に初期化
  dfs(G, 0);
  // t に辿り着けるかどうか
  for (int i = 1; i < n; i++) {
    if (dist[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << d[i + 1] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
queue<int> U;
vector<int> dist;
vector<int> d(10101);

void dfs(const Graph &G, int v) {
  U.push(v);
  dist[v] = 0;
  while (!U.empty()) {
    int v_2 = U.front();
    U.pop();
    for (auto next_v : G[v_2]) {
      if (dist[next_v] == -1) {
        U.push(next_v);
        dist[next_v] = dist[v_2] + 1;
        d[next_v] = v_2 + 1;
        // cout << next_v << endl; //通った場所を出力(確認用)
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n + 1);
  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);
  }
  // 頂点 s をスタートとした探索
  dist.assign(n + 1, -1); // 全頂点を「未訪問」に初期化
  dfs(G, 0);
  // t に辿り着けるかどうか
  for (int i = 1; i < n; i++) {
    if (dist[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << d[i + 1] << endl; }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

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

  vector<vector<long long>> v(n + 1);

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

  vector<long long> distance(n + 1, -1);
  vector<long long> front(n + 1);
  distance[1] = 0;

  queue<long long> q;
  long long top;
  q.push(1);

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

    for (long long i = 0; i < v[top].size(); i++) {
      if (distance[v[top][i]] != -1) {
        continue;
      }
      q.push(v[top][i]);
      distance[v[top][i]] = distance[top] + 1;
      front[v[top][i]] = top;
    }
  }

  printf("Yes\\n");
  for (long long i = 2; i <= n; i++) {
    printf("%lld\\n", front[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(m);
  vector<long long> b(m);

  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  /*
  for(long long i = 0; i < m; i++){
      printf("a %lld b %lld\\n", a[i], b[i]);
  }*/

  vector<long long> distance(n + 1, -1);
  vector<long long> front(n + 1);
  distance[1] = 0;

  queue<long long> q;
  long long top;
  q.push(1);

  while (!q.empty()) {
    top = q.front();
    q.pop();
    // printf("top %lld\\n", top);
    for (long long i = 0; i < m; i++) {
      // printf("a[i] %lld b[i] %lld ", a[i], b[i]);
      if (a[i] == top and distance[b[i]] == -1) {
        // printf("i %lld a[i] %lld push b[i] %lld top %lld\\n", i, a[i], b[i],
        // top);
        q.push(b[i]);
        distance[b[i]] = distance[top] + 1;
        front[b[i]] = top;
        // a.erase(a.begin()+i);
        // b.erase(b.begin()+i);
        // printf("front %lld\\n", top);
      }

      if (b[i] == top and distance[a[i]] == -1) {
        // printf("i %lld b[i] %lld push a[i] %lld top %lld\\n",i, b[i], a[i],
        // top);
        q.push(a[i]);
        distance[a[i]] = distance[top] + 1;
        front[a[i]] = top;
        // a.erase(a.begin()+i);
        // b.erase(b.begin()+i);
      }
    }
  }

  printf("Yes\\n");
  for (long long i = 2; i <= n; i++) {
    printf("%lld\\n", front[i]);
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 1e6 + 5;
int n, m, fa[N], ans[N], vis[N];

struct Edge {
  int v, ne;
} e[N];
int head[N], tot;

inline void add(int u, int v);
void bfs();

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  bfs();
  if (m < n - 1)
    printf("No\\n");
  else {
    printf("Yes\\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\\n", ans[i]);
  }
  return 0;
}

inline void add(int u, int v) {
  e[++tot] = (Edge){v, head[u]};
  head[u] = tot;
}

void bfs() {
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = head[u]; i; i = e[i].ne) {
      int v = e[i].v;
      if (!vis[v]) {
        vis[v] = 1;
        ans[v] = u;
        qu.push(v);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 2e5 + 5;
int n, m, fa[N], ans[N], vis[N];

struct Edge {
  int v, ne;
} e[N];
int head[N], tot;

inline void add(int u, int v);
void bfs();

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  bfs();
  if (m < n - 1)
    printf("No\\n");
  else {
    printf("Yes\\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\\n", ans[i]);
  }
  return 0;
}

inline void add(int u, int v) {
  e[++tot] = (Edge){v, head[u]};
  head[u] = tot;
}

void bfs() {
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = head[u]; i; i = e[i].ne) {
      int v = e[i].v;
      if (!vis[v]) {
        vis[v] = 1;
        ans[v] = u;
        qu.push(v);
      }
    }
  }
}
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++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, m;
  cin >> n >> m;
  int a[m + 1], b[m + 1];
  vector<vector<int>> g(n + 1);
  rep(i, 1, m + 1) {
    cin >> a[i] >> b[i];
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  queue<int> q;
  q.push(1);

  int prev[n + 1];
  rep(i, 0, n + 1) prev[i] = -1;

  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    rep(i, 0, g[tmp].size()) {
      int v = g[tmp][i];
      if (prev[v] == -1) {
        prev[v] = tmp;
        q.push(v);
      }
    }
  }
  rep(i, 2, n + 1) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n + 1) cout << prev[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[m + 1], b[m + 1];
  rep(i, 1, m + 1) cin >> a[i] >> b[i];
  queue<int> q;
  q.push(1);

  int prev[n + 1];
  rep(i, 0, n + 1) prev[i] = -1;

  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    rep(i, 1, m + 1) {
      if (a[i] == tmp && prev[b[i]] == -1) {
        prev[b[i]] = a[i];
        q.push(b[i]);
      } else if (b[i] == tmp && prev[a[i]] == -1) {
        prev[a[i]] = b[i];
        q.push(a[i]);
      }
    }
  }
  rep(i, 2, n + 1) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n + 1) cout << prev[i] << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;
using ll = long long int;

vector<vector<int>> cave;
vector<int> done;

void bfs() {
  queue<vector<int>> que;
  que.push({0, 0, 0});
  while (!que.empty()) {
    int pre = que.front()[0], cur = que.front()[1], cnt = que.front()[2];
    que.pop();
    if (done[cur] == -1 || done[cur] > cnt)
      done[cur] = cnt;
    else
      continue;
    for (auto next : cave[cur]) {
      if (next == cur || next == pre)
        continue;
      que.push({cur, next, cnt + 1});
    }
  }
  return;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  cave.resize(n);
  done.resize(n);
  REP(i, n) done[i] = -1;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    cave[a].push_back(b);
    cave[b].push_back(a);
  }
  bfs();
  // REP(i, n) cout << done[i] << endl;
  bool t = true;
  REP(i, n) if (done[i] == -1) t = false;
  if (t) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      for (int v = 0; v < cave[i].size(); v++) {
        if (done[i] - 1 == done[cave[i][v]]) {
          cout << cave[i][v] + 1 << endl;
          break;
        }
      }
    }
  } else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;
using ll = long long int;

vector<vector<int>> cave;
vector<int> done;

void dfs(int cur, int cnt) {
  if (done[cur] == -1 || done[cur] > cnt) {
    done[cur] = cnt;
  } else
    return;
  for (auto next : cave[cur]) {
    if (next == cur)
      continue;
    dfs(next, cnt + 1);
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;
  cave.resize(n);
  done.resize(n);
  REP(i, n) done[i] = -1;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    cave[a].push_back(b);
    cave[b].push_back(a);
  }
  dfs(0, 0);
  // REP(i, n) cout << done[i] << endl;
  bool t = true;
  REP(i, n) if (done[i] == -1) t = false;
  if (t) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      for (int v = 0; v < cave[i].size(); v++) {
        if (done[i] - 1 == done[cave[i][v]]) {
          cout << cave[i][v] + 1 << endl;
          break;
        }
      }
    }
  } else
    cout << "No" << endl;
}