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

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

vector<vector<ll>> g(110000);
ll p[110000], ans[110000];
queue<ll> q;
int main() {
  ll n, m, x, y;
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  p[1] = 1;
  q.push(1);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (auto i : g[v]) {
      if (p[i])
        continue;
      else {
        p[i] = 1;
        ans[i] = v;
        q.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n + 1) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

vector<pair<ll, ll>> p;
ll d[110000];
bool c[110000];
int main() {
  ll n, m;
  ll x, y;
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    p.pb(mp(x, y));
  }
  rep(i, 0, m + 10) d[i] = i - 1;

  sort(p.begin(), p.end());
  rep(i, 0, m + 10) {
    if (c[p[i] S])
      continue;
    else if (c[p[i] S] == false) {
      d[p[i] S] -= p[i] S - p[i] F - 1;
      c[p[i] S] = true;
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n + 1) cout << d[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 300005
#define endl "\\n"
vector<lli> adj[MAX + 5];
lli ans[MAX];
queue<lli> q;
lli N, M;
void BFS(int source, vector<lli> adj[MAX + 5], vector<bool> &visited) {
  visited[source] = true;
  q.push(source);
  while (!q.empty()) {
    source = q.front();
    visited[source] = true;
    for (auto i = adj[source].begin(); i != adj[source].end(); i++) {
      if (!visited[*i]) {
        visited[*i] = true;
        ans[*i] = source;
        q.push(*i);
      }
    }
    q.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> M;
  for (lli i = 0; i < M; i++) {
    lli A, B;
    cin >> A >> B;
    adj[A].push_back(B);
    adj[B].push_back(A);
  }
  vector<bool> visited(N + 5, 0);
  BFS(1, adj, visited);
  // cout<<endl;
  bool flag = true;
  for (lli i = 2; i <= N; i++) {
    if (!ans[i])
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (lli i = 2; i <= N; i++)
      cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 300005
#define endl "\\n"
vector<lli> adj[MAX + 5];
lli ans[MAX];
queue<lli> q;
lli N, M;
void BFS(int source, vector<lli> adj[MAX + 5], vector<bool> &visited) {
  visited[source] = true;
  q.push(source);
  while (!q.empty()) {
    source = q.front();
    visited[source] = true;
    for (auto i = adj[source].begin(); i != adj[source].end(); i++) {
      if (!visited[*i]) {
        BFS(*i, adj, visited);
        ans[*i] = source;
        q.push(*i);
      }
    }
    q.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> M;
  for (lli i = 0; i < M; i++) {
    lli A, B;
    cin >> A >> B;
    adj[A].push_back(B);
    adj[B].push_back(A);
  }
  vector<bool> visited(N + 5, 0);
  BFS(1, adj, visited);
  // cout<<endl;
  bool flag = true;
  for (lli i = 2; i <= N; i++) {
    if (!ans[i])
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (lli i = 2; i <= N; i++)
      cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
int main() {
  ll n, m;
  cin >> n >> m;
  vi edge[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[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 (auto nv : edge[v]) {
      if (dist[nv] == -1) {
        dist[nv] = v + 1;
        que.push(nv);
      }
    }
  }
  bool f = true;

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

  if (!f) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, n - 1) { cout << dist[i + 1] << endl; }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
// double pi =3.14159265358979322846264338327950288;
int main() {
  int n, m;
  cin >> n >> m;
  vi edge[m];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[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 (auto nv : edge[v]) {
      if (dist[nv] == -1) {
        dist[nv] = v + 1;
        que.push(nv);
      }
    }
  }
  bool f = true;
  rep(i, n) {
    if (dist[i] == -1)
      f = false;
  }
  if (!f) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, n - 1) { cout << dist[i + 1] << endl; }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

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 - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  vector<int> dist(N, -1);
  vector<int> sirube(N);
  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] = dist[v] + 1;
      sirube[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int v = 1; v < N; ++v)
    cout << sirube[v] + 1 << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000001

struct node {
  vector<int> to;
  int sirube;
  int dist = INF;
};
void dfs(int v, int p, vector<node> &nodes, int dist_now) {
  for (int i = 0; i < nodes[v].to.size(); i++) {
    int v_to = nodes[v].to[i];
    if (v_to == p)
      continue;
    if (nodes[v_to].dist <= dist_now + 1)
      continue;
    else {
      nodes[v_to].sirube = v;
      nodes[v_to].dist = dist_now + 1;
      dfs(v_to, v, nodes, dist_now + 1);
    }
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<node> nodes(N);
  nodes[0].dist = 0;
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    nodes[u - 1].to.push_back(v - 1);
    nodes[v - 1].to.push_back(u - 1);
  }
  int dist_now = 0;
  dfs(0, -1, nodes, dist_now);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << nodes[i].sirube + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define mod_ceil(a, b) (a % b) ? a / b + 1 : a / b
using namespace std;

#define o5 100100
#define o12 1000000000000

ll n, m;
vector<ll> G[o5];

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

  ll a, b, tmp;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  // bfs
  queue<ll> Q;
  vector<ll> source(n, -1);
  Q.push(0);
  while (!Q.empty()) {
    tmp = Q.front();
    Q.pop();
    for (ll j = 0; j < G[tmp].size(); j++) {
      a = G[tmp][j];
      if (source[a] == -1) {
        Q.push(a);
        source[a] = tmp;
      }
    }
  }
  source[0] = 0;
  bool possible = find(source.begin(), source.end(), -1) == source.end();
  if (possible) {
    cout << "Yes" << endl;
    for (ll i = 1; i < n; i++) {
      cout << source[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mod_ceil(a, b) (a % b) ? a / b + 1 : a / b
using namespace std;

#define o5 100100

ll n, m;
vector<ll> G[o5];

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

  ll a, b, tmp;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  // bfs
  stack<ll> S;
  vector<ll> path(n, o5);
  vector<ll> source(n, -1);
  S.push(0);
  path[0] = 0;
  while (!S.empty()) {
    tmp = S.top();
    S.pop();
    for (ll j = 0; j < G[tmp].size(); j++) {
      a = G[tmp][j];
      if (path[a] > path[tmp] + 1) {
        S.push(a);
        path[a] = path[tmp] + 1;
        source[a] = tmp;
      }
    }
  }

  bool possible = find(path.begin(), path.end(), o5) == path.end();
  if (possible) {
    cout << "Yes" << endl;
    for (ll i = 1; i < n; i++) {
      cout << source[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define divup(a, b) (((a) + ((b)-1u)) / (b)) // a/b の余り切り上げ
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
ll mod = 1000000007;
ll mod9 = 1000000009;

pair<ll, ll> a[200009];
ll dist[200009];
vector<vector<ll>> r(200009);

signed main() {
  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i].first >> a[i].second;
    r[a[i].first - 1].push_back(a[i].second - 1);
    r[a[i].second - 1].push_back(a[i].first - 1);
  }
  /* rep(i,n){
      rep(j,r[i].size()){
          if(!j) cout<<endl;
          cout<<" "<<r[i][j];
      }
  }*/
  rep(i, 200009) dist[i] = -1;
  queue<ll> Q;
  dist[0] = 0;
  Q.push(0);
  while (!Q.empty()) {
    ll v = Q.front();
    Q.pop();
    rep(i, r[v].size()) {
      if (dist[r[v][i]] != -1)
        continue;
      dist[r[v][i]] = v + 1;
      Q.push(r[v][i]);
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { printf("%lld\\n", dist[i + 1]); }
}


Error: 0

Fixed submission:

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define divup(a, b) (((a) + ((b)-1u)) / (b)) // a/b の余り切り上げ
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
ll mod = 1000000007;
ll mod9 = 1000000009;

pair<ll, ll> a[200009];
ll dist[200009];
vector<vector<ll>> r(200009);

ll search(ll x, ll ct) {
  rep(i, 200009) dist[i] = -1;
  queue<ll> Q;
  dist[0] = 0;
  Q.push(0);
  while (!Q.empty()) {
    ll v = Q.front();
    Q.pop();
    rep(i, r[v].size()) {
      if (dist[r[v][i]] != -1)
        continue;
      dist[r[v][i]] = v + 1;
      Q.push(r[v][i]);
    }
  }
}
signed main() {
  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i].first >> a[i].second;
    r[a[i].first - 1].push_back(a[i].second - 1);
    r[a[i].second - 1].push_back(a[i].first - 1);
  }
  /* rep(i,n){
      rep(j,r[i].size()){
          if(!j) cout<<endl;
          cout<<" "<<r[i][j];
      }
  }*/
  search(0, 0);
  cout << "Yes" << endl;
  rep(i, n - 1) { printf("%lld\\n", dist[i + 1]); }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int INF = 2e6;
ll MOD = 1e9 + 7;

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

  queue<int> que;
  que.push(0);
  vector<bool> vis(n, false);
  vis[0] = true;
  vector<int> dist(n);
  vector<int> ans(n);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto i : g[p]) {
      if (!vis[i]) {
        vis[i] = true;
        dist[i] = dist[p] + 1;
        que.push(i);
        ans[i] = p + 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (dist[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int INF = 2e6;
ll MOD = 1e9 + 7;

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

  queue<int> que;
  que.push(0);
  vector<bool> vis(n, false);
  vis[0] = true;
  vector<int> dist(n);
  vector<int> ans(n);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto i : g[p]) {
      if (!vis[i]) {
        vis[i] = true;
        dist[i] = dist[p] + 1;
        que.push(i);
        ans[i] = p + 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (dist[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[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;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> 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> prev(n, -1);
  vector<int> dist(n, INF);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : g[v]) {
      if (dist[nv] == INF) {
        dist[nv] = dist[v] + 1;
        q.push(nv);
        prev[nv] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << prev[i] + 1 << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> prev(n, -1);
  vector<int> dist(n, INF);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : g[v]) {
      if (dist[nv] == INF) {
        dist[nv] = dist[v] + 1;
        q.push(nv);
        prev[nv] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << prev[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

const ll N = 2e6 + 7;

ll n, m, cnt, a;
vector<int> g[N];
ll used[N], p[N];
string s;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  queue<int> q;
  q.push(1);
  p[1] = 1;
  used[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      if (!used[to]) {
        used[to]++;
        q.push(to);
        p[to] = (v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (!used[i])
      return cout << "No", 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << p[i] << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

const ll N = 1e5;

ll n, m, cnt, a;
vector<int> g[N];
ll used[N], p[N];
string s;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  queue<int> q;
  q.push(1);
  p[1] = 1;
  used[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      if (!used[to]) {
        used[to]++;
        q.push(to);
        p[to] = (v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (!used[i])
      return cout << "No", 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << p[i] << "\\n";
  }
}
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>;
using ull = unsigned long long;
const double pi = acos(-1);
const ll N_MAX = 100005;
ll now[N_MAX];
ll now_old[N_MAX];
const ll INF = 1e18;

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> c(n);
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    c[a].push_back(b);
    c[b].push_back(a);
  }
  vector<int> visit(n); // 訪れるステップ数
  vector<int> sig(n);   // 導
  vector<int> next(n), in(n);
  rep(i, n) {
    visit[i] = -1;
    next[i] = -1;
    in[i] = -1;
  }
  visit[0] = 1;
  next[0] = 0;
  in[0] = 0;
  int j = 0;
  int time = 0;
  in[0] = 0;
  for (int i = 0; i < n; i++) {
    int p = next[i];
    for (int q : c[p]) { //
      if (visit[q] < 0) {
        // cout<<"pq;"<<p<<q<<endl;
        sig[q] = p;
        visit[q] = 1;
        j++;
        next[j] = q;
        // cout<<q<<endl;
        in[j] = in[i] + 1;
      }
    }
  }
  // rep(i,n) cout<<in[i]<<" "<<next[i]<<endl;
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++)
    cout << sig[i] + 1 << endl;
}


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>;
using ull = unsigned long long;
const double pi = acos(-1);
const ll N_MAX = 100005;
ll now[N_MAX];
ll now_old[N_MAX];
const ll INF = 1e18;

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> c(n);
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    c[a].push_back(b);
    c[b].push_back(a);
  }
  vector<int> visit(n); // 訪れるステップ数
  vector<int> sig(n);   // 導
  rep(i, n) visit[i] = -1;
  visit[0] = 1;

  rep(time, n) {
    rep(p, n) {
      if (visit[p] == time) {
        for (int q : c[p]) { //
          if (visit[q] < 0) {
            // cout<<"pq;"<<p<<q<<endl;
            sig[q] = p;
            visit[q] = time + 1;
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++)
    cout << sig[i] + 1 << endl;
}