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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
#define ll long long

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int pointer[200000];

  for (int i = 0; i < 200000; i++)
    pointer[i] = -1;
  pointer[0] = 0;

  vector<vector<int>> adj;
  for (int i = 0; i < n; i++) {
    vector<int> tmp;
    adj.push_back(tmp);
  }

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

  queue<int> check_nodes;
  check_nodes.push(0);
  while (!check_nodes.empty()) {
    int cn = check_nodes.front();
    check_nodes.pop();
    for (int ad : adj[cn]) {
      if (pointer[ad] == -1) {
        pointer[ad] = cn;
        check_nodes.push(ad);
      }
    }
  }

  bool found = true;
  for (int i = 0; i < n; i++) {
    if (pointer[i] == -1) {
      found = false;
      break;
    }
  }

  if (found) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << pointer[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
#define ll long long

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int pointer[20000];

  for (int i = 0; i < n; i++)
    pointer[i] = -1;
  pointer[0] = 0;

  vector<vector<int>> adj;
  for (int i = 0; i < n; i++) {
    vector<int> tmp;
    adj.push_back(tmp);
  }

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

  queue<int> check_nodes;
  check_nodes.push(0);
  while (!check_nodes.empty()) {
    int cn = check_nodes.front();
    check_nodes.pop();
    for (int ad : adj[cn]) {
      if (pointer[ad] == -1) {
        // cout << "found " <<cn+1 << " " << ad+1 << endl;
        pointer[ad] = cn;
        check_nodes.push(ad);
      }
    }
  }

  bool found = true;
  for (int i = 0; i < n; i++) {
    if (pointer[i] == -1) {
      found = false;
      break;
    }
  }

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

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> v(N + 1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v.at(A).push_back(B);
    v.at(B).push_back(A);
  }
  vector<int> ans(N + 1, -1);
  queue<int> que;
  // vector<bool> flag(N+1,false);
  // vector<int> pre(N+1,-1);
  // flag.at(1)=true;
  que.push(1);
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (auto y : v.at(x)) {
      if (ans.at(y) == -1) {
        // flag.at(y)=true;
        que.push(y);
        // pre.at(y)=x;
        ans.at(y) = x;
      }
    }
  }
  // if(pre.at(1)==-1){
  //   cout << "No" << endl;
  //   return 0;
  // }
  for (int i = 2; i <= N; i++) {
    if (ans.at(i) == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << ans.at(i) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> v(N + 1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v.at(A).push_back(B);
    v.at(B).push_back(A);
  }
  vector<int> ans;
  for (int i = 2; i <= N; i++) {
    queue<int> que;
    vector<bool> flag(N + 1, false);
    vector<int> pre(N + 1, -1);
    flag.at(i) = true;
    que.push(i);
    while (!que.empty()) {
      int x = que.front();
      que.pop();
      if (x == 1) {
        break;
      }
      for (auto y : v.at(x)) {
        if (!flag.at(y)) {
          flag.at(y) = true;
          que.push(y);
          pre.at(y) = x;
        }
      }
    }
    if (pre.at(1) == -1) {
      cout << "No" << endl;
      return 0;
    }
    int x = pre.at(1);
    if (x == i) {
      ans.push_back(1);
      continue;
    }
    while (true) {
      int y = pre.at(x);
      if (y == i) {
        ans.push_back(x);
        break;
      }
      x = y;
    }
  }
  cout << "Yes" << endl;
  for (auto p : ans) {
    cout << p << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <queue>
// 配列
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;

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

Error: TLE

Fixed submission:


#include <iostream>
#include <queue>
// 配列
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;

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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;
const int nax = 1e6 + 6;
vector<ll> adj[nax];
ll depth[nax];
bool visited[nax];

int main() {

  ll n, m;
  cin >> n >> m;

  for (ll i = 1; i <= n; i++) {
    visited[i] = false;
    depth[i] = INT_MAX;
  }
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  depth[1] = 0;
  queue<ll> q;
  q.push(1);
  ll ans[n + 1];
  while (!q.empty()) {
    ll v = q.front();
    ll mind = INT_MAX;
    q.pop();

    visited[v] = true;
    for (ll i = 0; i < adj[v].size(); i++) {

      if (visited[adj[v][i]] == false) {
        depth[adj[v][i]] = depth[v] + 1;
        q.push(adj[v][i]);
        visited[adj[v][i]] = true;
      }
      if (depth[adj[v][i]] < mind) {
        ans[v] = adj[v][i];
        mind = depth[adj[v][i]];
      }
    }
  }
  bool possible = true;
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      possible = false;
      break;
    }
  }
  if (!possible)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (ll i = 2; i <= n; i++) {

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;
const int nax = 1e6 + 6;

int main() {

  ll n, m;
  cin >> n >> m;
  vector<ll> adj[m + 1];
  ll depth[n + 1];
  bool visited[n + 1];
  for (ll i = 1; i <= n; i++) {
    visited[i] = false;
    depth[i] = INT_MAX;
  }
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  depth[1] = 0;
  queue<ll> q;
  q.push(1);
  ll ans[n + 1];
  while (!q.empty()) {
    ll v = q.front();
    ll mind = INT_MAX;
    q.pop();

    visited[v] = true;
    for (ll i = 0; i < adj[v].size(); i++) {

      if (visited[adj[v][i]] == false) {
        depth[adj[v][i]] = depth[v] + 1;
        q.push(adj[v][i]);
        visited[adj[v][i]] = true;
      }
      if (depth[adj[v][i]] < mind) {
        ans[v] = adj[v][i];
        mind = depth[adj[v][i]];
      }
    }
  }
  bool possible = true;
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      possible = false;
      break;
    }
  }
  if (!possible)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (ll i = 2; i <= n; i++) {

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

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

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

using namespace std;

using PA = pair<int, int>;

vector<int> E[102400];
int D[102400];
bool V[102400];
int P[102400];

int main() {
  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    E[a - 1].push_back(b - 1);
    E[b - 1].push_back(a - 1);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1000000;
    V[i] = false;
    P[i] = -1;
  }

  D[0] = 0;
  P[0] = 1;

  priority_queue<PA, vector<PA>, greater<PA>> que;
  que.push(PA(0, 0));

  while (!que.empty()) {
    PA p = que.top();
    int ic = p.second;
    que.pop();

    for_each(E[ic].begin(), E[ic].end(), [&](int &hi) {
      if (D[hi] > D[ic] + 1) {
        D[hi] = D[ic] + 1;
        P[hi] = ic;
        que.push(PA(D[ic] + 1, hi));
      }
    });

    V[ic] = true;
  }

  bool f = true;
  for (int i = 1; i < N; i++)
    f &= P[i] != -1;

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


Error: TLE

Fixed submission:

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

using namespace std;

vector<int> E[102400];
int D[102400];
bool V[102400];
int P[102400];

int main() {
  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    E[a - 1].push_back(b - 1);
    E[b - 1].push_back(a - 1);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1000000;
    V[i] = false;
    P[i] = -1;
  }

  D[0] = 0;
  P[0] = 1;

  for (int n = 0; n < N; n++) {
    int ti, ic;
    ti = 1000000;
    ic = N;
    for (int i = 0; i < N; i++) {
      if (!V[i] && D[i] < ti) {
        ti = D[i];
        ic = i;
      }
    }

    if (ic == N) {
      cout << "No" << endl;
      return 0;
    }

    for_each(E[ic].begin(), E[ic].end(), [&](int &hi) {
      if (D[hi] > D[ic] + 1) {
        D[hi] = D[ic] + 1;
        P[hi] = ic;
      }
    });

    V[ic] = true;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int flag;
  int dist;
  int michi;
  vector<int> next;
} ROOM;

int main() {
  int N, M, A, B;
  queue<int> toVisit;
  cin >> N >> M;
  ROOM room[N];
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    A--;
    B--;
    room[A].next.push_back(B);
    room[B].next.push_back(A);
  }
  for (int i = 0; i < N; i++) {
    room[i].dist = 1e9;
    room[i].flag = 0;
    if (room[i].next.size() == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  int now = 0, s, minp, mind;
  room[0].flag = 1;
  room[0].dist = 0;
  room[0].michi = 0;
  while (1) {
    for (int i = 0; i < room[now].next.size(); i++) {
      s = room[now].next.at(i);
      if (room[now].dist + 1 < room[s].dist) {
        room[s].dist = room[now].dist + 1;
        room[s].michi = now;
        if (room[s].flag == 0)
          toVisit.push(s);
      }
    }
    if (toVisit.size() == 0)
      break;
    now = toVisit.front();
    toVisit.pop();
    room[now].flag = 1;
  }
  for (int i = 1; i < N; i++) {
    cout << room[i].michi + 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int flag;
  int dist;
  int michi;
  vector<int> next;
} ROOM;

int main() {
  int N, M, A, B;
  queue<int> toVisit;
  cin >> N >> M;
  ROOM room[N];
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    A--;
    B--;
    room[A].next.push_back(B);
    room[B].next.push_back(A);
  }
  for (int i = 0; i < N; i++) {
    room[i].dist = 1e9;
    room[i].flag = 0;
    if (room[i].next.size() == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  int now = 0, s, minp, mind;
  room[0].flag = 1;
  room[0].dist = 0;
  room[0].michi = 0;
  while (1) {
    for (int i = 0; i < room[now].next.size(); i++) {
      s = room[now].next.at(i);
      if (room[now].dist + 1 < room[s].dist) {
        room[s].dist = room[now].dist + 1;
        room[s].michi = now;
      }
      if (room[s].flag == 0)
        toVisit.push(s);
    }

    minp = -1;
    mind = 1e9;
    for (int i = 1; i < N; i++) {
      if (room[i].flag == 0 && room[i].dist < mind) {
        minp = i;
        mind = room[i].dist;
      }
    }
    if (minp < 0)
      break;
    now = minp;
    /*
    if(toVisit.size()==0) break;
    now=toVisit.front();
    toVisit.pop();
    */
    room[now].flag = 1;
  }
  for (int i = 1; i < N; i++) {
    cout << room[i].michi + 1 << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
using namespace std;
int main(void)

{
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> v(n);
  vector<ll> dis(n, (ll)INF), prev(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  priority_queue<ll> q;
  q.push(0);
  dis[0] = 0;
  ll cta = 1;
  while (!q.empty()) {
    int temp = q.top();
    q.pop();
    for (auto i : v[temp]) {
      if (dis[i] > dis[temp] + 1)
        dis[i] = dis[temp] + 1, q.push(i), prev[i] = temp, ++cta;
    }
  }
  if (cta < n)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    rep(i, 1, n) printf("%lld\\n", prev[i] + 1);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
using namespace std;
int main(void)

{
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> v(n);
  vector<ll> dis(n, (ll)INF), prev(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  priority_queue<ll> q;
  q.push(0);
  dis[0] = 0;
  ll cta = 1;
  while (!q.empty()) {
    int temp = q.top();
    q.pop();
    for (auto i : v[temp]) {
      if (dis[i] >= dis[temp] + 1)
        dis[i] = dis[temp] + 1, q.push(i), prev[i] = temp, ++cta;
    }
  }
  if (cta < n)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    rep(i, 1, n) printf("%d\\n", prev[i] + 1);
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;

typedef struct edge_ {
  int to;
  ll cost;
} edge;

vector<vector<edge>> G;
vector<ll> dist, par;
ll INF = 9999999999999;

void dijkstra(int n, int s) {
  dist.resize(n);
  par.resize(n);
  for (int i = 0; i < n; i++)
    dist[i] = INF;
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> pque;

  pque.push(P((ll)0, s));

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();
    int u = p.second;
    ll d = p.first;
    if (d > dist[u])
      continue;

    for (int i = 0; i < G[u].size(); i++) {
      edge e = G[u][i];
      int v = e.to;
      ll c = e.cost;
      if (dist[v] <= dist[u] + c)
        continue;

      dist[v] = dist[u] + c;
      pque.push(P(dist[v], v));
      par[v] = u;
    }
  }
}

// nは頂点数、mは辺数
// 受け取る頂点番号を−１していることに注意
void build_graph(int n, ll m) {
  G.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge e1 = {b, 1}, e2 = {a, 1};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  build_graph(n, m);
  dijkstra(n, 0);

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;

typedef struct edge_ {
  int to;
  ll cost;
} edge;

vector<vector<edge>> G;
vector<ll> dist, par;
ll INF = 999999999999;

void dijkstra(int n, int s) {
  for (int i = 0; i < n; i++)
    dist[i] = INF;
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> pque;

  pque.push(P((ll)0, s));

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();
    int u = p.second;
    ll d = p.first;
    if (d > dist[u])
      continue;

    for (int i = 0; i < G[u].size(); i++) {
      edge e = G[u][i];
      int v = e.to;
      ll c = e.cost;
      if (dist[v] < d + c)
        continue;

      dist[v] = d + c;
      pque.push(P(dist[v], v));
      par[v] = u;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  dist.resize(n);
  par.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge e1 = {b, 1}, e2 = {a, 1};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }

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

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

int N, M;
// std::vector<int> A, B;
int A, B;
std::vector<std::vector<int>> adj_list;
std::vector<int> sign;
std::queue<int> que;

int main() {
  std::cin >> N >> M;
  // A.assign(M, 0);
  // B.assign(M, 0);
  adj_list.assign(N, std::vector<int>());
  sign.assign(N, -1);
  for (int j = 0; j < N; j++) {
    adj_list.at(j).reserve(N);
  }

  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    --A;
    --B;
    adj_list.at(A).push_back(B);
    adj_list.at(B).push_back(A);
  }
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : adj_list.at(v)) {
      if (sign.at(nv) == -1) {
        sign.at(nv) = v;
        que.push(nv);
      }
    }
  }

  std::cout << "Yes" << std::endl;
  for (int j = 1; j < N; j++) {
    std::cout << sign.at(j) + 1 << std::endl;
  }
}


Error: 0

Fixed submission:

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

int N, M;
// std::vector<int> A, B;
int A, B;
std::vector<std::vector<int>> adj_list;
std::vector<int> sign;
std::queue<int> que;

int main() {
  std::cin >> N >> M;
  // A.assign(M, 0);
  // B.assign(M, 0);
  adj_list.assign(M, std::vector<int>());
  sign.assign(N, -1);
  for (int j = 0; j < N; j++) {
    adj_list.at(j).reserve(N);
  }

  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    --A;
    --B;
    adj_list.at(A).push_back(B);
    adj_list.at(B).push_back(A);
  }
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : adj_list.at(v)) {
      if (sign.at(nv) == -1) {
        sign.at(nv) = v;
        que.push(nv);
      }
    }
  }

  std::cout << "Yes" << std::endl;
  for (int j = 1; j < N; j++) {
    std::cout << sign.at(j) + 1 << std::endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \\
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

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

  vector<ll> michishirube(N, -1);
  queue<ll> q;
  vector<bool> isVisited(N, false);
  vector<bool> isAdd(N, false);
  q.push(0);
  isAdd[0] = true;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    isVisited[p] = true;
    REPV(it, G[p]) {
      if (!isVisited[*it] && !isAdd[*it]) {
        q.push(*it);
        isAdd[*it] = true;
        if (michishirube[*it] == -1) {
          michishirube[*it] = p;
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; ++i) {
    cout << michishirube[i] + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \\
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

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

  vector<ll> michishirube(N, -1);
  queue<ll> q;
  vector<bool> isVisited(N, false);
  vector<bool> isAdd(N, false);
  q.push(0);
  isAdd[0] = true;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    isVisited[p] = true;
    REPV(it, G[p]) {
      if (!isVisited[*it] || !isAdd[*it]) {
        q.push(*it);
        isAdd[*it] = true;
        if (michishirube[*it] == -1) {
          michishirube[*it] = p;
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; ++i) {
    cout << michishirube[i] + 1 << endl;
  }
}