alexjercan/bugnet huggingface dataset:
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);
  vector<int> B(M);

  vector<vector<int>> data(N + 1);

  for (int i = 0; i < M; i++) {
    cin >> A.at(i);
    cin >> B.at(i);
    data.at(A.at(i)).push_back(B.at(i));
    data.at(B.at(i)).push_back(A.at(i));
  }

  queue<int> q;

  vector<int> length(N + 1, 0);
  vector<int> to(N + 1, 1);

  vector<bool> flag(N + 1, false);

  length.at(1) = 1;
  to.at(1) = 0;

  q.push(1);

  while (1) {
    if (q.empty()) {
      break;
    }

    int now = q.front();
    q.pop();
    if (flag.at(now) == true) {
      continue;
    }

    flag.at(now) = true;

    for (int64_t j = 0; j < data.at(now).size(); j++) {
      int temp = data.at(now).at(j);
      if (flag.at(temp) == true) {
        continue;
      }

      q.push(temp);
      if (length.at(temp) == 0) {
        length.at(temp) = length.at(now) + 1;
        to.at(temp) = now;
      } else if (length.at(temp) >= length.at(now) + 1) {
        length.at(temp) = length.at(now) + 1;
        to.at(temp) = now;
      }
    }
  }

  cout << "Yes" << endl;
  for (int64_t i = 2; i <= N; i++) {
    cout << to.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);
  vector<int> B(M);

  vector<vector<int>> data(N + 1);

  for (int i = 0; i < M; i++) {
    cin >> A.at(i);
    cin >> B.at(i);
    data.at(A.at(i)).push_back(B.at(i));
    data.at(B.at(i)).push_back(A.at(i));
  }

  queue<int> q;

  vector<int> length(N + 1, 0);
  vector<int> to(N + 1, 1);

  vector<bool> flag(N + 1, false);

  length.at(1) = 1;
  to.at(1) = 0;

  q.push(1);

  while (1) {
    if (q.empty()) {
      break;
    }
    int now = q.front();
    q.pop();
    flag.at(now) = true;

    for (int64_t j = 0; j < data.at(now).size(); j++) {
      int temp = data.at(now).at(j);
      if (flag.at(temp) == true) {
        continue;
      }

      q.push(temp);
      if (length.at(temp) == 0) {
        length.at(temp) = length.at(now) + 1;
        to.at(temp) = now;
      } else if (length.at(temp) >= length.at(now) + 1) {
        length.at(temp) = length.at(now) + 1;
        to.at(temp) = now;
      }
    }
  }

  cout << "Yes" << endl;
  for (int64_t i = 2; i <= N; i++) {
    cout << to.at(i) << 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;
using ll = long long;
using P = pair<ll, ll>;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
void YesNo(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
void yesno(bool j) {
  cout << (j ? "yes" : "no") << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head << endl;
  return;
}

vector<int> to[200005];

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;

    to[v].push_back(u);
    to[u].push_back(v);
  }
  vector<int> c(n + 1, inf);
  queue<int> q;
  vector<int> ans(n + 1, -1);

  c[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int a = q.front();
    q.pop();

    for (int i : to[a]) {
      if (c[i] != inf)
        continue;
      c[i] = c[a] + 1;
      ans[i] = a;
      q.push(i);
    }
  }
  /* bool ok=true;
  for(int i=1 ;i<n+1;i++) {
      if(c[i]==inf) ok=false;
  }
  if(!ok) {
      pt("No");
  }
  else {
      pt("Yes");
      for(int i=2; i<=n; i++) {
          pt(ans[i]);
      }
  }*/
  pt("Yes");
  for (int i = 2; i <= n; i++) {
    pt(ans[i]);
  }
  return 0;
}


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<ll, ll>;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
void YesNo(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
void yesno(bool j) {
  cout << (j ? "yes" : "no") << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head << endl;
  return;
}

vector<int> to[20005];

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;

    to[v].push_back(u);
    to[u].push_back(v);
  }
  vector<int> c(n + 1, inf);
  queue<int> q;
  vector<int> ans(n + 1, -1);

  c[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int a = q.front();
    q.pop();

    for (int i : to[a]) {
      if (c[i] != inf)
        continue;
      c[i] = c[a] + 1;
      ans[i] = a;
      q.push(i);
    }
  }
  /* bool ok=true;
  for(int i=1 ;i<n+1;i++) {
      if(c[i]==inf) ok=false;
  }
  if(!ok) {
      pt("No");
  }
  else {
      pt("Yes");
      for(int i=2; i<=n; i++) {
          pt(ans[i]);
      }
  }*/
  pt("Yes");
  for (int i = 2; i <= n; i++) {
    pt(ans[i]);
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  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);
  }
  vector<int> prev(n);
  vector<int> dist(n);
  queue<int> que;

  // BPS
  int MAX_DIST = 10000000;
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      dist[i] = 0;
    } else {
      dist[i] = MAX_DIST;
    }
  }
  que.push(0);

  unordered_set<int> not_visited;
  for (int i = 0; i < n; i++) {
    not_visited.insert(i);
  }

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

    // visited
    if (not_visited.find(u) == not_visited.end()) {
      continue;
    }
    for (int v : to[u]) {
      int d = dist[u] + 1;
      if (dist[v] > d) {
        dist[v] = d;
        prev[v] = u;
        que.push(v);
      }
    }
    not_visited.erase(u);
    if (not_visited.size() == 0) {
      break;
    }
  }

  // for (int i = 0; i < n; i++) {
  //     if (dist[i] >= MAX_DIST) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      cout << "Yes" << endl;
    } else {
      cout << (prev[i] + 1) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  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);
  }
  vector<int> prev(n);
  vector<int> dist(n);
  // store distance and node
  priority_queue<pair<int, int>> que;

  // Dijkstra
  int MAX_DIST = 1000000;
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      dist[i] = 0;
    } else {
      dist[i] = MAX_DIST;
    }
  }
  que.push(make_pair(0, 0));

  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int u = p.second;

    // visited
    if (dist[u] < p.first) {
      continue;
    }
    for (int v : to[u]) {
      int d = dist[u] + 1;
      if (dist[v] > d) {
        dist[v] = d;
        prev[v] = u;
        que.push(make_pair(d, v));
      }
    }
  }

  // for (int i = 0; i < n; i++) {
  //     if (dist[i] >= MAX_DIST) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      cout << "Yes" << endl;
    } else {
      cout << (prev[i] + 1) << endl;
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// 行く先のindex, その前に訪問した頂点
typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(N);
  vector<int> arrows(N, -1);

  for (int i = 0; i < M; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    graph[ai].push_back(bi);
    graph[bi].push_back(ai);
  }
  queue<P> que;
  for (int i = 0; i < graph[0].size(); i++) {
    que.push(P(graph[0][i], 0));
  }
  map<int, bool> queued;

  while (que.size()) {
    P pr = que.front();
    que.pop();
    int next = pr.first;
    int last = pr.second;

    if (arrows[next] == -1)
      arrows[next] = last;
    for (int i = 0; i < graph[next].size(); i++) {
      if (arrows[graph[next][i]] == -1 and queued[graph[next][i]] == false) {
        que.push(P(graph[next][i], next));
      }
      queued[graph[next][i]] = true;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << arrows[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// 行く先のindex, その前に訪問した頂点
typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(N);
  vector<int> arrows(N, -1);

  for (int i = 0; i < M; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    graph[ai].push_back(bi);
    graph[bi].push_back(ai);
  }
  queue<P> que;
  for (int i = 0; i < graph[0].size(); i++) {
    que.push(P(graph[0][i], 0));
  }

  while (que.size()) {
    P pr = que.front();
    que.pop();
    int next = pr.first;
    int last = pr.second;

    if (arrows[next] == -1)
      arrows[next] = last;
    for (int i = 0; i < graph[next].size(); i++) {
      if (arrows[graph[next][i]] == -1)
        que.push(P(graph[next][i], next));
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << arrows[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// container util
#define SORT(c) sort((c).begin(), (c).end())
#define all(a) (a).begin(), (a).end()

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// print
#define pv(val) cerr << #val << '=' << (val) << endl

const int INF = 1e9;

int main(void) {

  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(N);

  REP(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    graph[A].push_back(B);
    graph[B].push_back(A);
  }

  vector<bool> was(N, false);
  vector<int> prev(N, -1);
  queue<int> q;
  was[0] = true;
  prev[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i : graph[v]) {
      if (was[i])
        continue;
      else if (!was[i]) {
        q.push(i);
        prev[i] = v + 1;
        was[i] = true;
      }
    }
  }

  bool flag = true;
  REP(i, N) if (!was[i]) flag = false;

  if (flag) {
    cout << "Yes" << endl;
    FOR(i, 1, N) cout << prev[i] << endl;

  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// container util
#define SORT(c) sort((c).begin(), (c).end())
#define all(a) (a).begin(), (a).end()

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// print
#define pv(val) cerr << #val << '=' << (val) << endl

const int INF = 1e9;

int main(void) {

  int N, M;
  cin >> N >> M;
  vector<vector<bool>> graph(N, vector<bool>(N, false));

  REP(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    graph[A][B] = true;
    graph[B][A] = true;
  }

  vector<bool> was(N, false);
  vector<int> prev(N, -1);
  queue<int> q;
  was[0] = true;
  prev[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    REP(i, N) {
      if (was[i])
        continue;
      else if (graph[v][i] and !was[i]) {
        q.push(i);
        prev[i] = v + 1;
        was[i] = true;
      }
    }
  }

  bool flag = true;
  REP(i, N) if (!was[i]) flag = false;

  if (flag) {
    cout << "Yes" << endl;
    FOR(i, 1, N) cout << prev[i] << endl;

  } else {
    cout << "No" << endl;
  }

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

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

  vector<int> dist(n, -1);
  queue<int> q;
  vector<int> pre(n, -1);

  dist[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : dp[v]) {
      if (dist[i] != -1)
        continue;
      dist[i] = dist[v] + 1;
      q.push(i);
      pre[i] = v;
    }
  }

  cout << "Yes" << '\\n';
  REP(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> dp(n);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    dp[a].push_back(b);
    dp[b].push_back(a);
  }

  vector<int> dist(n, -1);
  queue<int> q;
  vector<int> pre(n, -1);

  dist[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : dp[v]) {
      if (dist[i] != -1)
        continue;
      dist[i] = dist[v] + 1;
      q.push(i);
      pre[i] = v;
    }
  }

  cout << "Yes" << '\\n';
  REP(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << '\\n';
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
vector<int> G[100000];
int dist[100000], pre[100000];
signed main() {
  cout << "Yes" << endl;
  int n, m, a, b;
  cin >> n >> m;
  while (cin >> a >> b) {
    G[--a].push_back(--b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; i++)
    dist[i] = mod, pre[i] = -1;
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < G[v].size(); i++) {
      int nv = G[v][i];
      if (dist[nv] == mod) {
        que.push(nv);
        dist[nv] = dist[v] + 1;
        pre[nv] = v;
      }
    }
  }
  for (int i = 1; i < n; i++)
    cout << pre[i] + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
vector<int> G[100000];
int dist[100000], pre[100000];
void dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (dist[G[v][i]] > dist[v] + 1) {
      dist[G[v][i]] = dist[v] + 1;
      pre[G[v][i]] = v;
      dfs(G[v][i]);
    }
  }
}
signed main() {
  cout << "Yes" << endl;
  int n, m, a, b;
  cin >> n >> m;
  while (cin >> a >> b) {
    G[--a].push_back(--b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; i++)
    dist[i] = mod, pre[i] = -1;
  dist[0] = 0;
  dfs(0);
  for (int i = 1; i < n; i++)
    cout << pre[i] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

typedef long long ll;

#define debug(x) cerr << #x << ": " << x << "\\n";
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

  vector<vector<int>> G(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, -1);

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

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

  bool ok = true;
  rep(i, N - 1) if (ans[i + 1] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    rep(i, N - 1) cout << ans[i + 1] + 1 << endl;
  } else
    cout << "No" << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

typedef long long ll;

#define debug(x) cerr << #x << ": " << x << "\\n";
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

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

  vector<int> ans(N, -1);

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

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

  bool ok = true;
  rep(i, N - 1) if (ans[i + 1] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    rep(i, N - 1) cout << ans[i + 1] + 1 << endl;
  } else
    cout << "No" << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int dis[100010], ans[100010];
bool vis[100010];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    q;
int main() {
  int n, m, i, u, v;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  fill(dis, dis + n + 5, 2147483647);
  q.push(make_pair(0, 1));
  dis[1] = 0;
  while (q.size()) {
    int f = q.top().second;
    q.pop();
    if (vis[f])
      continue;
    vis[f] = true;
    for (i = 0; i < g[f].size(); i++)
      if (dis[g[f][i]] > dis[f] + 1) {
        dis[g[f][i]] = dis[f] + 1;
        q.push(make_pair(dis[f] + 1, g[f][i]));
        ans[g[f][i]] = f;
      }
  }
  for (i = 2; i <= n; i++)
    if (dis[i] == 2147483647) {
      cout << "No";
      return 0;
    }
  cout << "Yes\\n";
  for (i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int dep[100010], ans[100010];
queue<int> q;
int main() {
  int n, m, i, u, v;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  fill(dep, dep + n + 5, 2147483647);
  dep[1] = 1;
  for (i = 0; i < g[1].size(); i++)
    q.push(g[1][i]);
  while (q.size()) {
    u = q.front();
    q.pop();
    for (i = 0; i < g[u].size(); i++)
      if (dep[g[u][i]] == 2147483647)
        q.push(g[u][i]);
      else if (dep[g[u][i]] + 1 < dep[u]) {
        dep[u] = dep[g[u][i]] + 1;
        ans[u] = g[u][i];
      }
  }
  for (i = 2; i <= n; i++)
    if (dep[i] == 2147483647) {
      cout << "No";
      return 0;
    }
  cout << "Yes\\n";
  for (i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];        // 隣接リスト
  vector<int> sirusi(n, -1); // i番目の部屋の道しるべ。
  vector<bool> isVisit(n, false);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }

  // 幅優先探索
  queue<int> q;

  for (int i = 0; i < n; i++) {
    if (!isVisit[i]) {
      q.push(i);
      while (!q.empty()) {
        int v = q.front();
        q.pop();
        isVisit[v] = true;
        for (int j = 0; j < adj[v].size(); j++) {
          // cout << adj[v].size() << "サイズ" << endl;
          if (!isVisit[adj[v][j]]) {
            q.push(adj[v][j]);
            sirusi[adj[v][j]] = v + 1;
            isVisit[adj[v][j]] = true;
          }
        }
      }
    }
  }

  printf("Yes\\n");
  for (int i = 1; i < n; i++) {
    printf("%d\\n", sirusi[i]);
  }
}


Error: -11

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];        // 隣接リスト
  vector<int> sirusi(n, -1); // i番目の部屋の道しるべ。
  sirusi[0] = 0;

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  // 幅優先探索
  queue<int> q;
  q.push(0);
  int u;
  while (!q.empty()) {
    u = q.front(); // 今いる場所
    q.pop();
    for (int v = 0; v < adj[u].size(); v++) {
      if (sirusi[v] != -1)
        continue;
      sirusi[v] = u + 1;
      q.push(v);
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

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

int N, M;
map<pair<int, int>, int> mp;
vector<int> a[100000];
int mark[100000] = {0};
const int INF = 100000000;
int dist[100000];

int main() {
  cin >> N >> M;
  rep(i, N) { dist[i] = INF; }

  rep(i, M) {
    int x, y;
    cin >> x >> y;
    a[x - 1].push_back(y - 1);
    a[y - 1].push_back(x - 1);
    // mp[make_pair(x-1, y-1)] = 1;
    // mp[make_pair(y-1, x-1)] = 1;
  }

  dist[0] = 0;
  deque<int> que;
  que.push_back(0);

  mark[0] = 0;

  while (que.size()) {
    int p = que.front();
    que.pop_front();
    // for(int i = 0; i < N; i++) {
    for (auto v : a[p]) {
      if (dist[v] > dist[p] + 1) {
        // cout << dist[1] << endl;
        dist[v] = dist[p] + 1;
        mark[v] = p + 1;
        que.push_back(v);
      }
    }
    // }
  }

  // cout << dist[1] << endl;
  cout << "Yes" << endl;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

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

int N, M;
map<pair<int, int>, int> mp;
int mark[100000] = {0};
const int INF = 100000000;
int dist[100000];

int main() {
  cin >> N >> M;
  rep(i, N) { dist[i] = INF; }

  rep(i, M) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x - 1, y - 1)] = 1;
    mp[make_pair(y - 1, x - 1)] = 1;
  }

  dist[0] = 0;
  deque<int> que;
  que.push_back(0);

  mark[0] = 0;

  while (que.size()) {
    int p = que.front();
    que.pop_front();
    for (int i = 0; i < N; i++) {
      if (mp.count(make_pair(p, i)) || mp.count(make_pair(i, p))) {
        if (dist[i] > dist[p] + 1) {
          // cout << dist[1] << endl;
          dist[i] = dist[p] + 1;
          mark[i] = p + 1;
          que.push_back(i);
        }
      }
    }
  }

  // cout << dist[1] << endl;
  cout << "Yes" << endl;

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

  return 0;
}