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

#include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, inf);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != inf)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, inf);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[10005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!(q.empty())) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i] + 1;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
ll n, m;
struct node {
  ll from, to;
} road[5000005];
ll num;
ll flag[5000005], first[5000005];
queue<int> q;
void add(ll u, ll v) {
  road[++num].from = v;
  road[num].to = first[u]; // u
  first[u] = num;
}
void bfs(ll tv, ll c) {
  flag[tv] = c;
  q.push(tv);
  while (!q.empty()) {
    ll tmp = q.front();
    q.pop();
    for (ll i = first[tmp]; i; i = road[i].to) {
      if (flag[road[i].from] == 0) {
        q.push(road[i].from);
        flag[road[i].from] = tmp;
      }
    }
  }
}
int main() {
  FAST_IO;
  cin >> n >> m;
  ll u, v;
  ll f = 1;
  for (ll i = 1; i <= m; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  bfs(1, 1);
  for (ll i = 1; i <= n; i++) {
    if (flag[i] == 0)
      f = 0;
  }
  if (f == 0)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (ll i = 2; i <= n; i++) {
      cout << flag[i] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
ll n, m;
struct node {
  ll from, to;
} road[500005];
ll num;
ll flag[500005], first[500005];
queue<int> q;
void add(ll u, ll v) {
  road[++num].from = v;
  road[num].to = first[u]; // u
  first[u] = num;
}
int bfs(ll tv, ll c) {
  flag[tv] = c;
  q.push(tv);
  while (!q.empty()) {
    ll tmp = q.front();
    q.pop();
    for (ll i = first[tmp]; i; i = road[i].to) {
      if (flag[road[i].from] == 0) {
        q.push(road[i].from);
        flag[road[i].from] = tmp;
      }
    }
  }
}
int main() {
  FAST_IO;
  cin >> n >> m;
  ll u, v;
  ll f = 1;
  for (ll i = 1; i <= m; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  bfs(1, 1);
  for (ll i = 1; i <= n; i++) {
    if (flag[i] == 0)
      f = 0;
  }
  if (f == 0)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (ll i = 2; i <= n; i++) {
      cout << flag[i] << endl;
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < m; i++) {
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }
  vector<int> color(n, 1e9);
  vector<bool> seen(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : G[v]) {
      if (seen[i])
        continue;
      q.push(i);
      seen[i] = true;
      color[i] = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << color[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < n; i++) {
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }
  vector<int> color(n, 1e9);
  vector<bool> seen(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : G[v]) {
      if (seen[i])
        continue;
      q.push(i);
      seen[i] = true;
      color[i] = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << color[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000009
#define int long long
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int pow(int p, int r) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % mod) * (p % mod)) % mod;
    }
    r = r >> 1;
    p = ((p % mod) * (p % mod)) % mod;
  }
}
int vis[100001];
void bfs(vector<vector<int>> g, int i, int n) {
  queue<int> q;
  q.push(0);
  memset(vis, -1, sizeof(vis));
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int k : g[a]) {
      if (vis[k] == -1) {
        q.push(k);
        vis[k] = a;
      }
    }
  }
}
signed main() {
  garou;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>());
  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(g, 0, n);
  for (int i = 1; i < n; i++)
    if (vis[i] == -1) {
      cout << "No";
      return 0;
    }
  cout << "Yes" << '\\n';
  for (int i = 1; i < n; i++)
    cout << vis[i] + 1 << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000009
#define int long long
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int pow(int p, int r) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % mod) * (p % mod)) % mod;
    }
    r = r >> 1;
    p = ((p % mod) * (p % mod)) % mod;
  }
}
map<int, int> mp;
bool vis[100001];
void bfs(vector<vector<int>> g, int i, int n) {
  queue<int> q;
  q.push(0);
  vis[0] = true;
  bool f = false;
  while (!f && !q.empty()) {
    int a = q.front();
    vis[a] = true;
    q.pop();
    for (int k : g[a]) {
      if (!vis[k]) {
        mp.insert(pair<int, int>(k, a));
        q.push(k);
      }
    }
  }
}
signed main() {
  garou;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>());
  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(g, 0, n);
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      cout << "No";
      return 0;
    }
  cout << "Yes" << '\\n';
  for (auto i : mp) {
    cout << i.second + 1 << '\\n';
  }
}
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;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;
vector<int> to[100005]; // インデックスに対応する部屋が隣接している部屋

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

Error: 0

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>;

const int INF = 1001001001;
vector<int> to[100005]; // インデックスに対応する部屋が隣接している部屋

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < m; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << 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)
using namespace std;
typedef long long ll;

vector<vector<int>> to(100010);
int dist[100010];
int pre[100010];
const int INF = 20000000;

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  REP(i, N) dist[i] = INF;
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (auto nv : to[v]) {
      if (dist[nv] <= dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      q.push(nv);
    }
  }

  int ok = 1;
  REP(i, N) if (dist[i] == INF) ok = 0;
  //  REP(i,N) cout << dist[i] << endl;
  if (ok == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << (pre[i] + 1) << endl;
  }
}


Error: TLE

Fixed submission:

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

vector<vector<int>> to(100010);
int dist[100010];
int pre[100010];
const int INF = 20000000;

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  REP(i, N) dist[i] = INF;
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (auto nv : to[v]) {
      if (dist[nv] < dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      q.push(nv);
    }
  }

  int ok = 1;
  REP(i, N) if (dist[i] == INF) ok = 0;
  //  REP(i,N) cout << dist[i] << endl;
  if (ok == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << (pre[i] + 1) << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
const int maxn = 100005;
using namespace std;
int d[maxn] = {0}, p[maxn] = {0};
vector<int> a[maxn];
int main() {
  int i, j, k, m, n;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
    a[y].push_back(x);
  }
  queue<int> s;
  s.push(1);
  p[1] = 1;
  while (!s.empty()) {
    k = s.front();
    s.pop();
    for (j = 0; j < a[k].size(); j++)
      if (!p[a[k][j]]) {
        d[a[k][j]] = k;
        p[a[k][j]] = 1;
        s.push(a[k][j]);
      }
  }
  int ok = 1;
  for (i = 2; i <= n; i++) {
    if (d[i] == 0) {
      ok = 0;
      break;
    }
  }
  if (ok) {
    printf("Yes\\n");
    for (i = 2; i <= n; i++)
      printf("%d\\n", d[i]);
  } else
    printf("No\\n");
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
const int maxn = 10005, inf = 1000000000;
using namespace std;
int a[maxn][maxn], d[maxn], p[maxn];
int main() {
  int i, j, k, m, n, t = 1;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d%d", &x, &y);
    a[x][y] = a[y][x] = 1;
  }
  for (i = 1; i <= n; i++)
    d[i] = inf; // 初始化d[i]代表i点到t点的距离
  d[t] = 0;
  for (i = 1; i <= n; i++) {
    int zx = inf;            // zx=最小
    for (j = 1; j <= n; j++) // 遍历每个点
      if (!p[j] && d[j] < zx) { // 找到离原点最近的没有走过的点 用k记录
        zx = d[j];
        k = j;
      }
    p[k] = 1; // k点被走过
    for (j = 1; j <= n; j++)
      if (!p[j] && a[k][j] != 0 &&
          d[j] > d[k] + a[k][j]) // 更新k点可以走到的所有没走过的点
        d[j] = d[k] + a[k][j];
  }
  int ok = 1;
  for (i = 2; i <= n; i++) {
    if (d[i] == inf) {
      ok = 0;
      break;
    }
  }
  if (ok) {
    printf("Yes\\n");
    for (i = 2; i <= n; i++)
      printf("%d\\n", d[i]);
  } else
    printf("No\\n");
  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++)
using ll = long long;
const int M = 1e9 + 7;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

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

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int k = q.front();
    q.pop();
    rep(i, r[k].size()) {
      if (d[r[k][i]] == -1) {
        q.push(r[k][i]);
        d[r[k][i]] = k;
      }
    }
  }

  rep(i, d.size() - 1) if (d[i + 1] == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, d.size() - 1) cout << d[i + 1] + 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const int M = 1e9 + 7;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

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

  queue<int> q;
  rep(i, r[0].size()) q.push(0);

  while (!q.empty()) {
    int k = q.front();
    q.pop();
    rep(i, r[k].size()) {
      if (d[r[k][i]] == -1) {
        q.push(r[k][i]);
        d[r[k][i]] = k;
      }
    }
  }

  rep(i, d.size() - 1) if (d[i + 1] == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, d.size() - 1) cout << d[i + 1] + 1 << endl;

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

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

int main() {
  int n, m;
  cin >> n >> m;
  bool room[n + 1];
  for (int i = 0; i < n + 1; i++)
    room[i] = true;
  int ans[n + 1];
  queue<int> q;
  q.push(1);
  vector<vector<int>> path(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    int bound = path[now].size();
    for (int i = 0; i < bound; i++) {
      if (room[path[now][i]]) {
        room[path[now][i]] = false;
        ans[path[now][i]] = now;
        q.push(path[now][i]);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> play(vector<bool> &check, vector<vector<int>> &connect,
                 vector<int> &ans, vector<int> &k, int n, int zero) {
  int l = k.size();
  vector<int> ans_1;
  for (int i = 0; i < l; i++) {
    int a = k[i];
    for (int j = 1; j < n; j++) {
      int b = connect[a][j];
      if (b != 0) {
        if (check[b]) {
          ans[b] = a;
          zero++;
          check[b] = false;
          ans_1.push_back(b);
        }
      } else
        break;
    }
  }
  if (zero == n - 1)
    return ans;
  else
    return play(check, connect, ans, ans_1, n, zero);
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> r(m);
  for (int i = 0; i < m; i++)
    cin >> r[i].first >> r[i].second;
  vector<vector<int>> connect(n + 1, vector<int>(n));
  for (int i = 0; i < n + 1; i++)
    connect[i][0] = 1;
  for (int i = 0; i < m; i++) {
    int a = connect[r[i].first][0];
    int b = connect[r[i].second][0];
    connect[r[i].first][a] = r[i].second;
    connect[r[i].second][b] = r[i].first;
    connect[r[i].first][0]++;
    connect[r[i].second][0]++;
  }
  vector<bool> check(n + 1);
  for (int i = 0; i < n + 1; i++) {
    check[i] = true;
  }
  vector<int> ans(n + 1);
  vector<int> k;
  k.push_back(1);
  int zero = 0;
  ans = play(check, connect, ans, k, n, zero);
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      cout << "Yes" << endl;
    else
      cout << ans[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using pr = pair<int, int>;
struct edge {
  int to, cost;
};

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  edge init({(int)2e9, (int)2e9});
  vector<edge> mn(n + 1, init);
  mn.at(1).to = -1, mn.at(1).cost = 0;
  vector<vector<int>> g(n + 1);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    g.at(a).push_back(b);
    g.at(b).push_back(a);
  }
  priority_queue<pr, vector<pr>, greater<pr>> q;
  q.push({0, 1});
  while (q.size()) {
    pr tmp = q.top();
    q.pop();
    int nw = tmp.second, spend = tmp.first;
    if (spend > mn.at(nw).cost)
      continue;
    for (auto &&i : g.at(nw)) {
      if (spend + 1 < mn.at(i).cost) {
        edge t({nw, spend + 1});
        mn.at(i) = t;
        q.push({spend + 1, i});
      }
    }
  }
  cout << "Yes\\n"s;
  for (int i = 2; i <= n; ++i)
    cout << mn.at(i).to << "\\n"s;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct pr {
  int nw, cnt;
};

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  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).emplace_back(b);
    v.at(b).emplace_back(a);
  }
  vector<pair<int, int>> a(n + 1, {1e9, 1e9});
  a.at(1) = {0, 0};
  stack<pr> q;
  q.push({1, 0});
  while (q.size()) {
    pr tmp = q.top();
    q.pop();
    int nw = tmp.nw, cnt = tmp.cnt;
    for (auto &&i : v.at(nw)) {
      if (cnt + 1 < a.at(i).second) {
        a.at(i).first = nw, a.at(i).second = cnt + 1;
        q.push({i, cnt + 1});
      }
    }
  }
  bool yes = true;
  for (int i = 2; i < a.size(); ++i) {
    if (a.at(i).first == 1e9)
      yes = false;
  }
  if (yes) {
    cout << "Yes\\n"s;
    for (int i = 2; i < a.size(); ++i) {
      cout << a.at(i).first << "\\n"s;
    }

  } else
    cout << "No\\n"s;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct pr {
  int f, nw, cnt;
};

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  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).emplace_back(b);
    v.at(b).emplace_back(a);
  }
  vector<pair<int, int>> a(n + 1, {1e9, 1e9});
  a.at(1) = {0, 0};
  queue<pr> q;
  q.push({-1, 1, 0});
  while (q.size()) {
    pr tmp = q.front();
    q.pop();
    int f = tmp.f, nw = tmp.nw, cnt = tmp.cnt;
    for (auto &&i : v.at(nw)) {
      if (cnt + 1 < a.at(i).second) {
        a.at(i).first = nw, a.at(i).second = cnt + 1;
        q.push({nw, i, cnt + 1});
      }
    }
  }
  bool yes = true;
  for (int i = 2; i < a.size(); ++i) {
    if (a.at(i).first == 1e9)
      yes = false;
  }
  if (yes) {
    cout << "Yes\\n"s;
    for (int i = 2; i < a.size(); ++i) {
      cout << a.at(i).first << "\\n"s;
    }

  } else
    cout << "No\\n"s;
}

Error: TLE

Fixed submission:

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

void trip(int nw, int cnt, int f, vector<vector<int>> &v,
          vector<pair<int, int>> &a, vector<bool> mp) {
  mp.at(nw) = true;
  if (a.at(nw).second > cnt) {
    a.at(nw) = make_pair(f, cnt);
  }
  for (auto &&i : v.at(nw)) {
    if (mp.at(i))
      continue;
    trip(i, cnt + 1, nw, v, a, mp);
  }
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  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).emplace_back(b);
    v.at(b).emplace_back(a);
  }
  vector<pair<int, int>> a(n + 1, {1e9, 1e9});
  vector<bool> mp(n + 1, false);
  trip(1, 0, -1, v, a, mp);
  bool yes = true;
  for (int i = 2; i < a.size(); ++i) {
    if (a.at(i).first == 1e9)
      yes = false;
  }
  if (!yes)
    cout << "No\\n"s;
  else {
    cout << "Yes\\n"s;
    for (int i = 2; i < a.size(); ++i) {
      cout << a.at(i).first << "\\n"s;
    }
  }
}