alexjercan/bugnet huggingface dataset:
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;
#define INF 1e9

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

  int N, M;
  cin >> N >> M;
  vector<int> A, B;
  vector<int> V(N, -1); // visited
  vector<vector<int>> atesaki(N);
  /*    rep(i,M){
          int res1,res2;
          cin >> res1 >> res2;
          A.push_back(res1);
          B.push_back(res2);
      }
  */

  rep(i, M) {
    int res1, res2;
    cin >> res1 >> res2;
    atesaki[res1 - 1].push_back(res2);
    atesaki[res2 - 1].push_back(res1);
  }

  queue<int> q;
  q.push(1);
  V[0] = 0;

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

    rep(i, atesaki[now - 1].size()) {
      int next = atesaki[now - 1][i]; // aite
      // atesaki.at(now-1).at(i)の方がいいのかな
      if (V[next - 1] == -1) {
        V[next - 1] = now;
        q.push(next);
      }
    }
  }
  int judge = 1;
  rep(i, N) {
    if (V[i] == -1)
      judge = 0;
  }
  if (judge == 0)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << V[i] << endl;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

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

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

  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  vector<int> V(N, -1); // visited
  vector<vector<int>> atesaki;
  /*    rep(i,M){
          int res1,res2;
          cin >> res1 >> res2;
          A.push_back(res1);
          B.push_back(res2);
      }
      */
  rep(i, M) { cin >> A[i] >> B[i]; }
  rep(i, M) {
    int res1, res2;
    cin >> res1 >> res2;
    atesaki[res1 - 1].push_back(res2);
    atesaki[res2 - 1].push_back(res1);
  }

  queue<int> q;
  q.push(1);
  V[0] = 0;

  rep(i, M)

      while (!q.empty()) {
    int now = q.front();
    q.pop();
    /*
    rep(i,M){
        if(A[i] == now && V[B[i]-1] == -1){
            V[B[i]-1] = now;
            q.push(B[i]);
        }
        if(B[i] == now && V[A[i]-1] == -1){
            V[A[i]-1] = now;
            q.push(A[i]);
        }
    }*/
    for (auto x : atesaki[now - 1]) {
      if (V[atesaki[now - 1][x] - 1] == -1) {
        V[atesaki[now - 1][x] - 1] == now;
        q.push(atesaki[now - 1][x]);
      }
    }
  }
  int judge = 1;
  rep(i, N) {
    if (V[i] == -1)
      judge = 0;
  }
  if (judge == 0)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << V[i] << endl;
    }
  }

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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

const int INF = 1001001001;
vector<int> to[100005];

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

  rep(i, M) {
    cin >> X >> Y;
    --X;
    --Y;
    to[X].push_back(Y);
    to[Y].push_back(X);
  }

  vector<int> prev(N, -1);
  vector<int> dist(N, INF);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int point = que.front();
    que.pop();
    for (int u : to[point]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[point] + 1;
      prev[u] = point;
      que.push(u);
    }
  }

  int count = 0;
  rep(i, N) {
    if (prev[i] == -1) {
      count++;
    }
  }

  if (count == 1) {
    cout << "Yes" << endl;
    rep(i, N - 1) { cout << prev[i + 1] + 1 << endl; }
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

void BFS(int i, int N, int M, queue<int> &que, vector<vector<int>> A,
         vector<vector<int>> &cost);

int main() {
  int N, M;
  cin >> N >> M;
  int X, Y;
  vector<vector<int>> A(M * 2, vector<int>(2, 0));
  vector<vector<int>> cost(N, vector<int>(3, 0));
  queue<int> que;
  rep(i, M) {
    cin >> X >> Y;
    A[i * 2][0] = X;
    A[i * 2][1] = Y;
    A[i * 2 + 1][0] = Y;
    A[i * 2 + 1][1] = X;
  }
  sort(A.begin(), A.end());
  BFS(1, N, M, que, A, cost);

  int count = 0;
  rep(i, N) {
    if (cost[i][0] == 0) {
      count++;
    }
  }

  if (count == 1) {
    cout << "Yes" << endl;
    rep(i, N - 1) { cout << cost[i + 1][0] << endl; }
  } else {
    cout << "No" << endl;
  }
  return 0;
}

void BFS(int s, int N, int M, queue<int> &que, vector<vector<int>> A,
         vector<vector<int>> &cost) {
  int point;
  cost[s - 1][2] = true;
  que.push(s);
  while (!que.empty()) {
    point = que.front();
    que.pop();
    rep(i, 2 * M) {
      if (A[i][0] == point && cost[A[i][1] - 1][2] == false) {
        cost[A[i][1] - 1][2] = true;
        cost[A[i][1] - 1][0] = point;
        cost[A[i][1] - 1][1] = cost[point - 1][1] + 1;
        que.push(A[i][1]);
      }
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll INF = 1LL << 60;

using Graph = vector<vector<int>>;

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

  // グラフ入力受取
  Graph 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);
  }

  // 頂点 s をスタートとした探索
  string ans = "Yes";
  vector<vector<int>> A(N);
  vector<int> dist(N, -1);
  vector<int> prev(N, -1);
  queue<int> que;
  dist[0] = 0, que.push(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);
      }
    }
  }

  rep(i, N) {
    if (dist[i] == -1) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
  if (ans == "Yes") {
    rep2(i, 1, N) cout << prev[i] + 1 << endl;
  }

  return 0;
}
// Ctrl+Shift+Bでコンパイルと実行を行ってデバッグすること

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll INF = 1LL << 60;

using Graph = vector<vector<int>>;

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

  // グラフ入力受取
  Graph 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);
  }

  // 頂点 s をスタートとした探索
  string ans = "Yes";
  vector<vector<int>> A(N);
  rep2(i, 1, N) {
    vector<int> dist(N, -1);
    vector<int> prev(N, -1);
    queue<int> que;
    dist[i] = 0, que.push(i);
    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);
        }
      }
    }
    if (dist[0] == -1) {
      cout << "No" << endl;
      return 0;
    }
    int x = 0;
    while (x != -1) {
      A[i].push_back(x + 1);
      int px = prev[x];
      x = px;
      if (x == i)
        break;
    }

    if (i == N - 1) {
      cout << "Yes" << endl;
    }
  }
  rep2(i, 1, N) cout << A[i].back() << endl;

  return 0;
}
// Ctrl+Shift+Bでコンパイルと実行を行ってデバッグすること
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

#define EPS (1e-7)
#define INF 1e18
#define max(p, q) ((p) > (q) ? (p) : (q))
#define min(p, q) ((p) < (q) ? (p) : (q))
#define PI (acos(-1))

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, init, n) for (int i = init; i < (int)(n); i++)

int main() {

  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> V(N + 1);
  ll a, b;
  REP(i, M) {
    cin >> a >> b;
    V[a].push_back(b);
    V[b].push_back(a);
  }
  vector<ll> hint(N + 1, -1);
  queue<P> q;
  q.push(P(0, 1));
  while (!q.empty()) {
    P tmp = q.front();
    if (hint[tmp.second] < 0) {
      hint[tmp.second] = tmp.first;
      for (auto iter : V[tmp.second])
        if (hint[iter] < 0)
          q.push(P(tmp.second, iter));
    }
    q.pop();
  }
  bool connect = true;
  rep(i, 1, N + 1) if (hint[i] < 0) connect = false;
  if (!connect) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, 2, N + 1) cout << hint[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

#define EPS (1e-7)
#define INF 1e18
#define max(p, q) ((p) > (q) ? (p) : (q))
#define min(p, q) ((p) < (q) ? (p) : (q))
#define PI (acos(-1))

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, init, n) for (int i = init; i < (int)(n); i++)

int main() {

  ll N, M;
  cin >> N >> M;
  vector<set<ll>> V(N + 1);
  ll a, b;
  REP(i, M) {
    cin >> a >> b;
    V[a].insert(b);
    V[b].insert(a);
  }
  vector<ll> hint(N + 1, -1);
  queue<P> q;
  q.push(P(-1, 1));
  hint[1] = 0;
  while (!q.empty()) {
    P tmp = q.front();
    if (hint[tmp.second] < 0)
      hint[tmp.second] = tmp.first;
    for (auto iter : V[tmp.second])
      if (hint[iter] < 0)
        q.push(P(tmp.second, iter));
    q.pop();
  }
  bool connect = true;
  rep(i, 1, N + 1) if (hint[i] < 0) connect = false;
  if (!connect) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, 2, N + 1) cout << hint[i] << endl;

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

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int u, v, n, m, flag, cnt;
int fa[MAXN], head[MAXN];
struct edge {
  int v, next;
} e[MAXN << 1];
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v;
      if (fa[v])
        continue;
      fa[v] = u;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs();
  for (int i = 2; i <= n; ++i)
    if (!fa[i]) {
      printf("No");
      flag = 1;
      break;
    }
  if (!flag) {
    printf("Yes\\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\\n", fa[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int u, v, n, m, flag, cnt;
int fa[MAXN], head[MAXN];
struct edge {
  int v, next;
} e[MAXN];
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v;
      if (fa[v])
        continue;
      fa[v] = u;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs();
  for (int i = 2; i <= n; ++i)
    if (!fa[i]) {
      printf("No");
      flag = 1;
      break;
    }
  if (!flag) {
    printf("Yes\\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\\n", fa[i]);
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

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

  dist[0] = 0;
  ans[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int q = que.front();
    que.pop();
    for (auto u : mp[q]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[q] + 1;
      que.push(u);
      ans[u] = q;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++)
    cout << ans[i] + 1 << endl;
}

Error: 0

Fixed submission:

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

int n, m;
vector<vector<int>> mp;
vector<int> ans;

bool dfs(int bef, int from) {
  if (from == 0)
    return true;
  for (auto p : mp[from]) {
    if (p == bef)
      continue;
    if (dfs(from, p)) {
      ans[from] = p + 1;
      return true;
    }
  }
  return false;
}

int main() {
  bool check = true;
  cin >> n >> m;
  mp.resize(n);
  ans.resize(n, -1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mp[a].push_back(b);
    mp[b].push_back(a);
  }
  rep(i, n) { sort(mp[i].begin(), mp[i].end()); }
  for (int i = 1; i < n; i++) {
    if (ans[i] == -1)
      if (!dfs(-1, i))
        check = false;
  }
  if (check) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++)
      cout << ans[i] << endl;
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<pair<int, int>> que;
  que.emplace(0, 0);
  while (!que.empty()) {
    int from = que.front().first;
    int cur = que.front().second;
    que.pop();

    for (auto to : G[cur]) {
      if (ans[to] == -1) {
        ans[to] = cur;
        que.emplace(cur, to);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;

  G.resize(n);
  ans.resize(n, -1);
  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);
  }

  bfs();
  cout << "Yes" << '\\n';
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << '\\n';
  }
}

Error: TLE

Fixed submission:

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

vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<pair<int, int>> que;
  que.emplace(0, 0);
  while (!que.empty()) {
    int from = que.front().first;
    int cur = que.front().second;
    que.pop();

    if (ans[cur] == -1) {
      ans[cur] = from;
    }

    for (auto to : G[cur]) {
      if (ans[to] == -1) {
        que.emplace(cur, to);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;

  G.resize(n);
  ans.resize(n, -1);
  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);
  }

  bfs();
  cout << "Yes" << '\\n';
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << '\\n';
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip> //表示桁数
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

#include <queue>
#include <unordered_set>

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned char Byte;
using namespace std;

string ans = "Yes";
// double ans;
int N, M;
int rcount = 0;

vector<int> A;
vector<int> B;
vector<unordered_set<int>> rootMap;

vector<int> ansN;
vector<int> disN;

/*
void recruit(int room) {


        if()

        rep(i, N);

}*/

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

  unordered_set<int> argv;
  vector<unordered_set<int>> argvv(N, argv);

  rootMap = argvv;

  vector<int> iargv2(N, -1);
  ansN = iargv2;

  rep(i, M) {
    int tmpA, tmpB;
    cin >> tmpA >> tmpB;
    // A.push_back(tmpA);
    // B.push_back(tmpB);
    rootMap[tmpA - 1].insert(tmpB - 1);
    rootMap[tmpB - 1].insert(tmpA - 1);
  }

  unordered_set<int> sameRank;

  sameRank.insert(0);

  while (rcount < N) {
    unordered_set<int> tmpRank;
    for (int room : sameRank) {
      unordered_set<int> tmpRoom = rootMap[room];
      for (int i : tmpRoom) {
        if (ansN[i] == -1) {
          ansN[i] = room;
          tmpRank.insert(i);
          // rootMap[i].erase(room);
          rcount++;
        }
      }
    }
    sameRank = tmpRank;
  }

  cout << ans << endl;
  rep(i, N - 1) { cout << ansN[i + 1] + 1 << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip> //表示桁数
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

#include <queue>
#include <unordered_set>

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned char Byte;
using namespace std;

string ans = "Yes";
// double ans;
int N, M;
int rcount = 0;

vector<int> A;
vector<int> B;
vector<vector<bool>> rootMap;

vector<int> ansN;
vector<int> disN;

/*
void recruit(int room) {


        if()

        rep(i, N);

}*/

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

  vector<bool> argv(N, false);
  vector<vector<bool>> argvv(N, argv);

  rootMap = argvv;

  vector<int> iargv2(N, -1);
  ansN = iargv2;

  rep(i, M) {
    int tmpA, tmpB;
    cin >> tmpA >> tmpB;
    A.push_back(tmpA);
    B.push_back(tmpB);
    rootMap[tmpA - 1][tmpB - 1] = true;
    rootMap[tmpB - 1][tmpA - 1] = true;
  }

  unordered_set<int> sameRank;

  sameRank.insert(0);

  while (rcount < N) {
    unordered_set<int> tmpRank;
    for (int room : sameRank) {
      rep(i, N) {
        if (rootMap[room][i] == true && ansN[i] == -1) {
          ansN[i] = room;
          tmpRank.insert(i);
          rcount++;
        }
      }
    }
    sameRank = tmpRank;
  }

  cout << ans << endl;
  rep(i, N - 1) { cout << ansN[i + 1] + 1 << endl; }

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

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

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> ans(n + 1);
  vector<bool> flag(n + 1, true);
  // init
  vector<vector<int>> g(n + 1);
  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);
  flag[1] = false;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i : g[v]) {
      if (flag[i]) {
        que.push(i);
        ans[i] = v;
        flag[i] = false;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

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

int n, m;
vector<int> a, b, ans, depth;
vector<bool> line, flag;
vector<vector<bool>> table;

void f(int x, int d) {
  depth[x] = d + 1;
  for (int i = 2; i <= n; i++) {
    if (table[x][i] && depth[i] > d + 2) {
      f(i, d + 1);
    }
  }
}

int main(void) {
  cin >> n >> m;
  // init
  vector<bool> line;
  vector<vector<int>> g(n + 1);
  for (int i = 0; i <= n; i++) {
    line.push_back(false);
  }
  for (int i = 0; i <= n; i++) {
    flag.push_back(true);
    ans.push_back(-1);
    depth.push_back(100100);
    table.push_back(line);
  }
  for (int i = 0; i < m; i++) {
    int aa, bb;
    cin >> aa >> bb;
    a.push_back(aa);
    b.push_back(bb);
    table[aa][bb] = true;
    table[bb][aa] = true;
    g[aa].push_back(bb);
    g[bb].push_back(aa);
  }
  queue<int> que;
  que.push(1);
  depth[1] = 0;
  flag[1] = false;
  while (!que.empty()) {
    int v = que.front();
    for (int i : g[v]) {
      if (flag[i]) {
        que.push(i);
        depth[i] = depth[v] + 1;
        flag[i] = false;
      }
    }
    que.pop();
  }
  for (int i = 0; i < m; i++) {
    int mini, maxi;
    if (depth[a[i]] > depth[b[i]]) {
      mini = b[i];
      maxi = a[i];
    } else {
      mini = a[i];
      maxi = b[i];
    }
    if (ans[maxi] == -1) {
      ans[maxi] = mini;
    } else {
      if (depth[ans[maxi]] > depth[mini]) {
        ans[maxi] = mini;
      }
    }
  }
  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 <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

#define int long long
#define rr register

#define MAXN 200010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int n, m, to[MAXN];

int cnt, head[MAXN];

int vis[MAXN], dis[MAXN];

struct Edge {
  int next, to;
} pth[MAXN << 2];

std::queue<int> q;

void add(int from, int to) {
  pth[++cnt].to = to, pth[cnt].next = head[from];
  head[from] = cnt;
}

signed main() {
  memset(dis, 0x3f, sizeof dis);
  n = read(n), m = read(m);
  for (rr int i = 1; i <= m; ++i) {
    int u = read(u), v = read(v);
    add(u, v), add(v, u);
  }
  q.push(1);
  dis[1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (rr int i = head[u]; i; i = pth[i].next) {
      int v = pth[i].to;
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        to[v] = u;
      }
      if (!vis[v]) {
        q.push(v);
        vis[v] = 1;
      }
    }
  }
  for (int i = 2; i <= n; ++i) {
    if (dis[i] == inf) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (int i = 2; i <= n; ++i) {
    printf("%d\\n", to[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 100010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

struct Node {
  int nxt, to;
} e[MAXN << 2];

int n, m, tot;

int head[MAXN << 2], a[MAXN], dep[MAXN];

inline void add(int from, int to) {
  e[++tot].nxt = head[from];
  e[tot].to = to;
  head[from] = tot;
}

void dfs(int x) {
  for (rr int j = head[x]; j; j = e[j].nxt) {
    int to = e[j].to;
    if (dep[to] > dep[x] + 1) {
      a[to] = x;
      dep[to] = dep[x] + 1;
      dfs(to);
    }
  }
}

signed main() {
  n = read(n);
  m = read(m);
  memset(dep, 127, sizeof dep);
  dep[1] = 0;
  for (rr int i = 1; i <= m; i++) {
    int x = read(x), y = read(y);
    add(x, y);
    add(y, x);
  }
  dfs(1);
  for (rr int i = 2; i <= n; i++)
    if (a[i] == 0) {
      cout << "No";
      return 0;
    }
  cout << "Yes\\n";
  for (rr int i = 2; i <= n; i++)
    cout << a[i] << "\\n";
}