alexjercan/bugnet huggingface dataset:
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 FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int N, M;
vector<vector<int>> A(0, vector<int>(0));
vector<int> touroku(0);
vector<P> ans(0); // f:行き先 :0への距離
struct qi {
  int a;
  int b;
  int c;
};
queue<tuple<int, int, int>> lol{};
void dfs(int heyanum, int kyori, int previd) {
  if (ans[heyanum].second <= kyori && previd != touroku[heyanum]) {
    return;
  }
  REP(i, A[heyanum].size()) {
    int id = A[heyanum][i];
    if (previd == id)
      continue;
    if (kyori + 1 < ans[id].second) {
      ans[id] = P(heyanum, kyori + 1);
      touroku[id] = heyanum;
      // tuple<int, int, int> t = make_tuple(id, kyori + 1, heyanum);
      lol.push(make_tuple(id, kyori + 1, heyanum));
      // dfs(id, kyori + 1, heyanum);
    }
  }
  if (!lol.empty()) {
    tuple<int, int, int> qi = lol.front();
    lol.pop();
    dfs(get<0>(qi), get<1>(qi), get<2>(qi));
  }
}
int main() {
  cin >> N >> M;
  A.resize(N);
  ans.resize(N);
  touroku.resize(N);
  REP(i, N) {
    ans[i] = P(-1, INF);
    touroku[i] = -1;
  }

  REP(i, M) {
    int a, b;
    cin >> a >> b, a--, b--;
    A[a].push_back(b);
    A[b].push_back(a);
  }
  dfs(0, 0, -1);
  ans[0] = P(0, 0);
  cout << "Yes" << endl;
  REP(i, N - 1) { cout << ans[i + 1].first + 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 FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int N, M;
vector<vector<int>> A(0, vector<int>(0));
vector<int> touroku(0);
vector<P> ans(0); // f:行き先 :0への距離
void dfs(int heyanum, int kyori, int previd) {
  if (ans[heyanum].second <= kyori && previd != touroku[heyanum]) {
    return;
  }
  REP(i, A[heyanum].size()) {
    int id = A[heyanum][i];
    if (previd == id)
      continue;
    if (kyori + 1 < ans[id].second) {
      ans[id] = P(heyanum, kyori + 1);
      touroku[id] = heyanum;
      dfs(id, kyori + 1, heyanum);
    }
  }
}
int main() {
  cin >> N >> M;
  A.resize(N);
  ans.resize(N);
  touroku.resize(N);
  REP(i, N) {
    ans[i] = P(-1, INF);
    touroku[i] = -1;
  }

  REP(i, M) {
    int a, b;
    cin >> a >> b, a--, b--;
    A[a].push_back(b);
    A[b].push_back(a);
  }
  dfs(0, 0, -1);
  ans[0] = P(0, 0);
  cout << "Yes" << endl;
  REP(i, N - 1) { cout << ans[i + 1].first + 1 << endl; }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define vecall(itr, vec)                                                       \\
  for (auto(itr) = (vec).begin(); (itr) != (vec).end(); (itr)++)

using namespace std;
using p = pair<int, int>;
using ll = long long;

vector<vector<int>> path;

int main() {
  int n, m;
  cin >> n >> m;
  path.resize(n);
  vector<int> ans(n, 100002);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    path[a - 1].push_back(b - 1);
    path[b - 1].push_back(a - 1);
  }

  vector<int> queue;
  queue.push_back(0);
  ans[0] = 0;
  while (queue.size() != 0) {
    int now = queue[0];

    queue.erase(queue.begin());
    vecall(itr, path[now]) {
      if (ans[now] + 1 < ans[*itr]) {
        queue.push_back(*itr);
        ans[*itr] = ans[now] + 1;
      }
    }
  }
  string s = "Yes";
  vecall(itr, ans) {
    if (*itr == n + 1)
      s = "No";
  }
  int depth = 0;
  vector<int> x(n, -1);
  x[0] = 0;
  FOR(i, 1, n) {
    depth = ans[i];
    vecall(itr, path[i]) {
      if (ans[*itr] == depth - 1) {
        x[i] = *itr;
        break;
      }
    }
    if (x[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << s << endl;
  FOR(i, 1, n) cout << x[i] + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define vecall(itr, vec)                                                       \\
  for (auto(itr) = (vec).begin(); (itr) != (vec).end(); (itr)++)

using namespace std;
using p = pair<int, int>;
using ll = long long;

vector<vector<int>> path;

int main() {
  int n, m;
  cin >> n >> m;
  path.resize(n);
  vector<int> ans(n, 100002);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    path[a - 1].push_back(b - 1);
    path[b - 1].push_back(a - 1);
  }

  vector<int> queue;
  vector<int> next_q;
  queue.push_back(0);
  ans[0] = 0;
  int depth = 0;
  while (queue.size() != 0) {
    int now = queue[0];

    queue.erase(queue.begin());
    vecall(itr, path[now]) {
      if (ans[now] + 1 <= ans[*itr]) {
        queue.push_back(*itr);
        ans[*itr] = ans[now] + 1;
      }
    }
  }
  string s = "Yes";
  vecall(itr, ans) {
    if (*itr == n + 1)
      s = "No";
  }
  cout << s << endl;
  FOR(i, 1, n) {
    depth = ans[i];
    vecall(itr, path[i]) {
      if (ans[*itr] == depth - 1) {
        cout << *itr + 1 << endl;
        break;
      }
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> dist(n, inf);
  vector<int> pre(n);
  queue<int> q;
  dist.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist.at(u) != inf)
        continue;
      pre.at(u) = v;
      dist.at(u) = dist.at(v) + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre.at(i) + 1 << endl;
  }
}

Error: 0

Fixed submission:

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

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

  vector<int> dist(n, inf);
  vector<int> pre(n);
  queue<int> q;
  dist.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist.at(u) != inf)
        continue;
      pre.at(u) = v;
      dist.at(u) = dist.at(v) + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre.at(i) + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 100003

int INF = (1 << 30);
vector<vector<int>> grid;
vector<int> dis;
vector<int> signpost;

int calCulateMove(int source) {
  // cout<<"Calclating move for "<<source<<endl;

  dis[source] = 0;
  queue<int> Q;

  Q.push(source);

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

    for (int i = 0; i < grid[u].size(); i++) {
      int v = grid[u][i];
      if (dis[u] + 1 < dis[v]) {
        dis[v] = dis[u] + 1;
        signpost[v] = u;
        Q.push(v);
      }
    }
  }

  return 0;
}

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int N, M;

  cin >> N >> M;
  grid = vector<vector<int>>(N + 1);
  dis = vector<int>(N + 1, INF);
  signpost = vector<int>(N + 1, -1);

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

  // cout<<INF << endl;

  // for(int i=1; i<=N; i++){
  //     cout<<"Fors "<<i<<" -> ";
  //     for(int j=0; j<grid[i].size(); j++){
  //         cout<<grid[i][j] <<" ";
  //     }
  //     cout<<endl;
  // }

  calCulateMove(1);

  bool allTraversed = true;
  for (int i = 1; i <= N; i++) {
    if (dis[i] == INF) {
      allTraversed = false;
      break;
    }
  }

  if (allTraversed) {
    cout << "Yes\\n";
    for (int i = 2; i <= N; i++) {
      cout << signpost[i] << endl;
    }
  } else {
    cout << "No\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_N 100003

int INF = (1 << 30);
vector<vector<int>> grid;
bool visited[MAX_N];
int dis[MAX_N];
int signpost[MAX_N];

int calCulateMove(int source) {
  // cout<<"Calclating move for "<<source<<endl;
  for (int i = 0; i < grid[source].size(); i++) {
    int v = grid[source][i];
    if (dis[source] + 1 < dis[v]) {
      dis[v] = dis[source] + 1;
      signpost[v] = source;
      calCulateMove(v);
    }
  }

  return 0;
}

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int N, M;

  cin >> N >> M;
  grid = vector<vector<int>>(N + 1);
  for (int i = 1; i <= N; i++) {
    dis[i] = INF;
    signpost[i] = -1;
  }
  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    grid[a].push_back(b);
    grid[b].push_back(a);
  }

  // cout<<INF << endl;

  // for(int i=1; i<=N; i++){
  //     cout<<"Fors "<<i<<" -> ";
  //     for(int j=0; j<grid[i].size(); j++){
  //         cout<<grid[i][j] <<" ";
  //     }
  //     cout<<endl;
  // }

  dis[1] = 0;
  calCulateMove(1);

  bool allTraversed = true;
  for (int i = 1; i <= N; i++) {
    if (dis[i] == INF) {
      allTraversed = false;
      break;
    }
  }

  if (allTraversed) {
    cout << "Yes\\n";
    for (int i = 2; i <= N; i++) {
      cout << signpost[i] << endl;
    }
  } else {
    cout << "No\\n";
  }

  return 0;
}
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>> paths(N);
  for (int i = 0; i < M; i++) { // O(M)
    int a, b;
    cin >> a >> b;
    paths.at(a - 1).push_back(b - 1);
    paths.at(b - 1).push_back(a - 1);
  }
  vector<int> label(N);
  vector<int> depth(N);
  vector<bool> checked(N, false);
  checked.at(0) = true;
  queue<int> que;
  que.push(0);
  while (1) {
    int i = que.front();
    que.pop();
    for (auto j : paths.at(i)) {
      if (!checked.at(j)) {
        que.push(j);
        checked.at(j) = true;
        depth.at(j) = depth.at(i) + 1;
        label.at(j) = i;
      }
    }
    if (que.empty()) {
      break;
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++)
    if (!checked.at(i))
      ok = false;
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << label.at(i) + 1 << endl;
  } else {
    cout << "No" << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> paths(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    paths.at(a - 1).push_back(b - 1);
    paths.at(b - 1).push_back(a - 1);
  }
  vector<int> label(N, -1);
  vector<int> num(N, 100001);
  num.at(0) = 0;
  vector<int> next;
  vector<int> before;
  vector<int> end;
  before.push_back(0);
  end.push_back(0);
  for (int i = 0; i < N; i++) {
    for (auto b : before) {
      for (auto p : paths.at(b)) {
        if (!count(end.begin(), end.end(), p)) {
          num.at(p) = num.at(b) + 1;
          label.at(p) = b;
          end.push_back(p);
          next.push_back(p);
        }
      }
    }
    before = next;
    next.clear();
    if (before.size() == 0)
      break;
  }
  bool ok = true;
  for (int i = 1; i < N; i++)
    if (label.at(i) == -1)
      ok = false;
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << label.at(i) + 1 << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 100005;

vector<int> G[MAX];
int ans[MAX];

void bfs(int s) {
  rep(i, 0, MAX) ans[i] = -1;
  queue<int> q;
  ans[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (ans[v] == -1) {
        ans[v] = u;
        q.push(v);
      }
    }
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  bfs(1);

  puts("Yes");
  rep(i, 2, n + 1) { printf("%d\\n", ans[i]); }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 100;

vector<int> G[MAX];
int ans[MAX];

void bfs(int s) {
  rep(i, 0, MAX) ans[i] = -1;
  queue<int> q;
  ans[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (ans[v] == -1) {
        ans[v] = u;
        q.push(v);
      }
    }
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  bfs(1);

  puts("Yes");
  rep(i, 2, n + 1) { printf("%d\\n", ans[i]); }

  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 < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

vector<int> G[220000];
int ans[220000];
bool reached[220000];

void bfs(int ini) {
  queue<int> q;
  q.push(ini);
  while (q.size()) {
    int p = q.front();
    q.pop();
    for (int to : G[p]) {
      if (reached[to])
        continue;
      reached[to] = true;
      ans[to] = p;
      q.push(to);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[b].push_back(a);
    G[a].push_back(b);
  }
  bfs(0);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

struct UnionFind {
  vector<int> par;
  vector<int> siz;

  UnionFind(int n) : par(n), siz(n, 1) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }

  int root(int i) {
    while (par[i] != i) {
      i = par[i] = par[par[i]];
    }
    return i;
  }
  bool marge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool is_same(int x, int y) { return root(x) == root(y); }

  int size(int i) { return siz[root(i)]; }
};

set<int> G[220000];
int ans[220000];
bool reached[220000];

void bfs(int ini) {
  queue<int> q;
  q.push(ini);
  while (q.size()) {
    int p = q.front();
    q.pop();
    for (auto itr = G[p].begin(); itr != G[p].end(); itr++) {
      G[p].erase(*itr);
      if (reached[*itr])
        continue;
      reached[*itr] = true;
      ans[*itr] = p;
      if (*itr != p)
        q.push(*itr);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.marge(a, b);
    G[b].insert(a);
    G[a].insert(b);
  }
  set<int> ufsize_check;
  rep(i, n) { ufsize_check.insert(uf.root(i)); }
  if (ufsize_check.size() != 1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  bfs(0);
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Map(N);
  vector<int> Ans(N - 1, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    Map[A - 1].push_back(B - 1);
    Map[B - 1].push_back(A - 1);
  }
  queue<pair<int, int>> Q;
  Q.push({0, -1});
  while (!Q.empty()) {
    auto p = Q.front();
    int v = p.first, before = p.second;
    Q.pop();
    if (v > 0) {
      if (Ans[v - 1] >= 0)
        continue;
      Ans[v - 1] = before;
    }
    for (int u : Map[v])
      if (Ans[u - 1] < 0)
        Q.push({u, v});
  }

  bool OK = true;
  for (int a : Ans)
    if (a < 0)
      OK = false;
  if (!OK)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int a : Ans)
      cout << a + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Map(N);
  vector<int> Ans(N - 1, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    Map[A - 1].push_back(B - 1);
    Map[B - 1].push_back(A - 1);
  }
  queue<pair<int, int>> Q;
  Q.push({0, -1});
  while (!Q.empty()) {
    auto p = Q.front();
    int v = p.first, before = p.second;
    Q.pop();
    if (v > 0) {
      if (Ans[v - 1] >= 0)
        continue;
      Ans[v - 1] = before;
    }
    for (int u : Map[v])
      Q.push({u, v});
  }

  bool OK = true;
  for (int a : Ans)
    if (a < 0)
      OK = false;
  if (!OK)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int a : Ans)
      cout << a + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int N, M;
Graph G;

void solve() {
  vector<int> dist(N, -1);
  vector<int> prev(N, -1);
  queue<int> que;
  que.push(0);
  dist[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] == -1) {
        dist[nv] = dist[v] + 1;
        prev[nv] = v;
        que.push(nv);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << prev[i] + 1 << endl;
}

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const long long INF = 1LL << 60;
const int MOD = 1000000000 + 7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// cout << fixed << setprecision(10) << ans << endl;
// *min_element(c + l, c + r) *max_element(c + l, c + r)
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// struct Edge {
//   int to, id;
//   Edge(int to, int id): to(to), id(id) {}
// };
int N, M;

vector<vector<int>> g;

vector<bool> done;
vector<int> ans;

int main() {
  cin >> N >> M;
  g = vector<vector<int>>(M, vector<int>());
  // vector<vector<int>> g(M,vector<int>());
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  done = vector<bool>(N, false);
  ans = vector<int>(N, 0);
  // vector<bool> done(N,false);
  // vector<int> ans(N,0);
  queue<int> qu;
  qu.push(0);
  done[0] = true;
  while (!qu.empty()) {
    int now = qu.front();
    qu.pop();
    for (auto nxt : g[now]) {
      if (!done[nxt]) {
        done[nxt] = true;
        ans[nxt] = now;
        qu.push(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, N - 1) cout << ans[i + 1] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t N, M;
  cin >> N >> M;
  vector<pair<int64_t, int64_t>> edge(M);
  vector<int64_t> Ans(N, 100000);
  vector<vector<int64_t>> A(N, vector<int64_t>(0));
  queue<int64_t> Q;
  for (int64_t i = 0; i < M; i++) {
    cin >> edge.at(i).first >> edge.at(i).second;
    edge.at(i).first--;
    edge.at(i).second--;
    A.at(edge.at(i).first).push_back(edge.at(i).second);
    A.at(edge.at(i).second).push_back(edge.at(i).first);
  }
  Ans.at(0) = 0;
  Q.push(0);
  while (!Q.empty()) {
    int64_t q = Q.front();
    Q.pop();
    for (int64_t i = 0; i < A.at(q).size(); i++) {
      if (Ans.at(A.at(q).at(i)) == 100000) {
        Ans.at(A.at(q).at(i)) = q;
        Q.push(A.at(q).at(i));
      }
    }
  }
  cout << "Yes" << endl;
  for (int64_t i = 1; i < N; i++) {
    cout << Ans.at(i) + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> dp(100000, 100001);
vector<pair<int64_t, int64_t>> edge(200000);
vector<int64_t> Ans(100000, 0);
vector<vector<int64_t>> A(100000, vector<int64_t>(0));

void dfs(int64_t n) {
  for (int64_t i = 0; i < A.at(n).size(); i++) {
    if (dp.at(n) + 1 < dp.at(A.at(n).at(i))) {
      dp.at(A.at(n).at(i)) = dp.at(n) + 1;
      Ans.at(A.at(n).at(i)) = n;
      dfs(A.at(n).at(i));
    }
  }
}

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

  for (int64_t i = 0; i < M; i++) {
    cin >> edge.at(i).first >> edge.at(i).second;
    edge.at(i).first--;
    edge.at(i).second--;
    A.at(edge.at(i).first).push_back(edge.at(i).second);
    A.at(edge.at(i).second).push_back(edge.at(i).first);
  }
  dp.at(0) = 0;
  dfs(0);
  cout << "Yes" << endl;
  for (int64_t i = 1; i < N; i++) {
    cout << Ans.at(i) + 1 << endl;
  }
}