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

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e9
#define mod 1000000007
// priority_queue<llint,vector<llint>,greater<llint> > que;
// priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

void solve() {
  cin >> n >> m;
  g.resize(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);
  }
  vector<int> dist(n + 1, inf);
  vector<int> pre(n + 1);
  queue<int> que;

  dist[1] = 0;
  que.push(1);
  // BFS
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (dist[nv] != inf)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
      pre[nv] = v;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      continue;
    cout << pre[i] << endl;
  }
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e9
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m;
vector<vector<int>> g;
vector<int> x, d;
vector<bool> used;

void dijk(int s) {
  for (int i = 1; i <= n; i++) {
    d[i] = x[i] = inf;
    used[i] = false;
  }
  d[s] = 0;
  while (1) {
    int v = -1;
    for (int u = 1; u <= n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    for (int u = 1; u <= n; u++) {
      if (d[u] > d[v] + g[v][u]) {
        d[u] = d[v] + g[v][u];
        x[u] = v;
      }
    }
  }
}

void solve() {
  cin >> n >> m;
  g.resize(n + 1);
  for (int i = 1; i <= n; i++)
    g[i].resize(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      g[i][j] = inf;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a][b] = 1;
    g[b][a] = 1;
  }
  x.resize(n + 1);
  used.resize(n + 1);
  d.resize(n + 1);
  dijk(1);
  bool f = true;
  for (int i = 2; i <= n; i++) {
    if (d[i] == inf) {
      f = false;
      break;
    }
  }
  if (!f)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << x[i] << endl;
    }
  }
}

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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, m) {
    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;
    cout << pre[i] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n);
  vector<int> dist(n, inf);
  vector<int> pre(n);
  rep(i, 0, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    int q = v[p].size() + 1;
    rep(i, 0, q) {
      if (dist[i] != inf)
        continue;
      dist[i] = dist[p] + 1;
      pre[i] = p;
      que.push(i);
    }
  }
  cout << "Yes" << endl;
  rep(i, 0, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <time.h>
#include <vector>

struct edge {
  int to;
  int cost;
  edge(int to2, int cost2) {
    to = to2;
    cost = cost2;
  }
};

using namespace std;
typedef pair<int, int> P; // first: 最短距離, second: 頂点番号

#define MAX_V 1000000

int V, d[MAX_V];
vector<edge> G[MAX_V]; // 隣接リスト表現
long ROOT[MAX_V];
void dijkstra(int s) {
  for (long i = 0; i < V; i++) {
    d[i] = 1000000;
  }
  d[s] = 0; // 始点sへの最短距離は0
  ROOT[0] = 0;

  priority_queue<P, vector<P>, greater<P>>
      que; // 距離が小さい順に取り出せるようgreater<P>を指定
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second; // 更新した頂点の中で距離が最小の頂点v
    if (d[v] < p.first) {
      continue;
    }
    for (long i = 0; i < G[v].size(); i++) { // 頂点vから出る辺eを走査
      if (d[G[v][i].to] > d[v] + G[v][i].cost) {
        d[G[v][i].to] = d[v] + G[v][i].cost;
        que.push(P(d[G[v][i].to], G[v][i].to));
        ROOT[G[v][i].to] = v;
      }
    }
  }
}

int main() {
  long M;
  std::cin >> V >> M;
  for (long i = 0; i < M; i++) {
    long A, B;
    std::cin >> A >> B;
    G[A - 1].push_back(edge(B - 1, 1));
    G[B - 1].push_back(edge(A - 1, 1));
  }
  dijkstra(0);
  std::cout << "Yes" << std::endl;
  for (long i = 1; i < V; i++) {
    std::cout << ROOT[i] + 1 << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <time.h>
#include <vector>

struct edge {
  int to;
  int cost;
  edge(int to2, int cost2) {
    to = to2;
    cost = cost2;
  }
};

using namespace std;
typedef pair<int, int> P; // first: 最短距離, second: 頂点番号

#define MAX_V 1000000

int V, d[MAX_V];
vector<edge> G[MAX_V]; // 隣接リスト表現
vector<long> ROOT[MAX_V];
void dijkstra(int s) {
  for (long i = 0; i < V; i++) {
    d[i] = 1000000;
  }
  d[s] = 0; // 始点sへの最短距離は0
  ROOT[s].push_back(0);

  priority_queue<P, vector<P>, greater<P>>
      que; // 距離が小さい順に取り出せるようgreater<P>を指定
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second; // 更新した頂点の中で距離が最小の頂点v
    if (d[v] < p.first) {
      continue;
    }
    for (long i = 0; i < G[v].size(); i++) { // 頂点vから出る辺eを走査
      if (d[G[v][i].to] > d[v] + G[v][i].cost) {
        d[G[v][i].to] = d[v] + G[v][i].cost;
        que.push(P(d[G[v][i].to], G[v][i].to));
        ROOT[G[v][i].to].clear();
        for (long j = 0; j < ROOT[v].size(); j++) {
          ROOT[G[v][i].to].push_back(ROOT[v][j]);
        }
        ROOT[G[v][i].to].push_back(G[v][i].to);
      }
    }
  }
}

int main() {
  long M;
  std::cin >> V >> M;
  for (long i = 0; i < M; i++) {
    long A, B;
    std::cin >> A >> B;
    G[A - 1].push_back(edge(B - 1, 1));
    G[B - 1].push_back(edge(A - 1, 1));
  }
  dijkstra(0);
  std::cout << "Yes" << std::endl;
  for (long i = 1; i < V; i++) {
    std::cout << ROOT[i][ROOT[i].size() - 2] + 1 << std::endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1);
const ll INF = 1e15;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

const ll MAXN = 100000;
vl G[MAXN];

int main() {
  ll n, m;
  cin >> n >> m;
  vl a(m), b(m);
  cout << "Yes" << endl;
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  vl X(n, INF);
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll x = q.front();
    q.pop();
    ll k = G[x].size();
    rep(i, k) {
      if (X[G[x][i]] == INF) {
        X[G[x][i]] = x;
        q.push(G[x][i]);
      }
    }
  }
  rep(i, n - 1) { cout << X[i + 1] + 1 << endl; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = 3.14159265358979;
const ll INF = 1e15;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

const ll MAX_N = 100000;
vl G[MAX_N];
vl cost(MAX_N, INF);
vl X(MAX_N, -1);

void bfs(ll x, ll y) {
  ll k = G[x].size();
  cost[x] = y;
  rep(i, k) {
    if (cost[G[x][i]] > y + 1) {
      X[G[x][i]] = x;
      bfs(G[x][i], y + 1);
    }
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  vl a(m), b(m);
  cout << "Yes" << endl;
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  bfs(0, 0);
  FOR(i, 1, n) { cout << X[i] + 1 << endl; }
}
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;

vector<int> dist;
vector<int> pre;
queue<int> que;
bool can = false;

void bfs(vector<vector<int>> &g) {
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (auto nv : g[v]) {
      if (dist[nv] != 0)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }
}

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

  dist.assign(n, 0);
  pre.assign(n, 0);
  que.push(0);

  bfs(g);

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

vector<int> dist;
vector<int> pre;
queue<int> que;
bool can = false;

int bfs(vector<vector<int>> &g) {
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (auto nv : g[v]) {
      if (dist[nv] != 0)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = dist[v];
      que.push(nv);
    }
  }
}

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

  dist.assign(n, 0);
  pre.assign(n, 0);
  que.push(0);

  bfs(g);

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>

using namespace std;

vector<int> adj[100100];
int main() {
  int n, i, x, m, a, b;
  cin >> n >> m;
  vector<bool> used(n + 1);
  vector<int> p(n + 1);
  queue<int> q;
  q.push(1);
  p[1] = -1;
  for (i = 1; i <= n; i++) {
    used[i] = false;
  }
  used[1] = true;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int u : adj[x]) {
      if (!used[u]) {
        q.push(u);
        used[u] = true;
        p[u] = x;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (i = 2; i <= n; i++) {
    cout << p[i] << "\\n";
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>

using namespace std;

vector<int> adj[100100];
int main() {
  int n, i, x, m, a, b;
  vector<bool> used(n + 1);
  vector<int> p(n + 1);
  queue<int> q;
  used[1] = true;
  q.push(1);
  p[1] = -1;
  for (i = 2; i <= n; i++) {
    used[i] = false;
  }
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int u : adj[x]) {
      if (!used[u]) {
        q.push(u);
        used[u] = true;
        p[u] = x;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (i = 2; i <= n; i++) {
    cout << p[i];
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>

using namespace std;

vector<int> adj[100100]; // adjacency list
int main() {
  int m, i, a, b, v;
  int n; // number of nodes
  int s; // source vertex
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  queue<int> q;
  vector<bool> used(n);
  vector<int> p(n + 1);
  q.push(1);
  for (i = 1; i <= n; i++) {
    used[i] = false;
  }
  used[1] = true;
  p[1] = -1;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    // used[v]=true;
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        p[u] = v;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (i = 2; i <= n; i++) {
    cout << p[i] << "\\n";
  }
  return 0;
}


Error: WA

Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>

using namespace std;

vector<int> adj[100100]; // adjacency list
int main() {
  int m, i, a, b;
  int n; // number of nodes
  int s; // source vertex
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  queue<int> q;
  vector<bool> used(n);
  vector<int> d(n), p(n);
  s = 1;
  q.push(s);
  for (i = 1; i <= n; i++) {
    used[i] = false;
  }
  used[s] = true;
  p[s] = -1;
  d[s] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    // used[v]=true;
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES"
       << "\\n";
  for (i = 2; i <= n; i++) {
    cout << p[i] << "\\n";
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const int INF = 100001;

int main(void) {
  long N, M;
  cin >> N >> M;
  // vector<vector<bool>> road(N, vector<bool>(N, false));
  map<int, vector<int>> road;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

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

  vector<int> where_from(N, -1);
  where_from[0] = 1;

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

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

    for (int i = 0; i < road[room].size(); i++) {
      // 初めて訪れたら
      if (where_from[road[room][i]] == -1) {
        que.push(road[room][i]);
        where_from[road[room][i]] = room;
      }
    }
  }

  // いけるか
  for (int i = 1; i < N; i++) {
    if (where_from[i] < 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << where_from[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const int INF = 100001;

int main(void) {
  long N, M;
  cin >> N >> M;
  vector<vector<bool>> road(N, vector<bool>(N, false));
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    road[a][b] = road[b][a] = true;
  }

  vector<int> score(N, INF), where_from(N, -1);
  score[0] = 0;
  where_from[0] = 1;

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

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

    // つながっている道を探す
    // 遠い方にだけ進む
    for (int i = 0; i < N; i++) {
      // 初めて訪れたら
      // 道がつながっていたら
      if (where_from[i] == -1 && road[room][i]) {
        que.push(i);

        score[i] = score[room] + 1;
        where_from[i] = room;
      }
    }
  }

  // いけるか
  for (int i = 1; i < N; i++) {
    if (where_from[i] < 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << where_from[i] + 1 << endl;
  }

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

#include "bits/stdc++.h"
#define ll long long int
using namespace std;
unordered_map<ll, ll> sign;
vector<ll> g[100001];
bool vis[100001];
void bfs(ll node) {
  queue<ll> q;
  q.push(node);
  vis[node] = true;
  while (!q.empty()) {
    ll u = q.front();
    q.pop();
    for (auto &p : g[u]) {
      if (vis[p] == false) {
        q.push(p);
        vis[p] = true;
        if (sign[p] == -1) {
          sign[p] = u;
        }
      }
    }
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  ll x, y, i;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    sign[x] = -1;
    sign[y] = -1;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  memset(vis, false, sizeof(vis));
  bfs(1);
  for (i = 2; i <= n; i++) {
    if (sign[i] == -1) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (i = 2; i <= n; i++) {
    cout << sign[i] << "\\n";
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define ll long long int
using namespace std;
unordered_map<ll, ll> sign;
vector<ll> g[100001];
bool vis[100001];
void bfs(ll node) {
  queue<ll> q;
  q.push(node);
  vis[node] = true;
  while (!q.empty()) {
    ll u = q.front();
    q.pop();
    vis[u] = true;
    for (auto &p : g[u]) {
      if (vis[p] == false) {
        q.push(p);
        if (sign[p] == -1) {
          sign[p] = u;
        }
      }
    }
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  ll x, y, i;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    sign[x] = -1;
    sign[y] = -1;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  memset(vis, false, sizeof(vis));
  bfs(1);
  for (i = 2; i <= n; i++) {
    if (sign[i] == -1) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (i = 2; i <= n; i++) {
    cout << sign[i] << "\\n";
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, M;
  cin >> N >> M;
  vector<long> data(N, INT_MAX);
  vector<vector<long>> path(N, vector<long>());
  long a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    path.at(a - 1).push_back(b - 1);
    path.at(b - 1).push_back(a - 1);
  }
  queue<long> que;
  que.push(0);
  long size, sub;
  while (que.size() != 0) {
    size = que.size();
    for (int i = 0; i < size; i++) {
      sub = que.front();
      que.pop();
      for (unsigned int j = 0; j < path.at(sub).size(); j++) {
        if (data.at(path.at(sub).at(j)) == INT_MAX) {
          data.at(path.at(sub).at(j)) = sub;
          que.push(path.at(sub).at(j));
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << data.at(i) + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, M;
  cin >> N >> M;
  vector<long> data(N, INT_MAX);
  vector<vector<long>> path(N, vector<long>(N));
  long a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    path.at(a - 1).push_back(b - 1);
    path.at(b - 1).push_back(a - 1);
  }
  queue<long> que;
  que.push(0);
  long size, sub;
  while (que.size() != 0) {
    size = que.size();
    for (int i = 0; i < size; i++) {
      sub = que.front();
      que.pop();
      for (unsigned int j = 0; j < path.at(sub).size(); j++) {
        if (data.at(path.at(sub).at(j)) == INT_MAX) {
          data.at(path.at(sub).at(j)) = sub;
          que.push(path.at(sub).at(j));
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << data.at(i) + 1 << endl;
  return 0;
}