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

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

const int N = 100010;
vector<int> G[N];
vector<int> D(N);
vector<int> B(N);

void dfs(int now) {
  for (auto x : G[now]) {
    int to = x;
    if (D[to] > D[now] + 1) {
      D[to] = D[now] + 1;
      B[to] = now + 1;
      dfs(to);
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1e9;
  }
  D[0] = 0;
  B[0] = -1;

  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto x : G[now]) {
      int to = x;
      if (D[to] > D[now] + 1) {
        D[to] = D[now] + 1;
        B[to] = now + 1;
        q.push(to);
      }
    }
  }

  dfs(0);

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

  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 100010;
vector<int> G[N];
vector<int> D(N);
vector<int> B(N);

void dfs(int now) {
  for (auto x : G[now]) {
    int to = x;
    if (D[to] > D[now] + 1) {
      D[to] = D[now] + 1;
      B[to] = now + 1;
      dfs(to);
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1e9;
  }
  D[0] = 0;
  B[0] = -1;

  dfs(0);

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
const ll INF = 1e9;

using Graph = vector<vector<int>>;

int N, M;
Graph G;

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, INF);
  ans[0] = -1;
  queue<int> que;
  que.push(0);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto &nv : G[v]) {
      if (ans[nv] == INF) {
        que.push(nv);
        ans[nv] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (auto &v : ans) {
    if (v == -1)
      continue;
    cout << v + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
const ll INF = 1e9;

using Graph = vector<vector<int>>;

int N, M;
Graph G;

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());
  rep(i, 0, N) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, INF);
  ans[0] = -1;
  queue<int> que;
  que.push(0);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto &nv : G[v]) {
      if (ans[nv] == INF) {
        que.push(nv);
        ans[nv] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (auto &v : ans) {
    if (v == -1)
      continue;
    cout << v + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
string explanation1 = "これは幅優先探索の問題！";
string explanation2 =
    "参考URL:https://qiita.com/drken/items/996d80bcae64649a6580";

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;

  // グラフ入力受け取り
  Graph G(100005);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;               // distで０と１のズレを直しておくため
    G[a].push_back(b); // 有向グラフ
    G[b].push_back(a); // 無向グラフなのでこれも追加
  }

  // BFSのためのデータ構造
  vector<int> dist(n, INF); // 全頂点を「未訪問」に初期化
  queue<int> que;           // キュー
  vector<int> pre(n, -1);

  // 初期条件（頂点1をノードとする）
  dist[0] = 0;
  que.push(0); // 1を橙色頂点にする

  // BFS開始（キューが空になるまで探索を行う）
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();           // 先頭要素を削除

    // v から辿れる頂点をすべて調べる
    for (int u : G[v]) { // 拡張for文 変数uにG[v]の値を入れてる
      if (dist[u] != INF)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点uについて距離情報を更新してキューに追加する
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }

  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++; // 18,19行目で１減らしてたのを元に戻す
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
string explanation1 = "これは幅優先探索の問題！";
string explanation2 =
    "参考URL:https://qiita.com/drken/items/996d80bcae64649a6580";

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;

  // グラフ入力受け取り
  Graph G(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;               // distで０と１のズレを直しておくため
    G[a].push_back(b); // 有向グラフ
    G[b].push_back(a); // 無向グラフなのでこれも追加
  }

  // BFSのためのデータ構造
  vector<int> dist(n, INF); // 全頂点を「未訪問」に初期化
  queue<int> que;           // キュー
  vector<int> pre(n, -1);

  // 初期条件（頂点1をノードとする）
  dist[0] = 0;
  que.push(0); // 1を橙色頂点にする

  // BFS開始（キューが空になるまで探索を行う）
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();           // 先頭要素を削除

    // v から辿れる頂点をすべて調べる
    for (int u : G[v]) { // 拡張for文 変数uにG[v]の値を入れてる
      if (dist[u] != INF)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点uについて距離情報を更新してキューに追加する
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }

  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++; // 18,19行目で１減らしてたのを元に戻す
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  vector<int> ans(N, 0);
  for (int i = 1; i < N; i++) {
    for (int v : graph[i]) {
      if (dist[v] == dist[i] - 1) {
        ans[i] = v + 1;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

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

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

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

  vector<int> ans(N, 0);
  for (int i = 1; i < N; i++) {
    for (int v : graph[i]) {
      if (dist[v] == dist[i] - 1) {
        ans[i] = v + 1;
      }
    }
  }
  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>
using namespace std;
int main() {
  int n, j;
  cin >> n >> j;
  unordered_map<int, vector<int>> m;
  for (int i = 0; i < j; i++) {
    int a, b;
    cin >> a >> b;
    m[a].push_back(b);
    m[b].push_back(a);
  }
  vector<int> ans(n + 1, 0);
  int count = 0;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int s = q.size();
    for (int k = 0; k < s; k++) {
      int front = q.front();
      q.pop();
      int a = m[front].size();
      for (int i = 0; i < a; i++) {
        if (ans[m[front][i]] != 0 || m[front][i] == 1)
          continue;
        ans[m[front][i]] = front;
        count++;
        q.push(m[front][i]);
      }
    }
  }
  if (count != n - 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, j;
  cin >> n >> j;
  unordered_map<int, vector<int>> m;
  for (int i = 0; i < j; i++) {
    int a, b;
    cin >> a >> b;
    m[a].push_back(b);
    m[b].push_back(a);
  }
  vector<int> ans(n + 1, 0);
  int count = 0;
  queue<int> q;
  q.push(1);
  while (!q.empty() && count != n - 1) {
    int s = q.size();
    for (int k = 0; k < s; k++) {
      int front = q.front();
      q.pop();
      for (int i = 0; i <= (int)(m[front].size()); i++) {
        if (ans[m[front][i]] != 0)
          continue;
        ans[m[front][i]] = front;
        count++;
        q.push(m[front][i]);
      }
    }
  }
  if (count != n - 1) {
    cout << "No" << endl;
  } else {
    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;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

const string YES = "Yes";
V<V<int>> m;

void solve(int N, int M) {
  V<int> ans(N + 1);
  queue<int> q;
  q.push(1);
  int tmp, n;
  while (!q.empty()) {
    n = q.front();
    q.pop();
    REP(i, m[n].size()) {
      tmp = m[n][i];
      if (ans[tmp] == 0) {
        ans[tmp] = n;
        q.push(tmp);
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 2, N + 1) { cout << ans[i] << endl; }
}

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, N + 1) {
    V<int> a;
    m.push_back(a);
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

const string YES = "Yes";
V<V<int>> m;

void solve(int N, int M) {
  V<int> ans(N + 1);
  queue<int> q;
  q.push(1);
  int tmp, n;
  while (!q.empty()) {
    n = q.front();
    q.pop();
    REP(i, m[n].size()) {
      tmp = m[n][i];
      if (ans[tmp] == 0) {
        ans[tmp] = n;
        q.push(tmp);
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 2, N + 1) { cout << ans[i] << endl; }
}

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M + 1) {
    V<int> a;
    m.push_back(a);
  }

  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    m[a].push_back(b);
    m[b].push_back(a);
  }
  solve(N, M);
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (int i = 0; i < g[node].size(); i++) {
      int next = g[node][i];
      if (dist[next] == -1) {
        dist[next] = node;
        q.push(next);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << dist[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

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

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

typedef long long ll;
typedef long double ld;
static const double pi = 3.14159265359;
static const int inf = (1 << 21);
static const int mod = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Graph(N); // 隣接行列
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    Graph.at(A).push_back(B);
    Graph.at(B).push_back(A);
  }
  queue<int> q;
  q.push(0);
  vector<int> parent(N, -1); // ノードの親を判定　-1だと無い
  parent.at(0) = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int n : Graph.at(v)) {
      if (parent.at(n) != -1) {
        continue;
      }
      parent.at(n) = v;
      q.push(n);
    }
  }
  bool J = true;
  for (int i = 1; i < N; i++) {
    if (parent.at(i) == -1) {
      J = false;
    }
  }
  if (J) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << parent.at(i) + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define ll long long
#define ld long double
static const double pi = 3.14159265359;
static const int INF = (1 << 21);

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Graph(N, vector<int>(N)); // 隣接行列
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    Graph.at(A).push_back(B);
    Graph.at(B).push_back(A);
  }
  queue<int> q;
  q.push(0);
  vector<int> parent(N, -1); // ノードの親を判定　-1だと無い
  parent.at(0) = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int n : Graph.at(v)) {
      if (parent.at(n) != -1) {
        continue;
      }
      parent.at(n) = v;
      q.push(n);
    }
  }
  bool J = true;
  for (int i = 1; i < N; i++) {
    if (parent.at(i) == -1) {
      J = false;
    }
  }
  if (J) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << parent.at(i) + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long n, m;
vector<int> connect[200001];
queue<int> searchq;
static int dist[200001];
static int flag[200001];

void bfs() {
  while (!searchq.empty()) {
    int s = searchq.front();
    searchq.pop();
    for (int i = 0; i < connect[s].size(); i++) {
      if (dist[s] + 1 < dist[connect[s][i]]) {
        dist[connect[s][i]] = dist[s] + 1;
        flag[connect[s][i]] = s;
        searchq.push(connect[s][i]);
      }
    }
  }
}

int main() {
  bool check = true;

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

  for (int i = 1; i <= n; i++) {
    dist[i] = 999999;
    flag[i] = 999999;
  }
  dist[1] = 0;

  searchq.push(1);
  bfs();

  for (int i = 2; i <= n; i++) {
    if (flag[i] == 999999) {
      check = false;
      break;
    }
  }

  if (check) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << flag[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long n, m;
vector<vector<int>> edge;
vector<int> connect[200001];
queue<int> searchq;
static int dist[200001];
static int flag[200001];

void dijkstra() {
  if (searchq.empty()) {
    return;
  }
  int s = searchq.front();
  searchq.pop();
  for (int i = 0; i < connect[s].size(); i++) {
    if (dist[s] + 1 < dist[connect[s][i]]) {
      dist[connect[s][i]] = dist[s] + 1;
      flag[connect[s][i]] = s;
      searchq.push(connect[s][i]);
      dijkstra();
    }
  }
}

int main() {
  bool check = true;

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

  for (int i = 1; i <= n; i++) {
    dist[i] = 999999;
    flag[i] = 999999;
  }
  dist[1] = 0;

  searchq.push(1);
  dijkstra();

  for (int i = 2; i <= n; i++) {
    if (flag[i] == 999999) {
      check = false;
      break;
    }
  }

  if (check) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << flag[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

void Main() {
  int N, M;
  cin >> N >> M;
  map<int, vector<int>> path;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    if (path.count(a) == 0) {
      path.insert(make_pair(a, vector<int>()));
    }
    path[a].push_back(b);
    if (path.count(b) == 0) {
      path.insert(make_pair(b, vector<int>()));
    }
    path[b].push_back(a);
  }

  vector<int> check(N, -1);
  queue<int> q;
  q.push(0);
  check[0] = 0;
  while (q.size() > 0) {
    int curr = q.front();
    q.pop();
    const vector<int> &to = path[curr];
    for (auto e : to) {
      if (check[e] == -1) {
        q.push(e);
        check[e] = curr;
      }
    }
  }

  bool reachedAll = true;
  for (int i = 0; i < N; ++i) {
    if (check[i] < 0) {
      reachedAll = false;
      break;
    }
  }

  if (reachedAll) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; ++i) {
      cout << check[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

struct RoomInstruction {
  RoomInstruction() : shortestPath(0), prevRoom(0) {}
  int shortestPath;
  int prevRoom;
};

void dp(int curr, int prev, int accumPath,
        const map<int, vector<int>> &pathFrom,
        map<int, RoomInstruction> &rootMap) {
  if (prev > 0) {
    if (rootMap.count(curr) == 0) {
      RoomInstruction ri;
      ri.shortestPath = accumPath;
      ri.prevRoom = prev;
      rootMap.insert(make_pair(curr, ri));
    } else {
      rootMap[curr].prevRoom = prev;
      rootMap[curr].shortestPath = accumPath;
    }
  }

  const vector<int> &toPath = pathFrom.at(curr);
  for (auto e : toPath) {
    if (rootMap.count(e) > 0) {
      if (rootMap[e].shortestPath > accumPath) {
        dp(e, curr, accumPath + 1, pathFrom, rootMap);
      }
    } else {
      dp(e, curr, accumPath + 1, pathFrom, rootMap);
    }
  }
}

void Main() {
  int N, M;
  cin >> N >> M;
  map<int, vector<int>> pathFrom;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    if (pathFrom.count(a) == 0) {
      pathFrom.insert(make_pair(a, vector<int>()));
    }
    pathFrom[a].push_back(b);
    if (pathFrom.count(b) == 0) {
      pathFrom.insert(make_pair(b, vector<int>()));
    }
    pathFrom[b].push_back(a);
  }

  map<int, RoomInstruction> rootMap;
  dp(1, 0, 0, pathFrom, rootMap);
  if (rootMap.size() < N) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; ++i) {
      const RoomInstruction &inst = rootMap[i];
      cout << inst.prevRoom << endl;
    }
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));

vector<int> v[100001];

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

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

  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));

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

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

  cout << endl;
  return 0;
}