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

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

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

  vector<vector<int>> rooms(N);
  // vector<int> dist(N);
  vector<int> route(N);
  vector<bool> seen(N);

  int A, B;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    rooms[A - 1].push_back(B - 1);
    rooms[B - 1].push_back(A - 1);
  }

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

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

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();
    // v から辿れる頂点をすべて調べる
    for (int i = 0; i < rooms[v].size(); i++) {
      if (dist[rooms[v][i]] != -1)
        continue; // すでに発見済みの頂点は探索しない
      dist[rooms[v][i]] = dist[v] + 1;
      route[rooms[v][i]] = v;
      que.push(rooms[v][i]);
    }
  }

  int j;
  for (j = 1; j < N; j++) {
    if (dist[j] == -1) {
      break;
    }
  }
  if (j == N) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      // cout << i << ", " << dist[i] << ", " << route[i]+1 << endl;
      cout << route[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

#if 0
    for(int i = 0;i < N; i++) {
        cout << "room: " << i << endl;
        for(auto j = 0; j < rooms[i].size(); j++) {
            cout << rooms[i][j] << ", ";
        }
        cout << endl;
    }
#endif

  return 0;
}

Error: TLE

Fixed submission:

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

void DFS(vector<vector<int>> &rooms, vector<int> &dist, vector<int> &route,
         int r, int d, int g) {
  if (dist[r] <= d) {
    return;
  }

  dist[r] = d;
  route[r] = g;

  for (int i = 0; i < rooms[r].size(); i++) {
    DFS(rooms, dist, route, rooms[r][i], d + 1, r);
  }
}

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

  vector<vector<int>> rooms(N);
  vector<int> dist(N);
  vector<int> route(N);

  int A, B;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    rooms[A - 1].push_back(B - 1);
    rooms[B - 1].push_back(A - 1);
  }

  for (int i = 0; i < N; i++) {
    // dist[i] = -1;
    dist[i] = INT_MAX;
  }

  DFS(rooms, dist, route, 0, 0, 0);

  int j;
  for (j = 1; j < N; j++) {
    if (dist[j] == INT_MAX) {
      break;
    }
  }
  if (j == N) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      // cout << i << ", " << dist[i] << ", " << route[i]+1 << endl;
      cout << route[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

#if 0
    for(int i = 0;i < N; i++) {
        cout << "room: " << i << endl;
        for(auto j = 0; j < rooms[i].size(); j++) {
            cout << rooms[i][j] << ", ";
        }
        cout << endl;
    }
#endif

  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>> G(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);
  }

  vector<int> ans(N);
  using P = pair<int, int>;
  queue<P> q;
  q.push(P(0, -1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int nv : G[p.first]) {
      if (ans[nv] == 0 && nv != 0) {
        q.push(P(nv, p.first));
        ans[nv] = p.first + 1;
      }
    }
  }
  // DUMP(ans);

  for (int i = 1; i < N; ++i) {
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<int> ans(N);
  using P = pair<int, int>;
  queue<P> q;
  q.push(P(0, -1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int nv : G[p.first]) {
      if (ans[nv] == 0 && nv != 0) {
        q.push(P(nv, p.first));
        ans[nv] = p.first + 1;
      }
    }
  }
  // DUMP(ans);

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

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

#define rep(i, a, n) for (int i = a; i < n; i++)
#define ll long long
#define all(x) begin(x), end(x)
#define toInt(x) (x - '0')
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define int long long

#define INF 1e+9
#define MAX_V 100008

int V;
int N;
vector<vector<int>> G;
int d[MAX_V];

signed main() {
  cin >> V;
  N = V;
  int E;
  cin >> E;
  G.resize(V);
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(N, -1);
  queue<int> que;
  vector<int> prever(N, -1);
  // 初期条件 (頂点 0 を初期ノードとする)
  dist[0] = 0;
  que.push(0); // 0 を橙色頂点にする

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

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

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

  cout << "Yes" << endl;
  for (int v = 1; v < N; ++v)
    cout << prever[v] + 1 << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, a, n) for (int i = a; i < n; i++)
#define ll long long
#define all(x) begin(x), end(x)
#define toInt(x) (x - '0')
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define int long long

#define INF 1e+9
#define MAX_V 100008

struct edge {
  int to;
  int cost;
};

using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int prever[MAX_V];
// vector<int> prever;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  fill(prever, prever + V, -1);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        prever[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }
  // reverse(path.begin(), path.end());
  return path;
}

signed main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge e = {b, 1};
    G[a].push_back(e);
    e = {a, 1};
    G[b].push_back(e);
  }

  dijkstra(0);
  bool flag = true;
  vector<int> ans(V - 1, -1);

  for (int i = 1; i < V; i++) {
    vector<int> vl = get_path(i);

    int to = vl[1];
    if (ans[i - 1] == -1) {
      ans[i - 1] = to + 1;
    } else {
      if (ans[i - 1] != to + 1) {
        flag = false;
      }
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int x : ans) {
      cout << x << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define all(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;

using Graph = V<V<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph 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);
  }

  V<int> parent(n + 1, -1);
  queue<int> que;

  parent[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (parent[nv] != -1)
        continue;
      parent[nv] = v;
      que.push(nv);
    }
  }

  for (int i = 2; i <= n; i++) {
    if (parent[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
}


Error: TLE

Fixed submission:

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

#define all(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;

using Graph = V<V<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph 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);
  }

  V<int> dis(n + 1, -1);
  queue<int> que;
  V<int> ans;

  for (int i = 2; i <= n; i++) {
    dis[i] = i;
    que.push(i);

    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : g[v]) {
        if (dis[nv] != -1)
          continue;
        if (nv == 1) {
          dis[nv] = v;
          while (1) {
            if (dis[nv] == i)
              break;
            nv = dis[nv];
          }
          ans.push_back(nv);
        }
        dis[nv] = dis[v];
        que.push(nv);
      }
    }
    dis.assign(n + 1, -1);
    queue<int>().swap(que);
  }

  if (ans.size() == n - 1) {
    cout << "Yes" << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> R;
  for (int i = 0; i < N; i++) {
    vector<int> X;
    R.push_back(X);
  }
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    R[A].push_back(B);
    R[B].push_back(A);
  }
  int mark[N];
  for (int i = 0; i < N; i++) {
    mark[i] = -1;
  }
  mark[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int P = que.front();
    que.pop();
    for (int i = 0; i < (int)R[P].size(); i++) {
      if (mark[R[P][i]] == -1) {
        que.push(R[P][i]);
        mark[R[P][i]] = P;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << mark[i] + 1 << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  int A[N], B[N];
  vector<vector<int>> R;
  for (int i = 0; i < N; i++) {
    vector<int> X;
    R.push_back(X);
  }
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    R[A[i]].push_back(B[i]);
    R[B[i]].push_back(A[i]);
  }
  int mark[N];
  for (int i = 0; i < N; i++) {
    mark[i] = -1;
  }
  queue<int> que;
  que.push(0);
  while (que.size() > 0) {
    int P = que.front();
    que.pop();
    for (int i = 0; i < (int)R[P].size(); i++) {
      if (mark[R[P][i]] == -1) {
        que.push(R[P][i]);
        mark[R[P][i]] = P;
      }
    }
  }
  bool chk = true;
  for (int i = 1; i < N; i++) {
    if (mark[i] == -1)
      chk = false;
  }
  if (chk) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << mark[i] + 1 << endl;
    }
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> to[100005];
int n, m;
vector<int> root;

int main() {

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

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

  while (!que.empty()) {

    int now = que.front();
    que.pop();

    for (int u : to[now]) {
      if (root[u] == -1) {
        que.push(u);
        root[u] = now;
      }
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> a, b;
vector<vector<bool>> graph;
int n, m;
vector<int> root;

int main() {

  cin >> n >> m;
  a = b = vector<int>(m);
  root = vector<int>(n + 1, -1);
  rep(i, m) cin >> a[i] >> b[i];

  queue<int> que;

  que.push(1);

  while (que.size() != 0) {

    int now = que.front();
    que.pop();

    rep(i, m) {
      if (a[i] == now && root[b[i]] == -1) {
        que.push(b[i]);
        root[b[i]] = now;
      } else if (b[i] == now && root[a[i]] == -1) {
        que.push(a[i]);
        root[a[i]] = now;
      }
    }
  }

  for (int i = 2; i <= n; ++i) {
    if (root[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

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

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

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
int main() {

  long long n, m, a, b;
  cin >> n >> m;

  vector<vector<long long>> adj(n, vector<long long>());
  while (m--) {
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<int> d(n, INT_MAX), vis(n, 0), p(n, -1);

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

  d[0] = 0;

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

    for (auto u : adj[v]) {
      if (!vis[u]) {
        vis[u] = 1;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }

  if (count(vis.begin(), vis.end(), 0) == 0) {
    cout << "Yes" << endl;
    for (int v = 1; v < n; v++)
      cout << p[v] + 1 << endl;
  }

  else
    cout << "No" << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
int main() {

  long long n, m, a, b;
  cin >> n >> m;

  vector<vector<long long>> adj(n, vector<long long>());
  while (m--) {
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<int> d(n, INT_MAX), vis(n, 0), p(n, -1);

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

  d[0] = 0;

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

    for (auto u : adj[v]) {
      if (!vis[u]) {
        vis[u] = 1;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }

  if (count(vis.begin(), vis.end(), 0) == 0) {
    cout << "Yes" << endl;
    for (int v = 1; v < n; v++) {
      vector<int> path;
      for (int i = v; i != -1; i = p[i])
        path.push_back(i);
      cout << path[1] + 1 << endl;
    }

  }

  else
    cout << "No" << endl;

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

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9 + 5;
const int N = 1e5;
const int MOD = 998244353;
const double EPS = 1e-9;

const double PI = acos(-1);

set<int> gr[N + 1];
bool used[N + 1];
int ans[N + 1];

void bfs() {
  queue<pair<int, int>> que;
  que.push({1, 0});
  used[1] = 1;
  while (!que.empty()) {
    int cur = que.front().first, val = que.front().second;
    que.pop();
    if (!ans[cur])
      ans[cur] = val;
    for (int x : gr[cur]) {
      if (!used[x]) {
        que.push({x, cur});
        used[x] = 1;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    gr[x].insert(y);
    gr[y].insert(x);
  }
  bfs();
  bool ok = 1;
  for (int i = 2; i <= n; i++)
    ok &= used[i];
  if (!ok) {
    cout << "No";
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9 + 5;
const int N = 1e5;
const int MOD = 998244353;
const double EPS = 1e-9;

const double PI = acos(-1);

set<int> gr[N + 1];
bool used[N + 1];
int ans[N + 1];

void bfs() {
  queue<pair<int, int>> que;
  que.push({1, 0});
  while (!que.empty()) {
    int cur = que.front().first, val = que.front().second;
    que.pop();
    used[cur] = 1;
    if (!ans[cur])
      ans[cur] = val;
    for (int x : gr[cur]) {
      if (!used[x])
        que.push({x, cur});
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    gr[x].insert(y);
    gr[y].insert(x);
  }
  bfs();
  bool ok = 1;
  for (int i = 2; i <= n; i++)
    ok &= used[i];
  if (!ok) {
    cout << "No";
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

// ダイクストラ
// 負の重みの辺を含むグラフには適用できない
int N, M;
const int INF = 1000000000;
vector<int> p;
vector<vector<pair<int, int>>> G; // first : 頂点, second : 有向辺の重み

void dijkstra(int r) {
  // first : スタート地点からのコスト, second : 頂点
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;
  vector<int> color(N);
  vector<int> d(N, INF);
  color.at(r) = 1;
  d.at(r) = 0;
  PQ.push(make_pair(0, r));

  while (!PQ.empty()) {
    // 最短経路木に追加する候補からコスト最小の頂点 u を取り出す
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color.at(u) = 2; // 最短経路木に追加

    if (d.at(u) < f.first)
      continue;

    // u に隣接する頂点を候補に追加
    for (auto cand : G.at(u)) {
      int v = cand.first;
      if (color.at(v) == 2)
        continue;
      if (d.at(u) + cand.second < d.at(v)) {
        d.at(v) = d.at(u) + cand.second;
        PQ.push(make_pair(d.at(v), v));
        color.at(v) = 1;
        p.at(v) = u + 1;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  G.resize(N);
  p = vector<int>(N, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(make_pair(b, 1));
    G.at(b).push_back(make_pair(a, 1));
  }
  dijkstra(0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << p.at(i) << endl;
  }
}

Error: 0

Fixed submission:

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

// ダイクストラ
// 負の重みの辺を含むグラフには適用できない
int N, M;
const int INF = 1000000000;
vector<int> p;
vector<vector<pair<int, int>>> G; // first : 頂点, second : 有向辺の重み

void dijkstra(int r) {
  // first : スタート地点からのコスト, second : 頂点
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;
  vector<int> color(N);
  vector<int> d(N, INF);
  color.at(r) = 1;
  d.at(r) = 0;
  PQ.push(make_pair(0, r));

  while (!PQ.empty()) {
    // 最短経路木に追加する候補からコスト最小の頂点 u を取り出す
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color.at(u) = 2; // 最短経路木に追加

    if (d.at(u) < f.first)
      continue;

    // u に隣接する頂点を候補に追加
    for (auto cand : G.at(u)) {
      int v = cand.first;
      if (color.at(v) == 2)
        continue;
      if (d.at(u) + cand.second < d.at(v)) {
        d.at(v) = d.at(u) + cand.second;
        PQ.push(make_pair(d.at(v), v));
        color.at(v) = 1;
        p.at(v) = u + 1;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  G.resize(M);
  p = vector<int>(N, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(make_pair(b, 1));
    G.at(b).push_back(make_pair(a, 1));
  }
  dijkstra(0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << p.at(i) << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  long long int n = 0, m = 0;
  cin >> n >> m;
  vector<vector<long int>> g(n, vector<long int>());
  vector<int> yet(n, 0);
  vector<pair<int, int>> ans(n, {0, 0});
  for (int i = 0; i < m; i++) {
    long int l = 0, f = 0;
    cin >> l >> f;
    l--;
    f--;
    g[l].push_back(f);
    g[f].push_back(l);
  }
  queue<pair<long int, int>> q;
  pair<long int, int> a, b;
  long int cur;
  q.push({0, 0});
  yet[0]++;

  while (!q.empty()) {
    a = q.front();
    q.pop();
    cur = a.first;
    a.second++;
    for (int i = 0; i < g[cur].size(); i++) {
      if (yet[g[cur][i]] == 0) {
        yet[g[cur][i]]++;
        q.push({g[cur][i], a.second});
        ans[g[cur][i]].first = a.second;
        ans[g[cur][i]].second = cur + 1;
      }
    }
  }

  for (int i = 1; i < n; i++) {
    if (ans[i].first == 0) {
      cout << "No\\n";
      return 0;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  long long int n = 0, m = 0;
  cin >> n >> m;
  vector<vector<long int>> g(n, vector<long int>());
  vector<int> yet(n, 0);
  vector<int> ans(n, 0);
  for (int i = 0; i < m; i++) {
    long int a = 0, b = 0;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<pair<long int, int>> q;
  pair<long int, int> a, b;
  long int cur;

  for (int i = 1; i < n; i++) {
    q.push({i, 0});
    while (!q.empty()) {
      a = q.front();
      q.pop();
      cur = a.first;
      if (cur == 0) {
        ans[i] = a.second;
        break;
      } else {
        yet[cur]++;
        a.second++;
        for (int k = 0; k < g[cur].size(); k++) {
          if (yet[g[cur][k]] == 0) {
            q.push({g[cur][k], a.second});
          }
        }
      }
    }
    while (!q.empty()) {
      q.pop();
    }
    for (int d = 0; d < n; d++) {
      yet[d] = 0;
    }
  }
  for (int f = 1; f < n; f++) {
    if (ans[f] == 0) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    for (int k = 0; k < g[i].size(); k++) {
      if (ans[i] - 1 == ans[g[i][k]]) {
        g[i][k]++;
        cout << g[i][k] << endl;
        break;
      }
    }
  }
  return 0;
}