prithivMLmods/Coder-Stat huggingface dataset:
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
typedef long long ll;

struct Edge {
  int to, next;
} e[N];
int n, m, cnt, first[N], s[N], h, r, q[N << 2];

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cout << "Yes" << endl;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  q[h = r = 1] = 1;
  while (h <= r) {
    int u = q[h];
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (!s[v]) {
        s[v] = u;
        q[++r] = v;
      }
    }
    h++;
  }
  for (int i = 2; i <= n; i++)
    cout << s[i] << endl;
  return 0;
}

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

const int N = 4e5 + 5;
typedef long long ll;

struct Edge {
  int to, next;
} e[N];
int n, m, cnt, first[N], s[N], h, r, q[N << 2];

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cout << "Yes" << endl;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  q[h = r = 1] = 1;
  while (h <= r) {
    int u = q[h];
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (!s[v]) {
        s[v] = u;
        q[++r] = v;
      }
    }
    h++;
  }
  for (int i = 2; i <= n; i++)
    cout << s[i] << endl;
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> G(m + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> ans(n + 1, -1);
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  while (q.empty() != true) {
    int v = q.front();
    q.pop();
    for (int x : G[v]) {
      if (ans[x] == -1) {
        q.push(x);
        ans[x] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> ans(n + 1, -1);
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  while (q.empty() != true) {
    int v = q.front();
    q.pop();
    for (int x : G[v]) {
      if (ans[x] == -1) {
        q.push(x);
        ans[x] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); ++i)

int main() {
  cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> C(M);
  for (int i = 0; i < M; ++i) {
    int A, B;
    std::cin >> A >> B;
    A--;
    B--;
    C[A].emplace_back(B);
    C[B].emplace_back(A);
  }

  std::vector<int> next(N, -1);
  std::vector<int> flag(N, 0);

  std::queue<int> q;
  q.push(0);
  next[0] = 0;
  while (!q.empty()) {
    int const index = q.front();
    q.pop();
    if (flag[index])
      continue;
    flag[index] = 1;
    for (auto const n : C[index]) {
      if (next[n] == -1) {
        next[n] = index;
        q.push(n);
      }
    }
  }
  for (auto const n : next) {
    if (n == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::cout << "Yes\n";
  for (int i = 1; i < N; ++i) {
    std::cout << next[i] + 1 << "\n";
  }
  std::cout << std::flush;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); ++i)

int main() {
  cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> C(N);
  for (int i = 0; i < M; ++i) {
    int A, B;
    std::cin >> A >> B;
    A--;
    B--;
    C[A].emplace_back(B);
    C[B].emplace_back(A);
  }

  std::vector<int> next(N, -1);
  std::vector<int> flag(N, 0);

  std::queue<int> q;
  q.push(0);
  next[0] = 0;
  while (!q.empty()) {
    int const index = q.front();
    q.pop();
    if (flag[index])
      continue;
    flag[index] = 1;
    for (auto const n : C[index]) {
      if (next[n] == -1) {
        next[n] = index;
        q.push(n);
      }
    }
  }
  for (auto const n : next) {
    if (n == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::cout << "Yes\n";
  for (int i = 1; i < N; ++i) {
    std::cout << next[i] + 1 << "\n";
  }
  std::cout << std::flush;

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: 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>;

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

  const int INF = 1e9;
  vector<int> dist(n, INF);
  vector<int> parent(n);
  queue<int> q;
  q.push(0);
  dist[0];
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (int t : to[from]) {
      if (dist[t] != INF)
        continue;
      dist[t] = dist[from] + 1;
      q.push(t);
      parent[t] = from;
    }
  }
  for (int d : dist) {
    if (d == INF) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++)
    cout << parent[i] + 1 << endl;
  return 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<int, int>;

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

  const int INF = 1e9;
  vector<int> dist(n, INF);
  vector<int> parent(n);
  queue<int> q;
  q.push(0);
  dist[0];
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (int t : to[from]) {
      if (dist[t] != INF)
        continue;
      dist[t] = dist[from] + 1;
      q.push(t);
      parent[t] = from;
    }
  }
  for (int d : dist) {
    if (d == INF) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++)
    cout << parent[i] + 1 << endl;
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

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

  vector<int> dist(n, inf);
  vector<int> pre(n);
  queue<int> q;
  dist.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist.at(u) != inf)
        continue;
      pre.at(u) = v;
      dist.at(u) = dist.at(v) + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre.at(i) + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

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

  vector<int> dist(n, inf);
  vector<int> pre(n);
  queue<int> q;
  dist.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist.at(u) != inf)
        continue;
      pre.at(u) = v;
      dist.at(u) = dist.at(v) + 1;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre.at(i) + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 100;

vector<int> G[MAX];
int ans[MAX];

void bfs(int s) {
  rep(i, 0, MAX) ans[i] = -1;
  queue<int> q;
  ans[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (ans[v] == -1) {
        ans[v] = u;
        q.push(v);
      }
    }
  }
}

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

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  bfs(1);

  puts("Yes");
  rep(i, 2, n + 1) { printf("%d\n", ans[i]); }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 100005;

vector<int> G[MAX];
int ans[MAX];

void bfs(int s) {
  rep(i, 0, MAX) ans[i] = -1;
  queue<int> q;
  ans[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (ans[v] == -1) {
        ans[v] = u;
        q.push(v);
      }
    }
  }
}

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

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  bfs(1);

  puts("Yes");
  rep(i, 2, n + 1) { printf("%d\n", ans[i]); }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Map(N);
  vector<int> Ans(N - 1, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    Map[A - 1].push_back(B - 1);
    Map[B - 1].push_back(A - 1);
  }
  queue<pair<int, int>> Q;
  Q.push({0, -1});
  while (!Q.empty()) {
    auto p = Q.front();
    int v = p.first, before = p.second;
    Q.pop();
    if (v > 0) {
      if (Ans[v - 1] >= 0)
        continue;
      Ans[v - 1] = before;
    }
    for (int u : Map[v])
      Q.push({u, v});
  }

  bool OK = true;
  for (int a : Ans)
    if (a < 0)
      OK = false;
  if (!OK)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int a : Ans)
      cout << a + 1 << endl;
  }
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> Map(N);
  vector<int> Ans(N - 1, -1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    Map[A - 1].push_back(B - 1);
    Map[B - 1].push_back(A - 1);
  }
  queue<pair<int, int>> Q;
  Q.push({0, -1});
  while (!Q.empty()) {
    auto p = Q.front();
    int v = p.first, before = p.second;
    Q.pop();
    if (v > 0) {
      if (Ans[v - 1] >= 0)
        continue;
      Ans[v - 1] = before;
    }
    for (int u : Map[v])
      if (Ans[u - 1] < 0)
        Q.push({u, v});
  }

  bool OK = true;
  for (int a : Ans)
    if (a < 0)
      OK = false;
  if (!OK)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int a : Ans)
      cout << a + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> r(m + 1);
  vector<int> answer(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r.at(a).push_back(b);
    r.at(b).push_back(a);
  }
  vector<bool> checked(n + 1, false);
  queue<int> q;
  q.push(1);
  checked.at(1) = true;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : r.at(t)) {
      if (!checked.at(i)) {
        answer.at(i) = t;
        checked.at(i) = true;
        q.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << answer.at(i) << endl;
  }
}


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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> r(n + 1);
  vector<int> answer(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    r.at(a).push_back(b);
    r.at(b).push_back(a);
  }
  vector<bool> checked(n + 1, false);
  queue<int> q;
  q.push(1);
  checked.at(1) = true;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : r.at(t)) {
      if (!checked.at(i)) {
        answer.at(i) = t;
        checked.at(i) = true;
        q.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << answer.at(i) << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: 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[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;
  }
}


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[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;
  }
}
Problem p02678 (C++)

Buggy submission (status: 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;

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

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;

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;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#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");
}

Fixed submission:
#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");
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5000;
int tot, head[maxn];
struct E {
  int to, next;
} edge[maxn << 1];
void add(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int n, m, ans[maxn], cnt = 0;
void bfs(int x) {
  ans[x] = 1;
  cnt += 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (!ans[v]) {
        cnt += 1;
        ans[v] = now;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  mem(head, -1);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs(1);
  if (cnt != n) {
    puts("No");
    return 0;
  }
  puts("Yes");
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int tot, head[maxn];
struct E {
  int to, next;
} edge[maxn << 1];
void add(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int n, m, ans[maxn], cnt = 0;
void bfs(int x) {
  ans[x] = 1;
  cnt += 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (!ans[v]) {
        cnt += 1;
        ans[v] = now;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  mem(head, -1);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs(1);
  if (cnt != n) {
    puts("No");
    return 0;
  }
  puts("Yes");
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#include <algorithm>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#include <cmath>
#include <iomanip>
#include <queue>
#include <set>
const int MOD = 1000000007;

ll POWER(ll x, ll y) {
  ll ret = 1;
  ll p = x;
  for (ll i = y; i > 0; i /= 2) {
    if (i % 2)
      ret = (ret * p) % MOD;
    p = (p * p) % MOD;
  }
  return ret;
}
ll gcm(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> c(n + 1, 0), d(n + 1);

  vector<set<int>> ab(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ab[a].insert(b);
    ab[b].insert(a);
  }
  c[1] = 1;
  int rest = n - 1;
  queue<int> q;
  q.push(1);

  while (!q.empty()) {

    int a = q.front();

    auto it = ab[a].begin();
    while (it != ab[a].end()) {
      if (c[*it] == 0) {
        c[*it] = c[a] + 1;
        d[*it] = a;
        q.push(*it);
      }
      it++;
    }
  }

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


Fixed submission:
#include <iostream>
using namespace std;
#include <algorithm>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#include <cmath>
#include <iomanip>
#include <queue>
#include <set>
const int MOD = 1000000007;

ll POWER(ll x, ll y) {
  ll ret = 1;
  ll p = x;
  for (ll i = y; i > 0; i /= 2) {
    if (i % 2)
      ret = (ret * p) % MOD;
    p = (p * p) % MOD;
  }
  return ret;
}
ll gcm(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> c(n + 1, 0), d(n + 1);

  vector<set<int>> ab(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ab[a].insert(b);
    ab[b].insert(a);
  }
  c[1] = 1;
  int rest = n - 1;
  queue<int> q;
  q.push(1);

  while (!q.empty()) {

    int a = q.front();
    q.pop();
    auto it = ab[a].begin();
    while (it != ab[a].end()) {
      if (c[*it] == 0) {
        c[*it] = c[a] + 1;
        d[*it] = a;
        q.push(*it);
      }
      it++;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << d[i] << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {

  int n, m;
  cin >> n >> m;
  Graph G(m);

  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> dist(n, -1);
  queue<int> que;
  vector<int> pre(n, -1);
  dist[0] = 0;
  que.push(0);

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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> dist(n, -1);
  queue<int> que;
  vector<int> pre(n, -1);
  dist[0] = 0;
  que.push(0);

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

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int u, v, n, m, flag, cnt;
int fa[MAXN], head[MAXN];
struct edge {
  int v, next;
} e[MAXN];
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v;
      if (fa[v])
        continue;
      fa[v] = u;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs();
  for (int i = 2; i <= n; ++i)
    if (!fa[i]) {
      printf("No");
      flag = 1;
      break;
    }
  if (!flag) {
    printf("Yes\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", fa[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int u, v, n, m, flag, cnt;
int fa[MAXN], head[MAXN];
struct edge {
  int v, next;
} e[MAXN << 1];
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v;
      if (fa[v])
        continue;
      fa[v] = u;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  bfs();
  for (int i = 2; i <= n; ++i)
    if (!fa[i]) {
      printf("No");
      flag = 1;
      break;
    }
  if (!flag) {
    printf("Yes\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", fa[i]);
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct Edge {
  int next, to;
} edge[N];
int fi[N], se, pre[N], que[N], s, t;
inline void add_edge(int u, int v) { edge[++se] = {fi[u], v}, fi[u] = se; }
inline void add(int u, int v) { add_edge(u, v), add_edge(v, u); }
int main() {
  int n, m, u, v;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &u, &v), add(u, v);
  que[t++] = 1;
  while (s < t) {
    int u = que[s++];
    for (int i = fi[u]; i; i = edge[i].next) {
      int v = edge[i].to;
      if (pre[v])
        continue;
      pre[v] = u, que[t++] = v;
    }
  }
  if (s < n)
    return puts("No"), 0;
  puts("Yes");
  for (int i = 2; i <= n; i++)
    printf("%d\n", pre[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct Edge {
  int next, to;
} edge[N * 2];
int fi[N], se, pre[N], que[N], s, t;
inline void add_edge(int u, int v) { edge[++se] = {fi[u], v}, fi[u] = se; }
inline void add(int u, int v) { add_edge(u, v), add_edge(v, u); }
int main() {
  int n, m, u, v;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &u, &v), add(u, v);
  que[t++] = 1;
  while (s < t) {
    int u = que[s++];
    for (int i = fi[u]; i; i = edge[i].next) {
      int v = edge[i].to;
      if (pre[v])
        continue;
      pre[v] = u, que[t++] = v;
    }
  }
  if (s < n)
    return puts("No"), 0;
  puts("Yes");
  for (int i = 2; i <= n; i++)
    printf("%d\n", pre[i]);
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;

int vis[maxn], pre[maxn];
int head[maxn], tot;

struct Edge {
  int to, next;
} e[maxn << 1];

void addedge(int from, int to) {
  e[tot].to = to;
  e[tot].next = head[from];
  head[from] = tot++;
}

void bfs(int u) {
  memset(vis, 0, sizeof(vis));
  vis[u] = 1;
  queue<int> q;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (!vis[v]) {
        vis[v] = 1;
        pre[v] = u;
        q.push(v);
      }
    }
  }
}

int main() {
  memset(head, -1, sizeof(head));
  tot = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    addedge(from, to);
    addedge(to, from);
  }
  bfs(1);
  bool judge = true;
  for (int i = 2; i <= n; i++)
    if (!vis[i]) {
      judge = false;
      break;
    }
  if (judge) {
    puts("Yes");
    for (int i = 2; i <= n; i++)
      cout << pre[i] << endl;
  } else
    puts("No");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;

int vis[maxn], pre[maxn];
int head[maxn], tot;

struct Edge {
  int to, next;
} e[maxn << 1];

void addedge(int from, int to) {
  e[tot].to = to;
  e[tot].next = head[from];
  head[from] = tot++;
}

void bfs(int u) {
  memset(vis, 0, sizeof(vis));
  vis[u] = 1;
  queue<int> q;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (!vis[v]) {
        vis[v] = 1;
        pre[v] = u;
        q.push(v);
      }
    }
  }
}

int main() {
  memset(head, -1, sizeof(head));
  tot = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    addedge(from, to);
    addedge(to, from);
  }
  bfs(1);
  bool judge = true;
  for (int i = 2; i <= n; i++)
    if (!vis[i]) {
      judge = false;
      break;
    }
  if (judge) {
    puts("Yes");
    for (int i = 2; i <= n; i++)
      cout << pre[i] << endl;
  } else
    puts("No");
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi 3.14159265358979323846

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

  queue<int> que;
  que.push(0);
  vector<bool> visit(n, false);
  vector<int> key(n, -1); // 道しるべ

  while (que.size() != 0) {
    int now = que.front();
    que.pop();

    visit[now] = true;

    for (auto next : G[now]) {
      if (visit[next] == true)
        continue;

      if (key[next] == -1) {
        key[next] = now;
      }
      que.push(next);
    }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi 3.14159265358979323846

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

  queue<int> que;
  que.push(0);
  vector<bool> visit(n, false);
  vector<int> key(n, -1); // 道しるべ

  while (que.size() != 0) {
    int now = que.front();
    que.pop();
    if (visit[now] == true)
      continue;

    visit[now] = true;

    for (auto next : G[now]) {
      if (visit[next] == true)
        continue;

      if (key[next] == -1) {
        key[next] = now;
      }
      que.push(next);
    }
  }

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

  return 0;
}
Problem p02679 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define TIMER_
#include <bits/stdc++.h>
using namespace std;
#define rep(i, r) for (int i = 0; i < (r); i++)
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #define int long long
using ll = long long;
using ull = unsigned long long;
const long long mod = 1000000007;
long long pw[200200] = {1};

signed main() {
  fastio;
  map<pair<ll, ll>, pair<ll, ll>> chk;

  for (int i = 1; i < 200200; i++)
    pw[i] = (pw[i - 1] * 2) % mod;

  ll z = mod - 1;
  ll n;
  scanf("%d", &n);
  while (n--) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      z++;
      continue;
    } else if (a == 0) {
      chk[make_pair(0, 1)].first++;
    } else if (b == 0) {
      chk[make_pair(0, 1)].second++;
    } else {
      ll g = __gcd(abs(a), abs(b));
      a /= g;
      b /= g;
      if (a * b > 0) {
        chk[make_pair(abs(a), abs(b))].first++;
      } else {
        chk[make_pair(abs(b), abs(a))].second++;
      }
    }
  }

  ll r = 1;
  for (auto &p : chk) {
    r = r * (pw[p.second.first] + pw[p.second.second] - 1) % mod;
  }

  cout << (z + r) % mod << "\n";

#ifdef TIMER_
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


Fixed submission:
// #define TIMER_
#include <bits/stdc++.h>
using namespace std;
#define rep(i, r) for (int i = 0; i < (r); i++)
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #define int long long
using ll = long long;
using ull = unsigned long long;
const long long mod = 1000000007;
long long pw[200200] = {1};

signed main() {
  fastio;
  map<pair<ll, ll>, pair<ll, ll>> chk;

  for (int i = 1; i < 200200; i++)
    pw[i] = (pw[i - 1] * 2) % mod;

  ll z = mod - 1;
  int n;
  cin >> n;
  while (n--) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      z++;
      continue;
    } else if (a == 0) {
      chk[make_pair(0, 1)].first++;
    } else if (b == 0) {
      chk[make_pair(0, 1)].second++;
    } else {
      ll g = __gcd(abs(a), abs(b));
      a /= g;
      b /= g;
      if (a * b > 0) {
        chk[make_pair(abs(a), abs(b))].first++;
      } else {
        chk[make_pair(abs(b), abs(a))].second++;
      }
    }
  }

  ll r = 1;
  for (auto &p : chk) {
    r = r * (pw[p.second.first] + pw[p.second.second] - 1) % mod;
  }

  cout << (z + r) % mod << "\n";

#ifdef TIMER_
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
Problem p02679 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

constexpr auto MOD = 1000000007LL;

// a^n mod m を計算する(繰り返し二乗法,二分累乗法:O(logn)) (再帰版)
long long modpow(long long a, long long n) {
  return n ? modpow(a * a % MOD, n / 2) * (n % 2 ? a : 1) % MOD : 1;
}

// // MOD考慮power関数
// inline long long modpow(long long a,long long n)
// {
// 	long long ret=1;
// 	for(long long i=0;i<n;i++){
// 		ret*=a;
// 		ret%=MOD;
// 	}
// 	return ret;
// }

// gcd関数
inline long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  long long zero = 0;
  map<pair<long long, long long>, vector<int>> bad;
  while (n--) {
    long long x, y;
    cin >> x >> y;

    // 両方0ならカウントしてcontinue
    if (x == 0 && y == 0) {
      zero++;
      continue;
    }

    // 既約分数計算
    long long g = gcd(x, y);
    x /= g;
    y /= g;

    // 符号情報を統一
    if (y < 0)
      x = -x, y = -y;
    if (y == 0 && x < 0)
      x = -x, y = -y;

    int rotate90 = (x <= 0);
    if (rotate90) {
      swap(x, y);
      y = -y;
    }

    if (bad.count({x, y}) == 0)
      bad[{x, y}] = {0, 0};
    bad[{x, y}][rotate90]++;
  }

  // 組み合わせ計算
  long long ans = 1;
  for (auto x : bad) {
    vector<int> c = x.second;
    ans *= (modpow(2, c[0]) - 1) + (modpow(2, c[1]) - 1) + 1;
    ans %= MOD;
  }

  ans += zero - 1;
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

constexpr auto MOD = 1000000007LL;

// a^n mod m を計算する(繰り返し二乗法,二分累乗法:O(logn)) (再帰版)
long long modpow(long long a, long long n) {
  return n ? modpow(a * a % MOD, n / 2) * (n % 2 ? a : 1) % MOD : 1;
}

// // MOD考慮power関数
// inline long long modpow(long long a,long long n)
// {
// 	long long ret=1;
// 	for(long long i=0;i<n;i++){
// 		ret*=a;
// 		ret%=MOD;
// 	}
// 	return ret;
// }

// gcd関数
// inline long long gcd(long long a, long long b)
// {
// 	if(a<b) swap(a,b);
// 	if(a%b==0) return b;
// 	else return gcd(b,a%b);
// }

int main() {
  int n;
  cin >> n;
  long long zero = 0;
  map<pair<long long, long long>, vector<int>> bad;
  while (n--) {
    long long x, y;
    cin >> x >> y;

    // 両方0ならカウントしてcontinue
    if (x == 0 && y == 0) {
      zero++;
      continue;
    }

    // 既約分数計算
    long long g = gcd(x, y);
    x /= g;
    y /= g;

    // 符号情報を統一
    if (y < 0)
      x = -x, y = -y;
    if (y == 0 && x < 0)
      x = -x, y = -y;

    int rotate90 = (x <= 0);
    if (rotate90) {
      swap(x, y);
      y = -y;
    }

    if (bad.count({x, y}) == 0)
      bad[{x, y}] = {0, 0};
    bad[{x, y}][rotate90]++;
  }

  // 組み合わせ計算
  long long ans = 1;
  for (auto x : bad) {
    vector<int> c = x.second;
    ans *= (modpow(2, c[0]) - 1) + (modpow(2, c[1]) - 1) + 1;
    ans %= MOD;
  }

  ans += zero - 1;
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
}
Problem p02679 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1000000007;

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main(int, char **) {
  int n;
  cin >> n;
  long long cnt0 = mod - 1;
  map<pair<long long, long long>, pair<long long, long long>> cnt;
  long long pw[202020] = {1};
  for (int i = 1; i < 202020; i++)
    pw[i] = (pw[i - 1] * 2) % mod;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      cnt0++;
      continue;
    }
    if (make_pair(a, b) < make_pair(0ll, 0ll))
      a = -a, b = -b;
    long long g = gcd(abs(a), abs(b));
    a /= g;
    b /= g;
    if (b > 0)
      cnt[{a, b}].first++;
    else
      cnt[{-b, a}].second++;
  }
  long long ret = 1;
  for (auto &c : cnt) {
    ret = ret * (pw[c.second.first] + pw[c.second.second] - 1) % mod;
  }
  cout << (ret + cnt0) % mod << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1000000007;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int main(int, char **) {
  int n;
  cin >> n;
  long long cnt0 = mod - 1;
  map<pair<long long, long long>, pair<long long, long long>> cnt;
  long long pw[202020] = {1};
  for (int i = 1; i < 202020; i++)
    pw[i] = (pw[i - 1] * 2) % mod;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      cnt0++;
      continue;
    }
    if (make_pair(a, b) < make_pair(0ll, 0ll))
      a = -a, b = -b;
    long long g = gcd(abs(a), abs(b));
    a /= g;
    b /= g;
    if (b > 0)
      cnt[{a, b}].first++;
    else
      cnt[{-b, a}].second++;
  }
  long long ret = 1;
  for (auto &c : cnt) {
    ret = ret * (pw[c.second.first] + pw[c.second.second] - 1) % mod;
  }
  cout << (ret + cnt0) % mod << endl;
  return 0;
}