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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
#define pb push_back

/*int pare(int en,int start,int par[])
{
        if(par[en]==start)
        {
                return en;
        }
        pare(par[en],start,par);
}*/

int main() {
  int tst = 1;
  // scanf("%d",&tst);
  while (tst--) {
    int n, m, i, j, c = 0;
    cin >> n >> m;
    vector<int> g[n + 1], v;
    for (i = 0; i < m; i++) {
      int n1, n2;
      cin >> n1 >> n2;
      g[n1].pb(n2);
      g[n2].pb(n1);
    }

    bool vis[n + 1];
    int par[n + 1];
    memset(vis, false, sizeof(vis));
    queue<int> q;
    q.push(1);
    vis[1] = true;
    while (!q.empty()) {
      int top = q.front();
      q.pop();
      for (j = 0; j < (int)g[top].size(); j++) {
        int tt = g[top][j];
        if (vis[tt] == false) {
          vis[tt] = true;
          par[tt] = top;
          q.push(tt);
        }
      }
    }
    for (i = 1; i <= n; i++) {
      if (vis[i] == false) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    for (i = 2; i <= n; i++) {
      cout << par[i] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
#define pb push_back

/*int pare(int en,int start,int par[])
{
        if(par[en]==start)
        {
                return en;
        }
        pare(par[en],start,par);
}*/

int main() {
  int tst = 1;
  // scanf("%d",&tst);
  while (tst--) {
    int n, m, i, j, c = 0;
    cin >> n >> m;
    vector<int> g[n + 1], v;
    for (i = 0; i < m; i++) {
      int n1, n2;
      cin >> n1 >> n2;
      g[n1].pb(n2);
      g[n2].pb(n1);
    }

    for (i = 2; i <= n; i++) {
      bool vis[n + 1];
      int par[n + 1];
      memset(vis, false, sizeof(vis));
      queue<int> q;
      q.push(i);
      vis[i] = true;
      while (!q.empty()) {
        int top = q.front();
        q.pop();
        for (j = 0; j < (int)g[top].size(); j++) {
          int tt = g[top][j];
          if (tt == 1) {
            c = 1;
            par[1] = top;
            break;
          }
          if (vis[tt] == false) {
            vis[tt] = true;
            par[tt] = top;
            q.push(tt);
          }
        }
        if (c)
          break;
      }
      if (!c) {
        cout << "No" << endl;
        return 0;
      } else {
        c = 0;
        int en = 1;
        int st = i;
        while (1) {
          if (par[en] == st) {
            v.pb(en);
            break;
          } else {
            en = par[en];
          }
        }
      }
    }
    cout << "Yes" << endl;
    for (i = 0; i < n - 1; i++) {
      cout << v[i] << endl;
    }
  }
  return 0;
}
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>;

ll N, M, A, B;

void search(int start, vector<vector<ll>> &path, vector<ll> &dis,
            vector<ll> &prev) {
  priority_queue<P, vector<P>, greater<P>> pq;
  dis.at(start) = 0;
  pq.emplace(dis.at(start), start);
  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();
    int room = p.second;
    if (dis.at(room) < p.first)
      continue;
    for (auto &x : path.at(room)) {
      if (dis.at(x) > dis.at(room) + 1) {
        dis.at(x) = dis.at(room) + 1;
        prev.at(x) = room;
        pq.emplace(dis.at(x), x);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  vector<vector<ll>> path(N + 1);
  vector<ll> dis(N + 1, INFINITY), prev(N + 1, -1);
  rep(i, M) {
    cin >> A >> B;
    path.at(A).push_back(B);
    path.at(B).push_back(A);
  }
  for (int i = 1; i <= N; ++i) {
    if (path.at(i).empty()) {
      cout << "No" << endl;
      return 0;
    }
  }

  search(1, path, dis, prev);
  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i)
    cout << prev.at(i) << 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>;

ll N, M, A, B;

void search(int room, int preroom, ll n, vector<vector<ll>> &path,
            vector<ll> &cnt, vector<ll> &dir) {
  if (cnt.at(room) <= n)
    return;
  else {
    dir.at(room) = preroom;
    cnt.at(room) = n;
  }
  for (auto x : path.at(room))
    search(x, room, n + 1, path, cnt, dir);
}

int main() {
  cin >> N >> M;
  vector<vector<ll>> path(N + 1);
  vector<ll> cnt(N + 1, 10e6), dir(N + 1);
  rep(i, M) {
    cin >> A >> B;
    path.at(A).push_back(B);
    path.at(B).push_back(A);
  }
  for (int i = 1; i <= N; ++i) {
    if (path.at(i).empty()) {
      cout << "No" << endl;
      return 0;
    }
  }
  search(1, 0, 0, path, cnt, dir);
  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i)
    cout << dir.at(i) << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

#define PT pair<ll, int>

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

  int N, M;
  cin >> N >> M;
  vector<PT> E[100001];
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    E[a].pb({1, b});
    E[b].pb({1, a});
  }

  ll D[100001];
  rep1(i, N) D[i] = 1e18;
  D[1] = 0;

  priority_queue<PT, vector<PT>, greater<PT>> que;

  que.push(mp(0, 1));
  while (que.size()) {
    PT p = que.top();
    que.pop();
    int i = p.second;
    if (D[i] != p.first)
      continue;
    for (PT p2 : E[i]) {
      int j = p2.second;
      ll d = D[i] + p2.first;
      if (D[j] > d) {
        D[j] = d;
        que.push(mp(d, j));
      }
    }
  }

  int kotae[100001];
  for (int i = 2; i <= N; i++) {

    ll d = D[i];
    for (PT p2 : E[i]) {
      int j = p2.second;
      if (D[j] == d - 1) {
        kotae[i] = j;
      }
    }
  }

  co("Yes");
  for (int i = 2; i <= N; i++)
    co(kotae[i]);

  Would you please return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

#define PT pair<ll, int>

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

  int N, M;
  cin >> N >> M;
  vector<PT> E[100000];
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    E[a].pb({1, b});
    E[b].pb({1, a});
  }

  ll D[100001];
  rep1(i, N) D[i] = 1e18;
  D[1] = 0;

  priority_queue<PT, vector<PT>, greater<PT>> que;

  que.push(mp(0, 1));
  while (que.size()) {
    PT p = que.top();
    que.pop();
    int i = p.second;
    if (D[i] != p.first)
      continue;
    for (PT p2 : E[i]) {
      int j = p2.second;
      ll d = D[i] + p2.first;
      if (D[j] > d) {
        D[j] = d;
        que.push(mp(d, j));
      }
    }
  }

  int kotae[100001];
  for (int i = 2; i <= N; i++) {

    ll d = D[i];
    for (PT p2 : E[i]) {
      int j = p2.second;
      if (D[j] == d - 1) {
        kotae[i] = j;
      }
    }
  }

  co("Yes");
  for (int i = 2; i <= N; i++)
    co(kotae[i]);

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

#include <iostream>
#include <queue>
#include <vector>
int ans[200005];
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  queue<int> q;
  vector<int> u[200005];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x == 1) {
      q.push(y);
      ans[y] = 1;
    } else
      u[x].push_back(y);
    if (y == 1) {
      q.push(x);
      ans[x] = 1;
    } else
      u[y].push_back(x);
  }

  while (!q.empty()) {
    int now = q.front();
    for (int i = 0; i < u[now].size(); i++) {
      if (ans[u[now][i]] == 0) {
        ans[u[now][i]] = now;
        q.push(u[now][i]);
      }
    }
    q.pop();
  }
  for (int i = 2; i <= n; i++)
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
int ans[20005];
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  queue<int> q;
  vector<int> u[20005];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x == 1) {
      q.push(y);
      ans[y] = 1;
    } else
      u[x].push_back(y);
    if (y == 1) {
      q.push(x);
      ans[x] = 1;
    } else
      u[y].push_back(x);
  }

  while (!q.empty()) {
    int now = q.front();
    for (int i = 0; i < u[now].size(); i++) {
      if (ans[u[now][i]] == 0) {
        ans[u[now][i]] = now;
        q.push(u[now][i]);
      }
    }
    q.pop();
  }
  for (int i = 2; i <= n; i++)
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << 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)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vv(n + 1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    vv[a].push_back(b);
    vv[b].push_back(a);
  }
  map<int, int> ans;
  vector<int> que{1};
  while (true) {
    int flag = 1;
    vector<int> temp;
    for (int &q : que) {
      for (int &v : vv[q]) {
        if (ans[v] == 0) {
          ans[v] = q;
          flag = 0;
          temp.push_back(v);
        }
      }
    }
    if (flag)
      break;
    swap(temp, que);
  }
  for (int i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vv(n + 1, vector<int>(n + 1, 0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    vv[a][b] = 1;
    vv[b][a] = 1;
  }
  map<int, int> ans;
  vector<int> que{1};
  while (true) {
    int flag = 1;
    vector<int> temp;
    for (int &q : que) {
      for (int i = 2; i <= n; i++) {
        if (vv[q][i] == 1 && ans[i] == 0) {
          ans[i] = q;
          flag = 0;
          temp.push_back(i);
        }
      }
    }
    if (flag)
      break;
    swap(temp, que);
  }
  for (int i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;

  int A[M], B[M];
  Graph G(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  queue<int> q;
  vector<int> dist(N, INF);
  vector<int> an(N, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : G[v]) {
      if (dist[nv] <= dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      an[nv] = v;
      q.push(nv);
    }
  }
  string ans = "Yes";
  rep(i, N) {
    if (dist[i] == INF) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
  if (ans == "Yes") {
    for (int i = 1; i < N; i++) {
      cout << an[i] + 1 << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;

  int A[M], B[M];
  Graph G(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  queue<int> q;
  vector<int> dist(N, INF);
  vector<int> an(N, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      an[nv] = v;
      q.push(nv);
    }
  }
  string ans = "Yes";
  rep(i, N) {
    if (dist[i] == INF) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
  if (ans == "Yes") {
    for (int i = 1; i < N; i++) {
      cout << an[i] + 1 << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep1(i, N) for (ll i = 1; i <= N; i++)
#define repr(i, N) for (ll i = N - 1; i >= 0; i--)
#define repr1(i, N) for (ll i = N; i > 0; i--)

#define MOD 1000000007

using ll = long long;
using namespace std;

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vpi;
typedef pair<ll, ll> Pl;
typedef vector<ll> vl;
typedef vector<Pl> vpl;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b <= 0)
    return -1;
  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<vi> ab(N);
  // vector<vector<bool>> ch(N, vector<bool>(N ,false));
  vi ans(N, -1);
  ans[0] = 0;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ab[a].push_back(b);
    ab[b].push_back(a);
  }

  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int nr = que.front();
    que.pop();
    rep(i, ab[nr].size()) {
      if (ans[ab[nr][i]] != -1)
        continue;
      ans[ab[nr][i]] = nr;
      que.push(ab[nr][i]);
    }
  }

  bool flag = true;
  rep(i, N) {
    if (ans[i] == -1) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes"
         << "\\n";
    rep1(i, N - 1) cout << (ans[i] + 1) << "\\n";
  } else {
    cout << "No"
         << "\\n";
  }
  // string ans = ( flag ? "Yes" : "No" );
  // printf("%.8lf", ans);
  // cout << ans;
  // cout << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep1(i, N) for (ll i = 1; i <= N; i++)
#define repr(i, N) for (ll i = N - 1; i >= 0; i--)
#define repr1(i, N) for (ll i = N; i > 0; i--)

#define MOD 1000000007

using ll = long long;
using namespace std;

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vpi;
typedef pair<ll, ll> Pl;
typedef vector<ll> vl;
typedef vector<Pl> vpl;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b <= 0)
    return -1;
  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<bool>> ch(N, vector<bool>(N, false));
  vi ans(N, -1);
  ans[0] = 0;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ch[a][b] = true;
    ch[b][a] = true;
  }

  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int nr = que.front();
    que.pop();
    rep(i, N) {
      if (i != nr && ch[nr][i] && ans[i] == -1) {
        que.push(i);
        ans[i] = nr;
      }
    }
  }

  bool flag = true;
  rep(i, N) {
    if (ans[i] == -1) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes"
         << "\\n";
    rep1(i, N - 1) cout << (ans[i] + 1) << "\\n";
  } else {
    cout << "No"
         << "\\n";
  }
  // string ans = ( flag ? "Yes" : "No" );
  // printf("%.8lf", ans);
  // cout << ans;
  // cout << "\\n";
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;
vector<int> visited;
vector<int> res;
vector<vector<int>> G;

int main() {
  ll n, m;
  cin >> n >> m;
  visited.resize(n + 1);
  res.resize(n + 1);
  G.resize(n + 1);

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

  queue<int> que;
  que.push(1);
  res[1] = 0;
  visited[1] = 1;
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    for (int nv : G[v]) {
      if (visited[nv] != -1)
        continue;
      else
        visited[nv] = 1;
      que.push(nv);
      res[nv] = v;
    }
  }

  if (G[1].size() == 0)
    cout << "No" << '\\n';
  else {
    cout << "Yes" << '\\n';
    for (int i = 2; i <= n; i++)
      cout << res[i] << '\\n';
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;
vector<int> visited;
vector<int> res;
vector<int> ranks;
vector<vector<int>> G;
ll n, m;

int dfs(int num, int rank, int back) {
  if (num == n + 1)
    return 0;
  visited[num] = 1;
  if (ranks[num] > rank) {
    ranks[num] = rank;
    res[num] = back;
  }
  for (int i = 0; i < G[num].size(); i++) {
    if (visited[G[num][i]] == 0)
      dfs(G[num][i], rank + 1, num);
  }
}

int main() {
  cin >> n >> m;
  visited.resize(n + 1);
  res.resize(n + 1);
  G.resize(n + 1);

  rep(i, n + 1) visited[i] = 0;
  rep(i, n + 1) ranks[i] = INT_MAX;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs(1, 1, 0);

  if (G[1].size() == 0)
    cout << "No";
  else {
    cout << "Yes" << '\\n';
    rep(i, n) cout << res[i + 1] << '\\n';
  }

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

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

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

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

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

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

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

    // v から辿れる頂点をすべて調べる
    for (long long int nv : G[v]) {
      if (dist[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

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

  bool flag = false;
  for (long long int i = 1; i < N; i++) {
    if (parent[i] == -100)
      flag = true;
  }

  if (flag == true)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (long long int v = 1; v < N; ++v) {
      cout << parent[v] + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

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

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

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

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

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

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

  bool flag = false;
  for (int i = 1; i < N; i++) {
    if (parent[i] == -100)
      flag = true;
  }

  if (flag == true)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int v = 1; v < N; ++v) {
      cout << parent[v] + 1 << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m;
bool v[100001], unvisited = false;
int ans[100001];
int dis[100001];
vector<int> adj[100001], newcurr;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  newcurr.push_back(1);
  int distance = 0;
  while (0 == 0) {
    vector<int> curr = newcurr;
    newcurr.clear();
    if (curr.size() == 0) {
      break;
    }
    for (int i = 0; i < curr.size(); i++) {
      v[curr[i]] = true;
      dis[curr[i]] = distance;
    }
    distance++;
    for (int i = 0; i < curr.size(); i++) {
      for (int j = 0; j < adj[curr[i]].size(); j++) {
        if (!v[adj[curr[i]][j]]) {
          ans[adj[curr[i]][j]] = curr[i];
          v[adj[curr[i]][j]] = true;
          newcurr.push_back(adj[curr[i]][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      unvisited = true;
  }
  if (unvisited) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m;
bool v[100001], unvisited = false;
int ans[100001];
int dis[100001];
vector<int> adj[100001], newcurr;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  newcurr.push_back(1);
  int distance = 0;
  while (0 == 0) {
    vector<int> curr = newcurr;
    newcurr.clear();
    if (curr.size() == 0) {
      break;
    }
    for (int i = 0; i < curr.size(); i++) {
      v[curr[i]] = true;
      dis[curr[i]] = distance;
    }
    distance++;
    for (int i = 0; i < curr.size(); i++) {
      for (int j = 0; j < adj[curr[i]].size(); j++) {
        if (!v[adj[curr[i]][j]]) {
          ans[adj[curr[i]][j]] = curr[i];
          newcurr.push_back(adj[curr[i]][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      unvisited = true;
  }
  if (unvisited) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << endl;
    }
  }
}