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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

class graph {
public:
  int v;
  list<int> *adj;
  int *par;
  bool *m;
  bool *l;
  bool *visited;

  graph(int v) {
    this->v = v;
    adj = new list<int>[v];
    par = new int[v];
    m = new bool[v];
    l = new bool[v];
    visited = new bool[v];
    for (int i = 0; i < v; ++i) {
      par[i] = -1;
      m[i] = false;
      l[i] = false;
      visited[i] = false;
    }
  }

  void add(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  void bfs(queue<int> &i);
};

void graph::bfs(queue<int> &i) {
  if (i.empty())
    return;
  int k = i.front();
  i.pop();
  visited[k] = true;
  for (auto t : adj[k]) {
    if (!visited[t]) {
      par[t] = k;
      visited[t] = true;
      i.push(t);
    }
  }
  bfs(i);
}

void solve() {
  int n, m;
  // cin>>n>>m;
  scanf("%d %d", &n, &m);
  graph g(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g.add(u, v);
  }
  queue<int> q;
  q.push(0);
  g.visited[0] = true;
  g.bfs(q);
  // cout<<"Yes"<<endl;
  printf("Yes\\n");
  for (int i = 1; i < n; ++i) {
    // cout<<g.par[i]+1<<endl;
    printf("%d\\n", g.par[i] + 1);
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

class graph {
public:
  int v;
  list<int> *adj;
  int *par;
  bool *m;
  bool *l;
  bool *visited;

  graph(int v) {
    this->v = v;
    adj = new list<int>[v];
    par = new int[v];
    m = new bool[v];
    l = new bool[v];
    visited = new bool[v];
    for (int i = 0; i < v; ++i) {
      par[i] = -1;
      m[i] = false;
      l[i] = false;
      visited[i] = false;
    }
  }

  void add(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  void bfs(queue<int> i);
};

void graph::bfs(queue<int> i) {
  if (i.empty())
    return;
  ll k = i.front();
  i.pop();
  visited[k] = true;
  for (auto t : adj[k]) {
    if (!visited[t]) {
      par[t] = k;
      visited[t] = true;
      i.push(t);
    }
  }
  bfs(i);
}

void solve() {
  int n, m;
  cin >> n >> m;
  graph g(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g.add(u, v);
  }
  queue<int> q;
  q.push(0);
  g.bfs(q);
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << g.par[i] + 1 << endl;
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

int N, M;

bool visited[100005];
queue<int> q;
vector<int> v[100005];
int32_t main() {

  IOS;

  cin >> N >> M;

  bool flag = false;
  for (int i = 0; i < M; i++) {

    int r1, r2;

    cin >> r1 >> r2;

    v[r1].push_back(r2);

    v[r2].push_back(r1);

    if (r1 == 1 || r2 == 1)
      flag = true;
  }

  if (!flag) {
    cout << "No" << endl;
  } else {

    vector<int> ans(N + 1);

    q.push(1);

    while (q.size()) {

      int tmp = q.front();
      q.pop();

      if (!visited[tmp]) {

        visited[tmp] = true;

        for (int i : v[tmp]) {

          q.push(i);
          if (ans[i] == 0) {
            ans[i] = tmp;
          }
        }
      }
    }
    cout << "Yes" << endl;

    for (int i = 2; i <= N; i++) {

      cout << ans[i] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

int N, M;

bool visited[100005];
queue<int> q;

int32_t main() {

  IOS;

  cin >> N >> M;
  vector<vector<int>> v(N + 1, vector<int>(N + 1, 0));
  bool flag = false;
  for (int i = 0; i < M; i++) {

    int r1, r2;

    cin >> r1 >> r2;

    v[r1].push_back(r2);

    v[r2].push_back(r1);

    if (r1 == 1 || r2 == 1)
      flag = true;
  }

  if (!flag) {
    cout << "No" << endl;
  } else {

    vector<int> ans(N + 1);

    q.push(1);

    while (q.size()) {

      int tmp = q.front();
      q.pop();

      if (!visited[tmp]) {

        visited[tmp] = true;

        for (int i : v[tmp]) {

          q.push(i);
          if (ans[i] == 0) {
            ans[i] = tmp;
          }
        }
      }
    }
    cout << "Yes" << endl;

    for (int i = 2; i <= N; i++) {

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

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<lli, lli>
#define ps pair<double, string>
#define pib pair<pi, bool>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 100002
#define MAXL 100002
#define mod 1000000007

int n, m;
vector<int> adj[MAXN];
bool visited[MAXN];
int dist[MAXN];
int parent[MAXN];

void dijkstra(int s) {
  dist[s] = 0;
  priority_queue<pi, vector<pi>, greater<pi>> q;
  q.push(pi(dist[s], s));
  while (1) {
    int davez = -1;
    while (!q.empty()) {
      int atual = q.top().second;
      q.pop();
      if (!visited[atual]) {
        davez = atual;
        break;
      }
    }
    if (davez == -1)
      break;
    visited[davez] = true;
    for (int i = 0; i < adj[davez].size(); i++) {
      if (dist[adj[davez][i]] > dist[davez] + 1) {
        parent[adj[davez][i]] = davez;
        dist[adj[davez][i]] = dist[davez] + 1;
        q.push(pi(dist[adj[davez][i]], adj[davez][i]));
      }
    }
  }
}
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  for (int i = 1; i <= n; i++)
    dist[i] = INT_MAX, parent[i] = -1;
  dijkstra(1);
  bool can = true;
  for (int i = 2; i <= n; i++)
    if (dist[i] == INT_MAX)
      can = false;
  if (!can)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << parent[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<lli, lli>
#define ps pair<double, string>
#define pib pair<pi, bool>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 50
#define MAXL 100001
#define mod 1000000007

vector<int> adj[MAXN];
bool visited[MAXN];
int path[MAXN];
int dist[MAXN];

void dfs(int s, int c, int at) {
  visited[s] = true;
  if (dist[s] > at)
    path[s] = c, dist[s] = at;
  for (int i = 0; i < adj[s].size(); i++) {
    if (dist[adj[s][i]] > at + 1)
      path[adj[s][i]] = s, dist[adj[s][i]] = at + 1;
    if (!visited[adj[s][i]])
      dfs(adj[s][i], s, at + 1);
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  memset(path, -1, sizeof(path));
  memset(dist, 999999, sizeof(dist));
  dfs(1, 0, 0);
  bool can = true;
  for (int i = 2; i <= n; i++)
    if (path[i] == INT_MAX)
      can = false;
  if (!can)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << path[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  vector<set<int>> load(N);
  int A, B;
  for (ll i = 0; i < M; i++) {
    cin >> A >> B;
    --A;
    --B;
    load.at(A).insert(B);
    load.at(B).insert(A);
  }

  vector<int> flag(N, -1);
  flag.at(0) = 0;
  vector<int> dist(N, -1);
  dist.at(0) = 0;

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

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

    for (int next : load.at(now)) {
      if (dist.at(next) == -1) {
        q.push(next);
        dist.at(next) = dist.at(now) + 1;
        flag.at(next) = now + 1;
      }
    }
  }

  for (int i = 1; i < N; i++) {
    if (flag.at(i) == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << flag.at(i) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

ll N, M;

void put_flag(vector<vector<int>> load, vector<int> *flag, vector<int> *dist,
              int now) {
  for (int i = 1; i < N; i++) {
    if (load.at(now).at(i) == 1) {
      if (flag->at(i) == 0 || dist->at(now) < dist->at(flag->at(i) - 1)) {
        flag->at(i) = now + 1;
        dist->at(i) = dist->at(now) + 1;
        put_flag(load, flag, dist, i);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  vector<vector<int>> load(N, vector<int>(N, 0));
  int A, B;
  for (ll i = 0; i < M; i++) {
    cin >> A >> B;
    load.at(A - 1).at(B - 1) = 1;
    load.at(B - 1).at(A - 1) = 1;
  }

  vector<int> flag(N, 0);
  vector<int> dist(N, 0);

  put_flag(load, &flag, &dist, 0);

  for (int i = 1; i < N; i++) {
    if (flag.at(i) == 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << flag.at(i) << endl;

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

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

long n, m, aga;
vector<long> a(200000);
vector<long> b(200000);
vector<vector<long>> aaa(200000, vector<long>(0));
vector<long> ans(200000, -1);
vector<long> kaisu(200000, 99999999);

int main() {
  cin >> n >> m;
  for (long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    aaa[a[i] - 1].push_back(b[i] - 1);
    aaa[b[i] - 1].push_back(a[i] - 1);
  }
  ans[0] = 0;
  kaisu[0] = 0;
  queue<long> x;
  queue<long> y;
  x.push(0);
  y.push(0);
  while (x.size() != 0) {
    for (long i = 0; i < aaa[x.front()].size(); i++) {
      if (ans[aaa[x.front()][i]] == -1) {
        ans[aaa[x.front()][i]] = x.front() + 1;
        x.push(aaa[x.front()][i]);
        y.push(y.front() + 1);
      }
    }
    x.pop();
    y.pop();
  }
  cout << "Yes" << endl;
  for (long i = 1; i < n; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

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

long n, m;
vector<long> a(200000);
vector<long> b(200000);
vector<vector<long>> aaa(200000, vector<long>(0));
vector<long> ans(200000, -1);
vector<long> kaisu(200000, 99999999);

void dfs(long x, long y) {
  for (long i = 0; i < aaa[x].size(); i++) {
    if (y + 1 < kaisu[aaa[x][i]]) {
      ans[aaa[x][i]] = x + 1;
      kaisu[aaa[x][i]] = y + 1;
      dfs(aaa[x][i], y + 1);
    }
  }
  return;
}

int main() {
  cin >> n >> m;
  for (long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    aaa[a[i] - 1].push_back(b[i] - 1);
    aaa[b[i] - 1].push_back(a[i] - 1);
  }
  ans[0] = 0;
  kaisu[0] = 0;
  dfs(0, 0);

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

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

const int N = 4e5 + 5;
typedef long long ll;

struct Edge {
  int to, next;
} e[N];
int n, m, cnt, first[N], s[N], h, r, q[N << 2];

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cout << "Yes" << endl;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  q[h = r = 1] = 1;
  while (h <= r) {
    int u = q[h];
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (!s[v]) {
        s[v] = u;
        q[++r] = v;
      }
    }
    h++;
  }
  for (int i = 2; i <= n; i++)
    cout << s[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 2e5 + 5;
typedef long long ll;

struct Edge {
  int to, next;
} e[N];
int n, m, cnt, first[N], s[N], h, r, q[N << 2];

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cout << "Yes" << endl;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  q[h = r = 1] = 1;
  while (h <= r) {
    int u = q[h];
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (!s[v]) {
        s[v] = u;
        q[++r] = v;
      }
    }
    h++;
  }
  for (int i = 2; i <= n; i++)
    cout << s[i] << 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;
  vector<vector<int>> graph(N);
  vector<int> color(N, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    graph[A].push_back(B);
    graph[B].push_back(A);
  }

  queue<int> Q;

  Q.push(0);
  color[0] = 0;

  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();

    for (int i = 0; i < graph[now].size(); i++) {
      if (color[graph[now][i]] == -1) {
        color[graph[now][i]] = now;
        Q.push(graph[now][i]);
      }
    }
  }

  for (int i = 1; i < N; i++) {
    if (color[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(M);
  vector<int> color(N, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    graph[A].push_back(B);
    graph[B].push_back(A);
  }

  queue<int> Q;

  Q.push(0);
  color[0] = 0;

  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();

    for (int i = 0; i < graph[now].size(); i++) {
      if (color[graph[now][i]] == -1) {
        color[graph[now][i]] = now;
        Q.push(graph[now][i]);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (color[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

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

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

struct Room {
  vector<int> to;
  int dir;
  int dist;
  Room() : dir(-1), dist(100000000) {}
};

vector<pair<int, int>> FindRoute(vector<Room> &rooms,
                                 const vector<pair<int, int>> &candidates,
                                 int dist) {
  vector<pair<int, int>> next;
  for (const auto &ft : candidates) {
    Room &room = rooms[ft.second];
    if (dist < room.dist) {
      room.dir = ft.first;
      room.dist = dist;
      for (const auto &to : room.to)
        next.emplace_back(pair<int, int>(ft.second, to));
    }
  }
  return next;
}

int main() {
  auto &in = cin;

  int N, M;
  in >> N >> M;
  vector<Room> rooms(N);
  for (int i = 0; i < M; ++i) {
    int A, B;
    in >> A >> B;
    rooms[A - 1].to.emplace_back(B - 1);
    rooms[B - 1].to.emplace_back(A - 1);
  }

  vector<pair<int, int>> candidates = {pair<int, int>(-1, 0)};
  int dist = 0;
  do {
    candidates = FindRoute(rooms, candidates, dist++);
  } while (!candidates.empty());

  for (int i = 1; i < N; ++i) {
    if (rooms[i].dir < 0) {
      cout << "No";
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i) {
    cout << rooms[i].dir + 1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

struct Room {
  vector<int> to;
  int dir;
  int dist;
  Room() : dir(-1), dist(100000000) {}
};

void FindRoute(vector<Room> &rooms, int from, int to, int dist) {
  Room &room = rooms[to];
  if (room.dir >= 0 && room.dist < dist)
    return;
  room.dir = from;
  room.dist = dist;
  for (size_t i = 0; i < room.to.size(); ++i)
    FindRoute(rooms, to, room.to[i], dist + 1);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<Room> rooms(N);
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    rooms[A - 1].to.emplace_back(B - 1);
    rooms[B - 1].to.emplace_back(A - 1);
  }
  FindRoute(rooms, -1, 0, 1);

  for (int i = 1; i < N; ++i) {
    if (rooms[i].dir < 0) {
      cout << "No";
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i) {
    cout << rooms[i].dir + 1 << endl;
  }

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

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

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

  const int INF = 1e9;
  vector<int> dist(n, INF);
  vector<int> parent(n);
  queue<int> q;
  q.push(0);
  dist[0];
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (int t : to[from]) {
      if (dist[t] != INF)
        continue;
      dist[t] = dist[from] + 1;
      q.push(t);
      parent[t] = from;
    }
  }
  for (int d : dist) {
    if (d == INF) {
      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>
#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<vector<int>> to(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  const int INF = 1e9;
  vector<int> dist(n, INF);
  vector<int> parent(n);
  queue<int> q;
  q.push(0);
  dist[0];
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (int t : to[from]) {
      if (dist[t] != INF)
        continue;
      dist[t] = dist[from] + 1;
      q.push(t);
      parent[t] = from;
    }
  }
  for (int d : dist) {
    if (d == INF) {
      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 <iostream>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int from;
  int to;
};
typedef pair<int, int> P;

int main(int argc, char *argv[]) {
  int N, M;
  cin >> N >> M;

  vector<edge> E[N];
  int A[M];
  int B[M];
  for (int i = 0; i < M; ++i) {
    cin >> A[i] >> B[i];
    edge e = {A[i] - 1, B[i] - 1};
    E[A[i] - 1].push_back(e);
    edge e2 = {B[i] - 1, A[i] - 1};
    E[B[i] - 1].push_back(e2);
  }

  priority_queue<P, vector<P>, greater<P>> que;
  int d[N];
  d[0] = 0;
  for (int i = 1; i < N; ++i)
    d[i] = 10000000;

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

  cout << "Yes" << endl;

  for (int i = 1; i < N; ++i) {
    int min_d = 10000000;
    int ret;
    for (int j = 0; j < E[i].size(); ++j) {
      if (min_d > d[E[i][j].to]) {
        ret = E[i][j].to;
        min_d = d[E[i][j].to];
      }
    }
    cout << ret + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

struct edge {
  int from;
  int to;
};
typedef pair<int, int> P;

int main(int argc, char *argv[]) {
  int N, M;
  cin >> N >> M;

  vector<edge> E[M];
  int A[M];
  int B[M];
  for (int i = 0; i < M; ++i) {
    cin >> A[i] >> B[i];
    edge e = {A[i] - 1, B[i] - 1};
    E[A[i] - 1].push_back(e);
    edge e2 = {B[i] - 1, A[i] - 1};
    E[B[i] - 1].push_back(e2);
  }

  priority_queue<P, vector<P>, greater<P>> que;
  int d[N];
  d[0] = 0;
  for (int i = 1; i < N; ++i)
    d[i] = 100000000;

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

  cout << "Yes" << endl;

  for (int i = 1; i < N; ++i) {
    int min_d = 100000000;
    for (int j = 0; j < E[i].size(); ++j) {
      min_d = min(min_d, d[E[i][j].to]);
    }
    cout << min_d + 1 << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, inf = 1e9, a, b;
  cin >> n >> m;
  vector<lvector> G(n);
  lvector ans(n, inf);
  ans[0] = 0;
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  lque que;
  que.push(0);
  while (que.size()) {
    lque que_new;
    while (que.size()) {
      ll from = que.front();
      que.pop();
      for (ll to : G[from]) {
        if (ans[to] != inf)
          continue;
        ans[to] = from;
        que_new.push(to);
      }
    }
    que = que_new;
  }
  if (*max_element(ALL(ans)) == inf)
    print("No");
  else {
    print("Yes");
    rep(i, n - 1) print(ans[i + 1] + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

ll n, m, inf = 1e9;
vector<lvector> G(n);
lvector dist(n), ans(n);

void dfs(ll from, ll to, ll d) {
  dist[to] = d;
  ans[to] = from;
  for (ll u : G[to]) {
    if (u == from)
      continue;
    if (d + 1 < dist[u])
      dfs(to, u, d + 1);
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll a, b;
  cin >> n >> m;
  G.resize(n);
  dist.resize(n);
  ans.resize(n);
  fill(ALL(dist), inf);
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  dfs(0, 0, 0);
  if (*max_element(ALL(dist)) == inf)
    print("No");
  else {
    print("Yes");
    rep(i, n - 1) print(ans[i + 1] + 1);
  }
  return 0;
}