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;
const int INF = 10000000;

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

  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;

  rep(i, n) {
    if (i == 0)
      continue;

    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int INF = 10000000;

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

  vector<int> to[1000001000];

  rep(i, m) {
    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;

  rep(i, n) {
    if (i == 0)
      continue;

    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const long long LINF = 1000000000000000018ll;
const int INF = 1000000009;
const int mod = 1000000007;
const int mod2 = 998244353;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n, m;
vector<vector<int>> g;

vector<int> ans;

void bfs(int src_v) {
  vector<int> dist(n, -1);
  dist[src_v] = 0;
  queue<int> q;
  q.push(src_v);
  while (!q.empty()) {
    int cur_v = q.front();
    q.pop();
    for (int i = 0; i < (int)g[cur_v].size(); i++) {
      int next_v = g[cur_v][i];
      if (dist[next_v] != -1) {
        continue;
      }
      ans[next_v] = cur_v + 1;
      dist[next_v] = dist[cur_v] + 1;
      q.push(next_v);
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  cin >> n >> m;

  ans = vector<int>(n);
  g = vector<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);
  }

  bfs(0);

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const long long LINF = 1000000000000000018ll;
const int INF = 1000000009;
const int mod = 1000000007;
const int mod2 = 998244353;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n, m;
vector<vector<int>> g;

vector<int> ans;

int bfs(int src_v) {
  vector<int> dist(n, -1);
  dist[src_v] = 0;
  queue<int> q;
  q.push(src_v);
  while (!q.empty()) {
    int cur_v = q.front();
    q.pop();
    for (int i = 0; i < (int)g[cur_v].size(); i++) {
      int next_v = g[cur_v][i];
      if (dist[next_v] != -1) {
        continue;
      }
      ans[next_v] = cur_v + 1;
      dist[next_v] = dist[cur_v] + 1;
      q.push(next_v);
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  cin >> n >> m;

  ans = vector<int>(n);
  g = vector<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);
  }

  bfs(0);

  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << ans[i] << "\\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<int> A(M), B(M);
  for (int i = 0; i < M; i++)
    cin >> A.at(i) >> B.at(i);
  queue<int> decide;
  set<int> no_decide;
  vector<int> torch(N - 1);
  decide.push(1);
  for (int i = 2; i <= N; i++)
    no_decide.insert(i);

  vector<set<int>> path(N);
  int a, b;
  for (int i = 0; i < M; i++) {
    a = A.at(i);
    b = B.at(i);
    path.at(a - 1).insert(b);
    path.at(b - 1).insert(a);
  }

  int t, u;
  while (!decide.empty() && !no_decide.empty()) {
    t = decide.front();
    decide.pop();
    for (auto i = path.at(t - 1).begin(); i != path.at(t - 1).end(); i++) {
      u = *i;
      if (no_decide.find(u) != no_decide.end()) {
        no_decide.erase(u);
        decide.push(u);
        torch.at(u - 2) = t;
      }
    }
  }
  if (!no_decide.empty())
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 0; i < N - 1; i++)
      cout << torch.at(i) << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (int i = 0; i < M; i++)
    cin >> A.at(i) >> B.at(i);
  queue<int> decide;
  set<int> no_decide;
  vector<int> torch(N - 1);
  decide.push(1);
  for (int i = 2; i <= N; i++)
    no_decide.insert(i);
  int t, u;
  while (!decide.empty() && !no_decide.empty()) {
    t = decide.front();
    decide.pop();
    for (int i = 0; i < M; i++) {
      if (A.at(i) == t) {
        u = B.at(i);
        if (no_decide.find(u) != no_decide.end()) {
          no_decide.erase(u);
          decide.push(u);
          torch.at(u - 2) = t;
        }
      }
      if (B.at(i) == t) {
        u = A.at(i);
        if (no_decide.find(u) != no_decide.end()) {
          no_decide.erase(u);
          decide.push(u);
          torch.at(u - 2) = t;
        }
      }
    }
  }
  if (!no_decide.empty())
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 0; i < N - 1; i++)
      cout << torch.at(i) << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define node_num 100005
#define edge_num 400005
#define INF 100000000
class node {
public:
  // 这里的dis并不是主要的dis，只是用来比较，完善pq的
  int id, dis;
  bool operator<(const node &a) const { return a.dis < dis; }
};
int pre[edge_num];
int to[edge_num];
int val[edge_num];
int ans[node_num];
int tot; // 从1开始
int last[node_num];
int ndis[node_num];
int used[node_num];
void add(int u, int v) {
  tot++;
  to[tot] = v;
  val[tot] = 1;
  pre[tot] = last[u];
  last[u] = tot;
}
int n, m;
int a, b, c;
int main() {
  priority_queue<node> qu;
  scanf("%d%d", &n, &m);
  fill(ndis, ndis + node_num, INF);
  ndis[1] = 0;
  /*数据输入*/
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  qu.push(node{1, ndis[1]});
  /*主循环*/
  while (!qu.empty()) {
    /*弹出元素*/
    int x = qu.top().id;
    int ptr = last[x];
    qu.pop();
    /*判断终止*/
    if (used[x])
      continue;
    used[x] = 1;
    /*遍历松弛*/
    while (ptr) {
      int y = to[ptr];
      if (!used[y] && ndis[y] > ndis[x] + val[ptr]) {
        ndis[y] = ndis[x] + val[ptr];
        ans[y] = x;
        qu.push(node{y, ndis[y]});
      }
      ptr = pre[ptr];
    }
  }
  bool flag = true;
  for (int i = 2; i <= n; i++) {
    if (ndis[i] == INF) {
      flag = false;
      break;
    }
  }
  if (flag) {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", ans[i]);
    }
  } else
    printf("No\\n");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define node_num 10005
#define edge_num 40005
#define INF 100000000
class node {
public:
  // 这里的dis并不是主要的dis，只是用来比较，完善pq的
  int id, dis;
  bool operator<(const node &a) const { return a.dis < dis; }
};
int pre[edge_num];
int to[edge_num];
int val[edge_num];
int ans[node_num];
int tot; // 从1开始
int last[node_num];
int ndis[node_num];
int used[node_num];
void add(int u, int v) {
  tot++;
  to[tot] = v;
  val[tot] = 1;
  pre[tot] = last[u];
  last[u] = tot;
}
int n, m;
int a, b, c;
int main() {
  priority_queue<node> qu;
  scanf("%d%d", &n, &m);
  fill(ndis, ndis + node_num, INF);
  ndis[1] = 0;
  /*数据输入*/
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  qu.push(node{1, ndis[1]});
  /*主循环*/
  while (!qu.empty()) {
    /*弹出元素*/
    int x = qu.top().id;
    int ptr = last[x];
    qu.pop();
    /*判断终止*/
    if (used[x])
      continue;
    used[x] = 1;
    /*遍历松弛*/
    while (ptr) {
      int y = to[ptr];
      if (!used[y] && ndis[y] > ndis[x] + val[ptr]) {
        ndis[y] = ndis[x] + val[ptr];
        ans[y] = x;
        qu.push(node{y, ndis[y]});
      }
      ptr = pre[ptr];
    }
  }
  bool flag = true;
  for (int i = 2; i <= n; i++) {
    if (ndis[i] == INF) {
      flag = false;
      break;
    }
  }
  if (flag) {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", ans[i]);
    }
  } else
    printf("No\\n");
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define MAX 100010

using namespace std;

struct node {
  int index;
  int way = 0;
  vector<int> room;
};

void printans(vector<node> &cavemap, int &cnt, int &N) {
  if (cnt < N - 1)
    cout << "No" << '\\n';
  else {
    cout << "Yes" << '\\n';
    for (int i = 2; i <= N; i++)
      cout << cavemap[i].way << '\\n';
  }
}

void findans(vector<node> &cavemap, int &cnt, int &N, queue<int> &temp,
             int index) {
  if (temp.size() != 0)
    temp.pop();
  if (cnt > N - 1)
    return;
  int length = cavemap[index].room.size();
  for (int i = 0; i < length; i++)
    if (cavemap[cavemap[index].room[i]].way == 0) {
      cavemap[cavemap[index].room[i]].way = index;
      temp.push(cavemap[index].room[i]);
      cnt++;
    }
  if (temp.size() == 0)
    return;
  findans(cavemap, cnt, N, temp, temp.front());
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, cnt = 0, a, b;
  ;
  cin >> N >> M;
  vector<node> cavemap(MAX);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    cavemap[a].index = a;
    cavemap[b].index = b;
    cavemap[a].room.push_back(b);
    cavemap[b].room.push_back(a);
  }
  queue<int> temp;
  findans(cavemap, cnt, N, temp, 1);
  printans(cavemap, cnt, N);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define MAX 100010

using namespace std;

void printans(int *ans, int &cnt, int &N) {
  if (cnt < N - 1)
    cout << "No" << '\\n';
  else {
    cout << "Yes" << '\\n';
    for (int i = 2; i <= N; i++)
      cout << ans[i] << '\\n';
  }
}

void findans(set<long int> &cavemap, int *ans, int &cnt, int &N,
             queue<int> &temp, long int index) {
  if (temp.size() != 0)
    temp.pop();
  if (cnt >= N - 1)
    return;
  for (int i = 2; i <= N; i++)
    if (cavemap.find(index * 100010 + i) != cavemap.end() && ans[i] == 0) {
      ans[i] = index;
      temp.push(i);
      cnt++;
    }
  if (temp.size() == 0)
    return;
  findans(cavemap, ans, cnt, N, temp, temp.front());
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  set<long int> cavemap;
  int ans[MAX] = {0}, cnt = 0;
  long int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    cavemap.insert(a * 100010 + b);
    cavemap.insert(b * 100010 + a);
  }
  queue<int> temp;
  findans(cavemap, ans, cnt, N, temp, 1);
  printans(ans, cnt, 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 < (n); i++)

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

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

  vector<int> dep(n, 1001001001);
  vector<int> dir(n);
  queue<int> q;
  dep[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (int j : to[i]) {
      if (dep[j] > dep[i] + 1) {
        dep[j] = dep[i] + 1;
        dir[j] = i;
        q.push(j);
      } else if (dep[j] > dep[i] + 1) {
        dep[j] = dep[i] + 1;
        dir[j] = i;
        q.push(j);
      }
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << dir[i] + 1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  vector<int> dep(n, 1001001001);
  vector<int> dir(n);
  queue<int> q;
  dep[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    rep(j, m) {
      if (a[j] == i && dep[b[j]] > dep[i] + 1) {
        dep[b[j]] = dep[i] + 1;
        dir[b[j]] = i;
        q.push(b[j]);
      } else if (b[j] == i && dep[a[j]] > dep[i] + 1) {
        dep[a[j]] = dep[i] + 1;
        dir[a[j]] = i;
        q.push(a[j]);
      }
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << dir[i] + 1 << endl;
  }

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

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

int main() {
  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);
  }

  vector<int> prev(n, -1);
  queue<int> que;
  vector<int> dist(n, -1);
  que.push(0);
  dist[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] == -1) {
        prev[nv] = v;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
  }

  vector<int> ans;
  for (int i = 1; i < n; i++) {
    int x = prev[i];
    if (x != -1) {
      ans.push_back(x + 1);
    }
  }
  if (ans.size() > 0) {
    cout << "Yes"
         << "\\n";
    for (auto v : ans) {
      cout << v << "\\n";
    }
  } else {
    cout << "No"
         << "\\n";
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  //   int a[1000000], b[1000000];
  cin >> n >> m;
  Graph G(n);
  for (int i = 0; i < m; i++) {
    // cin >> a[i] >> b[i];
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans;

  for (int i = 1; i < n; i++) {
    vector<int> prev(n);
    vector<int> dist(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) {
          prev[nv] = v;
          dist[nv] = dist[v] + 1;
          que.push(nv);
        }
      }
    }
    if (dist[0] != -1) {
      int x = 0;
      int px = prev[0];
      while (px != i) {
        x = px;
        px = prev[px];
      }
      ans.push_back(x + 1);
    }
  }
  if (ans.size() > 0) {
    cout << "Yes"
         << "\\n";
    for (auto v : ans) {
      cout << v << "\\n";
    }
  } else {
    cout << "No"
         << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  // n=頂点数, m=辺数
  int n, m;
  cin >> n >> m;

  // graph
  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);
  }

  // BFS
  vector<int> depth(n + 1, -1);
  vector<int> adj(n + 1, -1);
  queue<int> que;
  depth[1] = 1;
  que.push(1);

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

    for (int nv : g[v]) {
      if (depth[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな頂点をキューに追加
      depth[nv] = depth[v] + 1;
      adj[nv] = v;
      que.push(nv);
    }
  }

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

Error: 0

Fixed submission:


#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  // input
  int n, m;
  cin >> n >> m;

  vector<vector<int>> path(n, vector<int>(n, -2));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    path.at(a - 1).at(b - 1) = -1;
    path.at(b - 1).at(a - 1) = -1;
  }

  // process
  vector<int> current, next;
  current.push_back(0);
  int flag = 1;
  while (flag) {
    for (int x : current) {
      // cout << "basepoint " << x << endl;
      for (int i = 0; i < n; i++) {
        if (path.at(x).at(i) == -1) {
          // cout << "rewrite " << x << " " << i << endl;
          path[x][i] = x;
          path[i][x] = x;
          next.push_back(i);
          // cout << "     push  " << i << endl;
        }
      }

      // debug
      //      for (int i = 0; i < n; i++){
      //        for (int j = 0; j < n; j++){
      //          cout << path.at(i).at(j) << " ";
      //        }
      //        cout << endl;
      //      }
      //      cout << endl;
    }
    if (next.size() == 0) {
      flag = 0;
    } else {
      current = next;
      while (next.size() > 0)
        next.pop_back();
    }
  }

  // output
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    int ret;
    for (int j = 0; j < n; j++) {
      if (path.at(i).at(j) != -2) {
        ret = path.at(i).at(j);
        break;
      }
    }
    cout << ret + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

struct Node {
  vector<int> neighbors;
  bool visited = false;
};

int N, M;
Node nodes[100000];

map<int, int> signs; // 道しるべ

void bfs(int start) {
  nodes[start].visited = true;

  queue<int> search_queue;
  search_queue.push(start);

  while (!search_queue.empty()) {
    // キューから探索対象のノードのインデックスを取り出す
    int i = search_queue.front();
    search_queue.pop();

    for (int n : nodes[i].neighbors) {
      if (nodes[n].visited) {
        continue; // 探索済みならキューに入れない
      }

      nodes[n].visited = true;
      signs[n] = i;
      search_queue.push(n);
    }
  }
}

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

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

  bfs(0);

  cout << "Yes" << endl;

  for (auto itr = signs.begin(); itr != signs.end(); itr++) {
    cout << itr->second + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

struct Node {
  vector<int> neighbors;
  bool visited = false;
};

int N, M;
Node nodes[2000];

map<int, int> signs; // 道しるべ

void bfs(int start) {
  nodes[start].visited = true;

  queue<int> search_queue;
  search_queue.push(start);

  while (!search_queue.empty()) {
    // キューから探索対象のノードのインデックスを取り出す
    int i = search_queue.front();
    search_queue.pop();

    for (int n : nodes[i].neighbors) {
      if (nodes[n].visited) {
        continue; // 探索済みならキューに入れない
      }

      nodes[n].visited = true;
      signs[n] = i;
      search_queue.push(n);
    }
  }
}

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

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

  bfs(0);

  cout << "Yes" << endl;

  for (auto itr = signs.begin(); itr != signs.end(); itr++) {
    cout << itr->second + 1 << endl;
  }

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

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

  vector<vector<ll>> edges(n);
  for (int i = 0; i < m; i++) {
    edges[a[i] - 1].push_back(b[i] - 1);
    edges[b[i] - 1].push_back(a[i] - 1);
  }

  vector<ll> prev(n);
  vector<ll> d(n);
  for (int i = 0; i < n; i++) {
    prev[i] = -1;
    d[i] = LLONG_MAX;
  }
  d[0] = 0;

  queue<ll> q;
  q.push(0);
  while (q.size() > 0) {
    ll node = q.front();
    q.pop();

    for (auto it = edges[node].begin(); it != edges[node].end(); it++) {
      ll nextNode = *it;
      if (d[nextNode] > d[node] + 1) {
        d[nextNode] = d[node] + 1;
        prev[nextNode] = node;
        q.push(nextNode);
      }
    }
  }

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

void UpdateQ(list<ll> &q, ll node, const vector<ll> &d) {
  q.remove(node);
  for (list<ll>::iterator it = q.begin(); it != q.end(); it++) {
    if (d[node] < d[*it]) {
      q.insert(it, node);
      break;
    }
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

  vector<ll> d(n);
  vector<ll> prev(n);
  list<ll> q;
  for (int i = 0; i < n; i++) {
    d[i] = LLONG_MAX;
    prev[i] = -1;
    q.push_back(i);
  }
  d[0] = 0;

  vector<vector<ll>> edges(n);
  for (int i = 0; i < m; i++) {
    edges[a[i] - 1].push_back(b[i] - 1);
    edges[b[i] - 1].push_back(a[i] - 1);
  }

  while (q.size() > 0) {
    ll u = *q.begin();
    q.pop_front();
    for (vector<ll>::iterator iter = edges[u].begin(); iter != edges[u].end();
         iter++) {
      ll v = *iter;
      ll alt = d[u] + 1;
      if (d[v] > alt) {
        d[v] = alt;
        prev[v] = u;
        UpdateQ(q, v, d);
      }
    }
  }

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

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

  return 0;
}