alexjercan/bugnet huggingface dataset:
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++)

typedef long long ll;
typedef pair<ll, ll> Path;
int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> vv(N + 1);
  REP(i, M) {
    Path p;
    cin >> p.first >> p.second;
    vv[p.first].push_back(p.second);
    vv[p.second].push_back(p.first);
  }
  queue<Path> que; // src, dst
  vector<ll> res(N + 1, 0);
  que.push(make_pair(0, 1));
  while (!que.empty()) {
    Path cur_p = que.front();
    que.pop();
    if (res[cur_p.second] <= 0) { // 初訪問
      res[cur_p.second] = cur_p.first;
    }
    for (auto x : vv[cur_p.second]) { // 今いるところから行けるところ // N*M
                                      // になっているっぽい
      if (res[x] == 0) { // 未訪問の時のみ
        que.push(make_pair(cur_p.second, x));
        res[x] = -1; // queueに入ったことを表しておくと重複が減るはず
      }
    }
  }
  cout << "Yes" << endl;
  REP(i, N + 1) {
    if (i > 1)
      cout << res[i] << 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++)

typedef long long ll;
typedef pair<ll, ll> Path;
int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> vv(N + 1);
  REP(i, M) {
    Path p;
    cin >> p.first >> p.second;
    vv[p.first].push_back(p.second);
    vv[p.second].push_back(p.first);
  }
  queue<Path> que; // src, dst
  vector<ll> res(N + 1, 0);
  que.push(make_pair(0, 1));
  while (!que.empty()) {
    Path cur_p = que.front();
    que.pop();
    if (res[cur_p.second] == 0) { // 初訪問
      res[cur_p.second] = cur_p.first;
    }
    for (auto x : vv[cur_p.second]) { // 今いるところから行けるところ
      if (res[x] == 0) {              // 未訪問の時のみ
        que.push(make_pair(cur_p.second, x));
      }
    }
  }
  cout << "Yes" << endl;
  REP(i, N + 1) {
    if (i > 1)
      cout << res[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const double pi = 3.141592653589793;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(M), B(M);
  vector<vector<ll>> Way(N, vector<ll>(0));
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    Way[A[i]].push_back(B[i]);
    Way[B[i]].push_back(A[i]);
  }
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;
  vector<ll> Dist(N, INF);
  vector<ll> Prev(N, -1);
  Dist[0] = 0;
  Q.push(pair<ll, ll>(0, 0));
  while (!Q.empty()) {
    pair<ll, ll> P = Q.top();
    Q.pop();
    ll v = P.second;
    if (Dist[v] < P.first)
      continue;
    ll Sz = Way[v].size();
    for (ll i = 0; i < Sz; i++) {
      ll e = Way[v][i];
      if (Dist[e] > Dist[v] + 1) {
        Dist[e] = Dist[v] + 1;
        Q.push(pair<ll, ll>(Dist[e], e));
        Prev[e] = v;
        // cout<<e+1<<" "<<v+1<<endl;
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const double pi = 3.141592653589793;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(M), B(M);
  vector<vector<ll>> Way(N, vector<ll>(0));
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    Way[A[i]].push_back(B[i]);
    Way[B[i]].push_back(A[i]);
  }
  priority_queue<pair<ll, ll>> Q;
  vector<ll> Dist(N, INF);
  vector<ll> Prev(N, -1);
  Dist[0] = 0;
  Q.push(make_pair(0, 0));
  while (!Q.empty()) {
    pair<ll, ll> P = Q.top();
    Q.pop();
    ll v = P.second;
    if (Dist[v] < P.first)
      continue;
    ll Sz = Way[v].size();
    for (ll i = 0; i < Sz; i++) {
      ll e = Way[v][i];
      if (Dist[e] > Dist[v] + 1) {
        Dist[e] = Dist[v] + 1;
        Q.push(make_pair(Dist[e], e));
        Prev[e] = v;
      }
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; i++) {
    cout << Prev[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

vector<int> to[100001];

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);
  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: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> maze(n, vector<int>(n, -INF));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    maze[a][b] = maze[b][a] = 1;
  }
  vector<int> dist(n, INF);
  vector<int> pre(n, -INF);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (maze[a][i] != -INF) {
        dist[i] = min(dist[a] + 1, dist[i]);
        if (dist[a] + 1 <= dist[i]) {
          dist[i] = dist[a] + 1;
          pre[i] = a;
          q.push(i);
        } else {
          continue;
        }
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int nodes, edges;
  cin >> nodes >> edges;
  vector<int> adj[nodes + 1];
  vector<bool> visited(nodes + 1);
  vector<int> ans(nodes + 1);
  for (int i = 1; i <= edges; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  visited[1] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int next : adj[cur]) {
      if (!visited[next]) {
        q.push(next);
        visited[next] = true;
        ans[next] = cur;
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= nodes; i++)
    cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int nodes, edges;
  cin >> nodes >> edges;
  vector<int> adj[nodes + 1];
  vector<bool> visited(nodes + 1);
  vector<int> ans(nodes + 1);
  for (int i = 1; i <= edges; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  visited[1] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int next : adj[cur]) {
      if (!visited[next]) {
        q.push(next);
        // visited[next] = true;
        ans[next] = cur;
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= nodes; i++)
    cout << ans[i] << "\\n";
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

typedef vector<vector<int>> Graph;
queue<int> que;
vector<int> par; // 各ノードの親
void bfs(int s, Graph &g) {
  que = queue<int>();
  par = vector<int>(g.size(), -1);
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (par[nv] != -1) { // 訪問済み
        continue;
      } else {
        par[nv] = v; // 親ノードを保存
        que.push(nv);
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout.precision(20);

  int n, m;
  cin >> n >> m;
  Graph v(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  bfs(0, v);

  cout << "Yes" << endl;
  REP(i, n - 1) { cout << par[i + 1] + 1 << endl; }

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

typedef vector<vector<int>> Graph;
queue<int> que;
vector<int> par; // 各ノードの親
void bfs(int s, Graph &g) {
  que = queue<int>();
  par = vector<int>(g.size(), -1);
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (par[nv] != -1) { // 訪問済み
        continue;
      } else {
        par[nv] = v; // 親ノードを保存
        que.push(nv);
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout.precision(20);

  int n, m;
  cin >> n >> m;
  Graph v(m);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  bfs(0, v);

  cout << "Yes" << endl;
  REP(i, n - 1) { cout << par[i + 1] + 1 << endl; }

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



#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
#define int int64_t

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int N, M;
vector<vector<int>> AL;
vector<int> D;
vector<int> ans;
const int INF = 1e9;

/* ======================== */
void getInput() {
  cin >> N >> M;
  AL = vector<vector<int>>(N, vector<int>());
  D = vector<int>(N, INF);
  ans = vector<int>(N, -1);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    AL[u].push_back(v);
    AL[v].push_back(u);
  }
}

void solve() {
  // auto nb = AL[0];
  // print(nb);
  // queue<int> que;
  queue<pair<int, int>> que;
  que.push({0, 0});
  D[0] = 0;
  // print(D);
  // exit(0);

  while (!que.empty()) {
    auto op = que.front();
    que.pop();
    auto n = op.first;
    auto d = op.second;
    // D[n] = d;
    auto nb = AL[n];

    for (auto nn : nb) {
      if (D[nn] <= d + 1)
        continue;
      D[nn] = d + 1;
      que.push({nn, d + 1});
      ans[nn] = n;
    }
  }
  // print(D);
  // print(ans);
  for (int i = 1; i < N; ++i) {
    if (ans[i] == -1) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] + 1 << endl;
}

signed main() {
  getInput();
  solve();
  return 0;
}

Error: TLE

Fixed submission:



#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
#define int int64_t

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int N, M;
vector<vector<int>> AL;
vector<int> D;
vector<int> ans;
const int INF = 1e9;

/* ======================== */
void getInput() {
  cin >> N >> M;
  AL = vector<vector<int>>(N, vector<int>());
  D = vector<int>(N, INF);
  ans = vector<int>(N, -1);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    AL[u].push_back(v);
    AL[v].push_back(u);
  }
}

void solve() {
  // auto nb = AL[0];
  // print(nb);
  // queue<int> que;
  queue<pair<int, int>> que;
  que.push({0, 0});
  D[0] = 0;
  // print(D);
  // exit(0);

  while (!que.empty()) {
    auto op = que.front();
    que.pop();
    auto n = op.first;
    auto d = op.second;
    // D[n] = d;
    auto nb = AL[n];

    for (auto nn : nb) {
      if (D[nn] <= d)
        continue;
      D[nn] = d + 1;
      que.push({nn, d + 1});
      ans[nn] = n;
    }
  }
  // print(D);
  // print(ans);
  for (int i = 1; i < N; ++i) {
    if (ans[i] == -1) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] + 1 << endl;
}

signed main() {
  getInput();
  solve();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define FORR(i, a, b) for (ll i = b - 1; i >= a; --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, 0, n)

// #define DEBUG_MODE

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

  vector<vector<int>> conn(N + 1, vector<int>(0));
  REP(i, M) {
    ll A, B;
    cin >> A >> B;
    conn[A].push_back(B);
    conn[B].push_back(A);
  }

#ifdef DEBUG_MODE
  FOR(i, 1, N + 1) {
    cout << i << ": ";
    REP(j, conn[i].size()) { cout << conn[i][j] << " "; }
    cout << endl;
  }
#endif

  vector<int> ans(N + 1, 0);
  queue<int> search_list;

  REP(i, conn[1].size()) {
    search_list.push(conn[1][i]);
    ans[conn[1][i]] = 1;
  }

  while (!search_list.empty()) {
    int n = search_list.front();
    search_list.pop();

    REP(i, conn[n].size()) {
      int m = conn[n][i];
      if (ans[m] != 0)
        continue;
      search_list.push(m);
      ans[m] = n;
    }

#ifdef DEBUG_MODE
    cout << "search for " << n << endl;
    FOR(i, 2, N + 1) { cout << i << ": " << ans[i] << endl; }
#endif
  }

  cout << "Yes" << endl;
  FOR(i, 2, N + 1) cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define FORR(i, a, b) for (ll i = b - 1; i >= a; --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, 0, n)

// #define DEBUG_MODE

int N, M;

void solve(vector<map<int, int>> &conn, queue<int> &search_list,
           vector<int> &ans) {
  if (search_list.empty())
    return;
  int n = search_list.front();
  search_list.pop();

#ifdef DEBUG_MODE
  cout << "search for " << n << endl;
  FOR(i, 2, N + 1) { cout << i << ": " << ans[i] << endl; }
#endif

  FOR(i, 1, N + 1) {
    if (conn[i][n]) {
      if (ans[i] != 0)
        continue;
      search_list.push(i);
      ans[i] = n;
    }
  }
  solve(conn, search_list, ans);
}

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

  vector<map<int, int>> conn(N + 1);
  REP(i, M) {
    ll A, B;
    cin >> A >> B;
    conn[A][B] = 1;
    conn[B][A] = 1;
  }

#ifdef DEBUG_MODE
  FOR(i, 1, N + 1) {
    cout << i << ": ";
    for (auto j = conn[i].begin(); j != conn[i].end(); ++j) {
      cout << j << " ";
    }
    cout << endl;
  }
#endif

  vector<int> ans(N + 1, 0);
  queue<int> search_list;

  REP(i, conn[1].size()) {
    search_list.push(conn[1][i]);
    ans[conn[1][i]] = 1;
  }

  solve(conn, search_list, ans);

  cout << "Yes" << endl;
  FOR(i, 2, N + 1) cout << ans[i] << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

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

int light[1000005];
vector<vector<int>> miti(1000005);

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

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    miti[a].push_back(b);
    miti[b].push_back(a);
  }

  deque<pair<int, int>> next;
  for (int x : miti[1]) {
    next.push_back(make_pair(x, 1));
  }
  do {
    deque<pair<int, int>> nn;
    for (pair<int, int> x : next) {
      if (light[x.first] == 0) {
        light[x.first] = x.second;
        for (int y : miti[x.first]) {
          if (light[y] == 0) {
            nn.push_back(make_pair(y, x.first));
          }
        }
      }
    }
    next = nn;
  } while (!next.empty());

  bool ok = true;
  for (int i = 2; i <= n; i++) {
    if (light[i] == 0)
      ok = false;
  }
  if (!ok) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << light[i] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

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

int light[1000005];
vector<vector<int>> miti(1000005);

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

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    miti[a].push_back(b);
    miti[b].push_back(a);
  }

  deque<pair<int, int>> next;
  for (int x : miti[1]) {
    next.push_back(make_pair(x, 1));
  }
  do {
    for (pair<int, int> x : next) {
      if (light[x.first] == 0) {
        light[x.first] = x.second;
        for (int y : miti[x.first]) {
          if (light[y] == 0) {
            next.push_back(make_pair(y, x.first));
          }
        }
      }
      next.pop_front();
    }
  } while (!next.empty());

  bool ok = true;
  for (int i = 2; i <= n; i++) {
    if (light[i] == 0)
      ok = false;
  }
  if (!ok) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << light[i] << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cmath>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <thread> ///////////////////
#include <tuple>

#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define rem(i, m, n) for (int i = (m); i > (n); --i)

#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long int;
using std::swap;

vector<int> v;
vector<int> w;
vector<long long int> vll;
vector<long long int> wll;
// vector<vector<int>> vec(50, vector<int>(4, 0));
vector<vector<int>> vv;
vector<vector<int>> ww;

int N, M;

int main() {

  cin >> N >> M;
  vv.resize(N + 1);
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    vv[a].push_back(b);
    vv[b].push_back(a);
  }

  vector<int> dist(N + 1, -1);
  vector<int> ans(N + 1, -1);
  queue<int> que;

  dist[1] = 0;
  que.push(1);
  ans[0] = 0;

  while (!que.empty()) {
    int x = que.front(); // キューから先頭頂点を取り出す
    que.pop();

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

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

  if (binary_search(ans.begin(), ans.end(), -1)) {
    cout << "No" << '\\n';
    return 0;
  }
  cout << "Yes" << '\\n';
  rep(i, 2, N + 1) { cout << ans[i] << '\\n'; }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <thread> ///////////////////
#include <tuple>

#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define rem(i, m, n) for (int i = (m); i > (n); --i)

#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long int;
using std::swap;

vector<int> v;
vector<int> w;
vector<long long int> vll;
vector<long long int> wll;
// vector<vector<int>> vec(50, vector<int>(4, 0));
vector<vector<int>> vv;
vector<vector<int>> ww;

int N, M;
int setr(int x) {
  rep(i, 1, N + 1) {
    if (x == i)
      continue;
    if (vv[x][i] != 0) {
      if (ww[x][1] + 1 < ww[i][1]) {
        ww[i][0] = x;
        ww[i][1] = ww[x][1] + 1;
        setr(i);
      }
    }
  }
  return 0;
}

int main() {

  cin >> N >> M;
  vv.resize(N + 1, vector<int>(N + 1, 0));
  ww.resize(N + 1, vector<int>(2, 1000000));

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    vv[a][b] = b;
    vv[b][a] = a;
  }
  ww[1][0] = 0;
  ww[1][1] = 0;
  setr(1);

  cout << "Yes" << '\\n';
  rep(i, 2, N + 1) if (ww[i][1] == 1000000) {
    cout << "No" << '\\n';
    return 0;
  }
  rep(i, 2, N + 1) { cout << ww[i][0] << '\\n'; }
  return 0;
}