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

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

int main() {

  int q, com, x, l, r;
  set<int> S;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> com;

    if (com == 0) {
      cin >> x;
      S.insert(x);
      cout << S.size() << endl;
    } else if (com == 1) {
      cin >> x;
      cout << S.count(x) << endl;
    } else if (com == 2) {
      cin >> x;
      S.erase(x);
    } else if (com == 3) {
      set<int>::iterator itl, itr;

      cin >> l >> r;
      itl = lower_bound(S.begin(), S.end(), l);
      itr = upper_bound(S.begin(), S.end(), r);

      for (set<int>::iterator it = itl; it != itr; it++)
        cout << *it << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

int main() {

  int q, com, x, l, r;
  set<int> S;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> com;

    if (com == 0) {
      cin >> x;
      S.insert(x);
      cout << S.size() << endl;
    } else if (com == 1) {
      cin >> x;
      cout << S.count(x) << endl;
    } else if (com == 2) {
      cin >> x;
      S.erase(x);
    } else if (com == 3) {
      set<int>::iterator itl, itr;

      cin >> l >> r;
      itl = S.lower_bound(l);
      itr = S.upper_bound(r);

      for (set<int>::iterator it = itl; it != itr; it++)
        cout << *it << endl;
    }
  }

  return 0;
}
Problem p02457 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
set<int> st;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int Q, k;
    cin >> Q >> k;
    if (Q == 0) {
      st.insert(k);
      cout << st.size() << endl;
    } else if (Q == 1) {
      cout << st.count(k) << endl;
    } else if (Q == 2) {
      st.erase(k);
    } else {
      int y;
      cin >> y;
      auto itr = st.lower_bound(k);
      while (*itr <= y) {
        cout << *itr << endl;
        itr++;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
set<int> st;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int Q, k;
    cin >> Q >> k;
    if (Q == 0) {
      st.insert(k);
      cout << st.size() << endl;
    } else if (Q == 1) {
      cout << st.count(k) << endl;
    } else if (Q == 2) {
      st.erase(k);
    } else {
      int y;
      cin >> y;
      auto nowitr = st.lower_bound(k);
      auto enditr = st.upper_bound(y);
      while (nowitr != enditr) {
        cout << *nowitr << endl;
        nowitr++;
      }
    }
  }
  return 0;
}
Problem p02460 (Python)

Buggy submission (status: Runtime Error):
dict = {}
q = int(input())
for i in range(q):
    query, *val = input().split(" ")
    if query == "0":
        dict[val[0]] = int(val[1])
    elif query == "1":
        print(dict.get(val[0], 0))
    else:
        dict.pop(val[0])


Fixed submission:
dict = {}
q = int(input())
for i in range(q):
    query, *val = input().split(" ")
    if query == "0":
        dict[val[0]] = int(val[1])
    elif query == "1":
        print(dict.get(val[0], 0))
    else:
        if val[0] in dict:
            dict.pop(val[0])
Problem p02462 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
multimap<string, int> mp;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int Q;
    cin >> Q;
    string s, l, r;
    int x;
    if (Q == 0) {
      cin >> s >> x;
      mp.insert(make_pair(s, x));
    }
    if (Q == 1) {
      cin >> s;
      auto now = mp.lower_bound(s);
      while (now->first == s && now != mp.end()) {
        cout << now->second << endl;
        ++now;
      }
    }
    if (Q == 2) {
      cin >> s;
      mp.erase(s);
    }
    if (Q == 3) {
      cin >> l >> r;
      auto now = mp.lower_bound(l);
      auto end = mp.upper_bound(r);
      assert(l <= r);
      while (now != end) {
        cout << now->first << " " << now->second << endl;
        ++now;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
multimap<string, int> mp;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int Q;
    cin >> Q;
    string s, l, r;
    int x;
    if (Q == 0) {
      cin >> s >> x;
      mp.insert(make_pair(s, x));
    }
    if (Q == 1) {
      cin >> s;
      for (auto it = mp.lower_bound(s); it != mp.end() && it->first == s;
           it++) {
        cout << it->second << endl;
      }
    }
    if (Q == 2) {
      cin >> s;
      mp.erase(s);
    }
    if (Q == 3) {
      cin >> l >> r;
      auto now = mp.lower_bound(l);
      auto end = mp.upper_bound(r);
      assert(l <= r);
      while (now != end) {
        cout << now->first << " " << now->second << endl;
        ++now;
      }
    }
  }
  return 0;
}
Problem p02462 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
using namespace std;

signed main() {
  multimap<string, int> map;
  int q;
  cin >> q;
  while (q--) {
    int y;
    string k;
    cin >> y >> k;
    if (y == 0) {
      int x;
      cin >> x;
      map.insert(pair<string, int>(k, x));
    } else if (y == 1) {
      for (auto it = map.find(k); it->first == k; it++) {
        cout << it->second << endl;
      }
    } else if (y == 2) {
      map.erase(k);
    } else {
      string z;
      cin >> z;
      for (auto it = map.lower_bound(k); it != map.upper_bound(z); it++) {
        cout << it->first << " " << it->second << endl;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
using namespace std;

signed main() {
  multimap<string, int> map;
  int q;
  cin >> q;
  while (q--) {
    int y;
    string k;
    cin >> y >> k;
    if (y == 0) {
      int x;
      cin >> x;
      map.insert(pair<string, int>(k, x));
    } else if (y == 1) {
      for (auto it = map.find(k); it != map.end() && it->first == k; it++) {
        cout << it->second << endl;
      }
    } else if (y == 2) {
      map.erase(k);
    } else {
      string z;
      cin >> z;
      for (auto it = map.lower_bound(k); it != map.upper_bound(z); it++) {
        cout << it->first << " " << it->second << endl;
      }
    }
  }
  return 0;
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
bool isPrime(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  cin >> n;
  vector<int> res;
  int temp = n;
  for (int i = 2; i <= n; i++) {
    if (isPrime(i))
      while (temp % i == 0) {
        res.push_back(i);
        temp /= i;
      }
    if (temp == 1)
      break;
  }
  cout << n << ":";
  for (int i = 0; i < res.size(); i++) {
    cout << " ";
    cout << res[i];
  }
  cout << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
bool isPrime(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  cin >> n;
  vector<int> res;
  int temp = n;
  if (isPrime(n))
    res.push_back(n);
  else {
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        while (temp % i == 0) {
          res.push_back(i);
          temp /= i;
        }
      if (temp == 1)
        break;
    }
  }
  cout << n << ":";
  for (int i = 0; i < res.size(); i++) {
    cout << " ";
    cout << res[i];
  }
  cout << endl;
}
Problem p02467 (C++)

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

int main() {
  long long int N;
  cin >> N;
  cout << N << ":";
  if (N <= 1) {
    cout << " " << N << endl;
    return 0;
  }
  vector<bool> x(N, true);
  long long int M = sqrtl(N) + 1;
  for (size_t i = 2; i <= M; i++) {
    while (N % i == 0) {
      cout << " " << i;
      N /= i;
    }
  }
  if (N != 1) {
    cout << " " << N;
  }
  cout << endl;
  return 0;
}

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

int main() {
  long long int N;
  cin >> N;
  cout << N << ":";
  if (N <= 1) {
    cout << " " << N << endl;
    return 0;
  }
  long long int M = sqrtl(N) + 1;
  for (size_t i = 2; i <= M; i++) {
    while (N % i == 0) {
      cout << " " << i;
      N /= i;
    }
  }
  if (N != 1) {
    cout << " " << N;
  }
  cout << endl;
  return 0;
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int nn = n;
  cout << n << ":";
  bool cnt = true;
  while (1) {
    if (n % 2 == 0) {
      cout << " " << 2;
      n /= 2;
      cnt = false;
    } else
      break;
  }
  while (1) {
    if (n % 3 == 0) {
      cout << " " << 3;
      n /= 3;
      cnt = false;
    } else
      break;
  }
  for (int i = 3; i <= nn; i += 2) {
    bool gouseisuu = false;
    for (int j = 3; j <= i; j += 2) {
      if (i % j == 0) {
        gouseisuu = true;
        break;
      }
      if (!gouseisuu) {
        while (1) {
          if (n % i == 0) {
            cout << " " << i;
            n /= i;
            cnt = false;
          } else
            break;
        }
      }
    }
    if (n == 1)
      break;
  }
  if (cnt)
    cout << " " << nn;
  cout << endl;
}

Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int nn = n;
  cout << n << ":";
  bool cnt = true;
  while (1) {
    if (n % 2 == 0) {
      cout << " " << 2;
      n /= 2;
      cnt = false;
    } else
      break;
  }
  while (1) {
    if (n % 3 == 0) {
      cout << " " << 3;
      n /= 3;
      cnt = false;
    } else
      break;
  }
  for (int i = 3; i <= nn; i += 2) {
    bool gouseisuu = false;
    for (int j = 3; j <= i; j += 2) {
      if (i % j == 0) {
        gouseisuu = true;
        break;
      }
      if (!gouseisuu) {
        while (1) {
          if (n % i == 0) {
            cout << " " << i;
            n /= i;
            cnt = false;
          } else
            break;
        }
      }
    }
    if (n == 1)
      break;
    if (n == nn && cnt == true && i >= sqrt(n))
      break;
  }
  if (cnt)
    cout << " " << nn;
  cout << endl;
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#define MAX_N 100001
using namespace std;

bool isPrime(int n) {
  if (n == 0 || n == 1)
    return false;
  if (n == 2 || n == 3)
    return true;
  if (n % 2 == 0)
    return false;
  for (int i = 5; i <= sqrt(n); i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int nc = n;
  cout << n << ':';
  for (int i = 2; i <= nc; i++) {
    if (isPrime(i) == false)
      continue;
    if (nc % i)
      continue;
    while (nc % i == 0) {
      cout << ' ' << i;
      nc /= i;
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#define MAX_N 100001
using namespace std;

bool isPrime(int n) {
  if (n == 0 || n == 1)
    return false;
  if (n == 2 || n == 3)
    return true;
  if (n % 2 == 0)
    return false;
  for (int i = 5; i <= sqrt(n); i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int nc = n;
  cout << n << ':';
  if (isPrime(n) == true) {
    cout << ' ' << n << endl;
    return 0;
  }
  for (int i = 2; i <= nc; i++) {
    if (isPrime(i) == false)
      continue;
    if (nc % i)
      continue;
    while (nc % i == 0) {
      cout << ' ' << i;
      nc /= i;
    }
  }
  cout << endl;

  return 0;
}
Problem p02467 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int n, m;
  vector<int> v;
  cin >> n;
  m = n;
  for (int i = 2; i <= sqrt(n); i++) {
    while (m % i == 0) {
      v.push_back(i);
      m /= i;
    }
  }
  if (m >= v.back())
    v.push_back(m);
  cout << n << ':';
  for (int i : v)
    cout << ' ' << i;
  cout << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int n, m;
  vector<int> v;
  cin >> n;
  m = n;
  for (int i = 2; i <= sqrt(n); i++) {
    while (m % i == 0) {
      v.push_back(i);
      m /= i;
    }
  }
  if (m != 1)
    v.push_back(m);
  cout << n << ':';
  for (int i : v)
    cout << ' ' << i;
  cout << endl;
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int a;
  cin >> a;

  cout << a << ":";

  vector<int> r;
  int j = 2;
  while (true) {
    if (a == 1) {
      break;
    }
    if (a % j == 0) {
      r.push_back(j);
      a /= j;
    } else {
      j += 1;
    }
  }
  for (int i : r) {
    cout << ' ' << i;
  }
  cout << endl;
}

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

using namespace std;

int main() {
  int a;
  cin >> a;

  cout << a << ":";

  vector<int> r;
  int j = 2;
  while (true) {
    if (a == 1) {
      break;
    }
    if (a % j == 0) {
      r.push_back(j);
      a /= j;
    } else {
      j += 1;
    }
    if (j * j > a) {
      r.push_back(a);
      break;
    }
  }
  for (int i : r) {
    cout << ' ' << i;
  }
  cout << endl;
}
Problem p02467 (C++)

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

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<int> factorize;
  cout << n << ": ";
  int input = n;
  for (int i = 2; i <= sqrt((double)input); ++i) {
    while (n % i == 0) {
      n /= i;
      factorize.push_back(i);
    }
  }
  for (int i = 0; i < factorize.size() - 1; ++i) {
    cout << factorize[i] << " ";
  }
  cout << factorize[factorize.size() - 1] << endl;
}

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

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

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<int> factorize;
  cout << n << ": ";
  int input = n;
  for (int i = 2; i <= sqrt((double)input); ++i) {
    while (n % i == 0) {
      n /= i;
      factorize.push_back(i);
    }
  }
  if (n != 1) {
    factorize.push_back(n);
  }
  for (int i = 0; i < factorize.size() - 1; ++i) {
    cout << factorize[i] << " ";
  }
  cout << factorize[factorize.size() - 1] << endl;
}

int main() {
  solve();
  return (0);
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define CIN(in)                                                                \
  int in;                                                                      \
  cin >> in;
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  CIN(in);
  cout << in << ":";
  loop(i, 2, 1e9 + 10) {
    while (in % i == 0) {
      cout << " " << i;
      in /= i;
    }
    if (in == 1)
      break;
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define CIN(in)                                                                \
  int in;                                                                      \
  cin >> in;
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  CIN(in);
  cout << in << ":";
  loop(i, 2, 1e9 + 10) {
    if (i * i > in) {
      cout << " " << in;
      break;
    }
    while (in % i == 0) {
      cout << " " << i;
      in /= i;
    }
    if (in == 1)
      break;
  }
  cout << endl;
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  int n;
  int dp[100001];
  int pos = 1;

  cin >> n;
  cout << n << ":";
  FOR(i, 2, n) {
    while (n % i == 0) {
      dp[pos] = i;
      pos++;
      n /= i;
    }
  }
  FOR(i, 1, pos - 1) { cout << " " << dp[i]; }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  int n;
  int dp[100001];
  int pos = 1;

  cin >> n;
  cout << n << ":";
  FOR(i, 2, n) {
    if (pos == 1 && i > sqrt(n)) {
      dp[pos] = n;
      pos++;
      n = 1;
    }
    while (n % i == 0) {
      dp[pos] = i;
      pos++;
      n /= i;
    }
  }
  FOR(i, 1, pos - 1) { cout << " " << dp[i]; }
  cout << endl;
  return 0;
}
Problem p02467 (C++)

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

bool isprime(int n) {
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  for (int i = 2; i <= sqrt(n) + 1; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int h;
  cin >> h;
  // int n=h;
  cout << h << ":";
  int n = h;
  for (int i = 2; i <= h; i++) {
    if (isprime(i)) {
      while (n % i == 0) {
        n /= i;
        cout << " " << i;
      }
      if (isprime(n))
        break;
    }
  }
  if (n != 1)
    cout << " " << n;
  cout << endl;
  return 0;
}

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

bool isprime(int n) {
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  for (int i = 2; i <= sqrt(n) + 1; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int h;
  cin >> h;
  // int n=h;
  cout << h << ":";
  int n = h;
  for (int i = 2; i <= sqrt(h) + 1; i++) {
    if (isprime(i)) {
      while (n % i == 0) {
        n /= i;
        cout << " " << i;
      }
      if (isprime(n))
        break;
    }
  }
  if (n != 1)
    cout << " " << n;
  cout << endl;
  return 0;
}
Problem p02467 (C++)

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

bool flag[1000010];

int main() {
  int n, p;
  cin >> n;
  p = n;
  cout << n << ":";
  while (1) {
    if (n == 1) {
      cout << endl;
      break;
    }
    for (int i = 2; 1000010; i++) {
      if (n % i == 0) {
        cout << " " << i;
        n /= i;
        break;
      }
    }
    if (n == p) {
      cout << " " << p << endl;
      break;
    }
  }
}

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

bool flag[1000010];

int main() {
  int n, p;
  cin >> n;
  p = n;
  cout << n << ":";
  while (1) {
    if (n == 1) {
      cout << endl;
      break;
    }
    for (int i = 2; i < 1000010; i++) {
      if (n % i == 0) {
        cout << " " << i;
        n /= i;
        break;
      }
    }
    if (n == p) {
      cout << " " << p << endl;
      break;
    }
  }
}
Problem p02467 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int isprime(int n) {
  if (n < 2)
    return 0;
  else
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return 0;
    }
  return 1;
} // dfs,
void solve(int n) {
  int sum = 0;
  cout << n << ":";
  while (n > 1) {
    for (int i = 2; i <= n; i++)
      if (isprime(i) && (n % i == 0)) {
        cout << " "; // 格式
        cout << i;
        n /= i;
        sum++;
        break;
      }
  }
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  solve(n);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int isprime(int n) {
  if (n < 2)
    return 0;
  else
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return 0;
    }
  return 1;
} // dfs,
void solve(int n) {
  if (isprime(n)) {
    cout << n << ": " << n << endl; // 特殊情况排除
    return;
  }
  int sum = 0;
  cout << n << ":";
  while (n > 1) {
    for (int i = 2; i <= n; i++)
      if (isprime(i) && (n % i == 0)) {
        cout << " "; // 格式
        cout << i;
        n /= i;
        sum++;
        break;
      }
  }
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  solve(n);
  return 0;
}
Problem p02467 (C++)

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

int main() {
  int n, i;

  cin >> n;

  cout << n << ":";

  for (i = 2; n != 1; i++) {
    if (i > n / 2) {
      cout << " " << n;
      n = 1;
      continue;
    }
    while (n % i == 0) {
      cout << " " << i;
      n /= i;
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, i;

  cin >> n;

  cout << n << ":";

  for (i = 2; n != 1; i++) {
    if (i > sqrt(n)) {
      cout << " " << n;
      n = 1;
      continue;
    }
    while (n % i == 0) {
      cout << " " << i;
      n /= i;
    }
  }
  cout << endl;

  return 0;
}
Problem p02467 (C++)

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

// 素数判定(素数ならtrue)
bool Prime(int prime) {
  // 偶数除く
  if (prime == 2) {
    return true;
  } else if (prime % 2 == 0 || prime < 2) {
    return false;
  }

  float Asqrt = sqrtf(prime);

  for (int i = 3; i <= Asqrt; i += 2) {
    if (prime % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  int n;

  cin >> n;

  cout << n << ":";

  for (int prime = 2;; prime++) {

    if (Prime(n)) {
      cout << " " << n << endl;
      break;
    }

    while (true) {
      // 素数 かつ 素因数の場合
      if (Prime(prime) && n % prime == 0) {
        n /= prime;
        cout << " " << prime;

        // 分解可能なら続ける
        continue;
      }

      // 分解不可でbreak;
      break;
    }
  }

  return 0;
}


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

// 素数判定(素数ならtrue)
bool Prime(int prime) {
  // 偶数除く
  if (prime == 2) {
    return true;
  } else if (prime % 2 == 0 || prime < 2) {
    return false;
  }

  float Asqrt = sqrtf(prime);

  for (int i = 3; i <= Asqrt; i += 2) {
    if (prime % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  int n;

  cin >> n;

  cout << n << ":";

  for (int prime = 2;; prime++) {

    if (Prime(n)) {
      cout << " " << n << endl;
      break;
    }

    while (true) {
      // 素数 かつ 素因数の場合
      if (Prime(prime) && n % prime == 0) {
        n /= prime;
        cout << " " << prime;

        if (n == 1) {
          cout << endl;
          exit(0);
        }

        // 分解可能なら続ける
        continue;
      }

      // 分解不可でbreak;
      break;
    }
  }

  return 0;
}
Problem p02467 (C++)

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

int main() {
  int n, i;

  cin >> n;

  cout << n << ":";

  for (i = 2; n != 1; i++) {
    while (n % i == 0) {
      cout << " " << i;
      n /= i;
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, i;

  cin >> n;

  cout << n << ":";

  for (i = 2; n != 1; i++) {
    if (i > sqrt(n)) {
      cout << " " << n;
      n = 1;
      continue;
    }
    while (n % i == 0) {
      cout << " " << i;
      n /= i;
    }
  }
  cout << endl;

  return 0;
}
Problem p02468 (C++)

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

bool judge(int s) {
  for (int i = 2; i * i <= s; i++) {
    if (s % i != 0 && (i + 1) * (i + 1) > s) {
      return true;
    }
    if (s % i == 0) {
      return false;
      break;
    }
  }
}

int main() {
  unsigned long long a;
  int m, n, b;
  cin >> m >> n;
  a = m;
  b = 1000000007;

  int l = n;
  int k[1000] = {};

  if (judge(l)) {
    k[0] = l;
  }

  else {
    int count = 0;
    for (int j = 2;;) {
      if (l == 1) {
        break;
      }
      if (l % j == 0 && judge(j)) {
        l = l / j;
        k[count] = j;
        count++;
        continue;
      }
      j++;
    }
  }

  for (int j = 0; k[j] != 0; j++) {
    for (int i = 1; i < k[j]; i++) {
      a = a * m;
      if (a >= b) {
        a = a % b;
        if (a == m && k[j] - i > k[j] % i) {
          k[j] = k[j] % i;
          i = 0;
        }
        if (a == 1) {
          break;
        }
      }
    }
    m = a;
  }

  cout << a << endl;

  return 0;
}


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

bool judge(int s) {
  for (int i = 2; i * i <= s; i++) {
    if (s % i != 0 && (i + 1) * (i + 1) > s) {
      return true;
    }
    if (s % i == 0) {
      return false;
      break;
    }
  }
}

int main() {
  unsigned long long a;
  int m, n, b;
  cin >> m >> n;
  a = m;
  b = 1000000007;

  int l = n;
  int k[1000] = {};

  if (judge(l)) {
    k[0] = l;
  }

  else {
    int count = 0;
    for (int j = 2;;) {
      if (l == 1) {
        break;
      }
      if (l % j == 0) {
        l = l / j;
        k[count] = j;
        count++;
        continue;
      }
      j++;
    }
  }

  for (int j = 0; k[j] != 0; j++) {
    for (int i = 1; i < k[j]; i++) {
      a = a * m;
      if (a >= b) {
        a = a % b;
        if (a == m && k[j] - i > k[j] % i) {
          k[j] = k[j] % i;
          i = 0;
        }
        if (a == 1) {
          break;
        }
      }
    }
    m = a;
  }

  cout << a << endl;

  return 0;
}
Problem p02468 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;
typedef long long ll;
/*
  AOJ power
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp
  繰り返し2乗法
*/

ll power(int m, int n) {
  ll ans = 1;
  while (n > 0) {
    if (n == 1)
      return (ans * m) % ll(1e9 + 7);
    ans *= m * m;
    ans %= ll(1e9 + 7);
    n -= 2;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> m >> n;
  cout << power(m, n) << "\n";
}


Fixed submission:
#include <iostream>

using namespace std;
typedef long long ll;
/*
  AOJ power
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp
  繰り返し2乗法
*/

ll power(int m, int n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return m * power(m, n - 1) % ll(1e9 + 7);
  ll k = power(m, n / 2);
  return k * k % ll(1e9 + 7);
}
int main() {
  int n, m;
  cin >> m >> n;
  cout << power(m, n) << "\n";
}
Problem p02468 (C++)

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

using namespace std;

#define MOD 1000000007LL

long long power(int a, int b) {
  if (b == 0) {
    return 1LL;
  }

  return power(a, b - 1) * a % MOD;
}

int main() {
  int n, m;

  cin >> m >> n;

  cout << power(m, n) << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

#define MOD 1000000007LL

long long power(int a, int b) {
  if (b == 0) {
    return 1LL;
  }

  if (b % 2 == 1) {
    return power(a, b / 2) * power(a, b / 2) % MOD * a % MOD;
  }

  return power(a, b / 2) * power(a, b / 2) % MOD;
}

int main() {
  int n, m;

  cin >> m >> n;

  cout << power(m, n) << endl;

  return 0;
}
Problem p02468 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>

int main(void) {
  long long n, i, m, mt, mt2;
  scanf("%lld%lld", &m, &n);
  mt = m, mt2 = m;
  m = 1;
  long long a = log2(n), b = n - (int)pow(2, a);
  while (a) {
    for (i = 0; i < a; ++i) {
      mt *= mt;
      mt %= 1000000007;
    }
    a = log2(b);
    m *= mt;
    m %= 1000000007;
    if (a)
      b = b - (int)pow(2, a);
    else if (b) {
      m *= mt2;
      m %= 1000000007;
    }
    mt = mt2;
  }
  printf("%lld\n", m);
  return 0;
}


Fixed submission:
#include <math.h>
#include <stdio.h>

int main(void) {
  long long n, i, m, mt, mt2;
  scanf("%lld%lld", &m, &n);
  mt = m, mt2 = m;
  m = 1;
  long long a = log2(n), b = n - (int)pow(2, a);
  while (a) {
    for (i = 0; i < a; ++i) {
      mt *= mt;
      mt %= 1000000007;
    }
    a = log2(!b ? 1 : b);
    m *= mt;
    m %= 1000000007;
    if (a)
      b = b - (int)pow(2, a);
    else if (b) {
      m *= mt2;
      m %= 1000000007;
    }
    mt = mt2;
  }
  printf("%lld\n", m);
  return 0;
}
Problem p02468 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int main() {
  int64_t m, n, q = 1000000007;
  std::cin >> m >> n;

  int64_t r = m % q;
  for (int64_t i = 1; i < n; i++) {
    r = (r * m) % q;
  }
  std::cout << r << "\n";
  return 0;
}


Fixed submission:
#include <iostream>

int main() {
  int64_t m, n, q = 1000000007;
  std::cin >> m >> n;

  int64_t r = 1;
  int64_t m2 = m * m;
  int64_t m4 = m * m * m * m;
  int64_t i = 0;
  for (int64_t j = 0; j < n / 4; j++) {
    r = (r * m4) % q;
    i += 4;
  }
  for (; i < n; i++) {
    r = (r * m) % q;
  }
  std::cout << r << "\n";
  return 0;
}
Problem p02469 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
#define MOD 1000000007
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(a, b % a);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
signed main() {
  int n;
  int a[11];
  cin >> n;
  rep(i, n) cin >> a[i];
  int ans = a[0];
  rep(i, n) ans = lcm(ans, a[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
#define MOD 1000000007
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
signed main() {
  int n;
  int a[11];
  cin >> n;
  rep(i, n) cin >> a[i];
  int ans = a[0];
  rep(i, n) ans = lcm(ans, a[i]);
  cout << ans << endl;
  return 0;
}
Problem p02469 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
inline int gcd(int x, int y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
inline int lcm(int x, int y) { return (x * y) / gcd(y, x % y); }
int main() {
  int n, x;
  vector<int> v;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> x, v.push_back(x);
  for (int i = 0; i < n; ++i) {
    v[i + 1] = lcm(v[i], v[i + 1]);
  }
  cout << v[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
inline int gcd(int x, int y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
inline int lcm(int x, int y) { return (x * y) / gcd(y, x % y); }
int main() {
  int n, x;
  vector<int> v;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> x, v.push_back(x);
  for (int i = 0; i < n - 1; ++i) {
    v[i + 1] = lcm(v[i], v[i + 1]);
  }
  cout << v[n - 1] << endl;
  return 0;
}
Problem p02469 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;

int gcd(int m, int n) {
  if (n == 0)
    return m;
  return gcd(n, m % n);
}

int lcm(int m, int n) { return m * n / gcd(m, n); }

int main() {
  int n, A[n - 1];

  cin >> n;
  for (int i = 0; i <= n - 1; i++)
    cin >> A[i];
  int ans = A[0];
  for (int i = 1; i <= n - 1; i++)
    ans = lcm(ans, A[i]);
  cout << ans << endl;
  return 0;
}

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

int gcd(int m, int n) {
  if (n == 0)
    return m;
  return gcd(n, m % n);
}

int lcm(int m, int n) { return m * n / gcd(m, n); }

int main() {
  int n, A[10];

  cin >> n;
  for (int i = 0; i <= n - 1; i++)
    cin >> A[i];
  int ans = A[0];
  for (int i = 1; i <= n - 1; i++)
    ans = lcm(ans, A[i]);
  cout << ans << endl;
  return 0;
}
Problem p02471 (C++)

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

#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

int ExtendedGCD(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  } else {
    int g = ExtendedGCD(b, a % b, x, y);
    tie(x, y) = make_tuple(y, x - a / b * y);
    return g;
  }
}

// int ExtendedGCD(int a,int b,int& x,int& y)
//{
//	x=1,y=0;
//	for(int u=0,v=1;b;){
//		ll t=a/b;
//		swap(a-=b*t,b);
//		swap(x-=u*t,u);
//		swap(y-=v*t,v);
//	}
//	return a;
// }

int main() {
  for (int a, b; cin >> a >> b && a | b;) {
    int x, y;
    for (int i = 0; i < 10000000; i++)
      ExtendedGCD(a, b, x, y);
    cout << x << ' ' << y << endl;
    ;
  }
}

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

#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

int ExtendedGCD(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  } else {
    int g = ExtendedGCD(b, a % b, x, y);
    tie(x, y) = make_tuple(y, x - a / b * y);
    return g;
  }
}

// int ExtendedGCD(int a,int b,int& x,int& y)
//{
//	x=1,y=0;
//	for(int u=0,v=1;b;){
//		ll t=a/b;
//		swap(a-=b*t,b);
//		swap(x-=u*t,u);
//		swap(y-=v*t,v);
//	}
//	return a;
// }

int main() {
  for (int a, b; cin >> a >> b && a | b;) {
    int x, y;
    for (int i = 0; i < 1000000; i++)
      ExtendedGCD(a, b, x, y);
    cout << x << ' ' << y << endl;
    ;
  }
}
Problem p02471 (C++)

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

using namespace std;

long long d = 0;
bool flag = 0;

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

long long judge(long long x, long long y) { return abs(x) + abs(y); }

pair<long long, long long> calc(long long a, long long b, long long x,
                                long long y) {
  if (a * x + b * y == d)
    return pair<long long, long long>(x, y);
  if (a * x + b * y == -d)
    return pair<long long, long long>(-x, -y);
  flag ^= 1;
  long long n = a * x + b * y, tmp = 0;
  if (b % n == 0)
    return calc(b, a, y, x);
  tmp = b / n;
  x *= tmp;
  y = y * tmp - 1;
  long long k = (x + y) / (b - a);
  x -= b * k;
  y += a * k;
  return calc(b, a, y, x);
}

int main() {
  long long a = 0, b = 0;
  cin >> a >> b;
  d = gcd(a, b);
  pair<long long, long long> ans = calc(a, b, 1, 0);
  if (flag) {
    long long tmp = ans.first;
    ans.first = ans.second;
    ans.second = tmp;
  }
  long long k = (ans.first + ans.second) / (b - a);
  ans.first -= b * k;
  ans.second += a * k;

  while (judge(ans.first - b / d, ans.second + a / d) <
         judge(ans.first, ans.second)) {
    ans.first -= b / d;
    ans.second += a / d;
  }
  while (judge(ans.first + b / d, ans.second - a / d) <
         judge(ans.first, ans.second)) {
    ans.first += b / d;
    ans.second -= a / d;
  }

  cout << ans.first << " " << ans.second << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

long long d = 0;
bool flag = 0;

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

long long judge(long long x, long long y) { return abs(x) + abs(y); }

pair<long long, long long> calc(long long a, long long b, long long x,
                                long long y) {
  if (a * x + b * y == d)
    return pair<long long, long long>(x, y);
  if (a * x + b * y == -d)
    return pair<long long, long long>(-x, -y);
  flag ^= 1;
  long long n = a * x + b * y, tmp = 0;
  if (b % n == 0)
    return calc(b, a, y, x);
  tmp = b / n;
  x *= tmp;
  y = y * tmp - 1;
  long long k = (x + y) / (b - a);
  x -= b * k;
  y += a * k;
  return calc(b, a, y, x);
}

int main() {
  long long a = 0, b = 0;
  cin >> a >> b;
  if (a == b) {
    cout << "0 1" << endl;
    return 0;
  }
  d = gcd(a, b);
  pair<long long, long long> ans = calc(a, b, 1, 0);
  if (flag) {
    long long tmp = ans.first;
    ans.first = ans.second;
    ans.second = tmp;
  }
  long long k = (ans.first + ans.second) / (b - a);
  ans.first -= b * k;
  ans.second += a * k;

  while (judge(ans.first - b / d, ans.second + a / d) <
         judge(ans.first, ans.second)) {
    ans.first -= b / d;
    ans.second += a / d;
  }
  while (judge(ans.first + b / d, ans.second - a / d) <
         judge(ans.first, ans.second)) {
    ans.first += b / d;
    ans.second -= a / d;
  }

  cout << ans.first << " " << ans.second << endl;
  return 0;
}
Problem p02471 (C++)

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

ll s1, s2, s, t1, t2, t;

void xgcd(ll x, ll y) {
  if (x % y == 0)
    return;
  ll q = x / y;
  s = s2 - q * s1;
  t = t2 - q * t1;
  s2 = s1, t2 = t1, s1 = s, t1 = t;
  xgcd(y, x % y);
}

int main() {
  ll a, b;
  while (1) {
    cin >> a >> b;
    s1 = 0;
    s2 = 1;
    t1 = 1;
    t2 = 0;
    xgcd(a, b);
    cout << s1 << " " << t1 << endl;
  }
  return 0;
}

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

ll s1, s2, s, t1, t2, t;

void xgcd(ll x, ll y) {
  if (x % y == 0)
    return;
  ll q = x / y;
  s = s2 - q * s1;
  t = t2 - q * t1;
  s2 = s1, t2 = t1, s1 = s, t1 = t;
  xgcd(y, x % y);
}

int main() {
  ll a, b;
  // while(1) {
  cin >> a >> b;
  s1 = 0;
  s2 = 1;
  t1 = 1;
  t2 = 0;
  xgcd(a, b);
  cout << s1 << " " << t1 << endl;
  //}
  return 0;
}
Problem p02472 (Python)

Buggy submission (status: Runtime Error):
a = int(input())
b = int(input())
print(a + b)


Fixed submission:
print(sum(map(int, input().split())))
Problem p02534 (C++)

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

int main(void) {
  int n;
  while (n--) {
    cout << "ACL";
  }
}

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

int main(void) {
  int n;
  cin >> n;
  while (n--) {
    cout << "ACL";
  }
}
Problem p02536 (C++)

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

int par[100010];

int parent(int i) {
  if (par[i] == i)
    return i;
  else {
    return par[i] = parent(par[i]);
  }
}

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

  for (int i = 0; i < N; i++)
    par[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    int pa = par[A], pb = par[B];
    if (pa != pb) {
      par[pa] = pb;
    }
  }

  set<int> S;
  for (int i = 0; i < N; i++) {
    S.insert(parent(par[i]));
  }
  cout << S.size() - 1 << endl;
}

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

int par[100010];

int parent(int i) {
  if (par[i] == i)
    return i;
  else {
    return par[i] = parent(par[i]);
  }
}

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

  for (int i = 0; i < N; i++)
    par[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    int pa = parent(A), pb = parent(B);
    if (pa == pb)
      continue;
    else {
      if (pa > pb) {
        par[pa] = pb;
      } else {
        par[pb] = pa;
      }
    }
  }

  set<int> S;
  for (int i = 0; i < N; i++) {
    S.insert(parent(par[i]));
  }
  cout << S.size() - 1 << endl;
}
Problem p02536 (C++)

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

int root(int x, vector<int> &city) {
  if (city.at(x) == x) {
    return x;
  } else {
    return city.at(x) = root(city.at(x), city);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> city(n);
  vector<int> rank(n);
  vector<int> unionCity(n);
  for (int i = 0; i < n; i++) {
    city.at(i) = i;
    rank.at(i) = 0;
    unionCity.at(i) = 0;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int x = city.at(a - 1);
    int y = city.at(b - 1);
    if (rank[x] < rank[y]) {
      city.at(x) = y;
    } else {
      city.at(y) = x;
    }
    if (rank.at(x) == rank.at(y)) {
      rank.at(x)++;
    }
  }

  for (int i = 0; i < n; i++) {
    int rt = root(i, city);
    if (unionCity.at(rt) == 0) {
      unionCity.at(rt)++;
    }
  }
  int sum = 0;

  for (int i = 0; i < n; i++) {
    sum += unionCity.at(i);
  }
  cout << sum - 1 << endl;
}

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

int root(int x, vector<int> &city) {
  if (city.at(x) == x) {
    return x;
  } else {
    return city.at(x) = root(city.at(x), city);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> city(n);
  vector<int> rank(n);
  vector<int> unionCity(n);
  for (int i = 0; i < n; i++) {
    city.at(i) = i;
    rank.at(i) = 0;
    unionCity.at(i) = 0;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int x = city.at(a - 1);
    int y = city.at(b - 1);
    int rtx = root(x, city);
    int rty = root(y, city);
    if (rtx != rty) {
      if (rank[rtx] < rank[rty]) {
        city.at(rtx) = rty;
      } else {
        city.at(rty) = rtx;
      }
      if (rank.at(rtx) == rank.at(rty)) {
        rank.at(rtx)++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    int rt = root(i, city);
    if (unionCity.at(rt) == 0) {
      unionCity.at(rt)++;
    }
  }
  int sum = 0;

  for (int i = 0; i < n; i++) {
    sum += unionCity.at(i);
  }
  cout << sum - 1 << endl;
}
Problem p02536 (C++)

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

struct union_find {
  vector<int> r;
  union_find(int n) : r(n, -1) {}
  int root(int a) {
    if (r[a] < 0) {
      return a;
    } else {
      return root(r[a]);
    }
  }
  void add(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (r[ra] > r[rb]) {
      swap(ra, rb);
    }
    r[ra] += r[rb];
    r[rb] = ra;
  }
  int sum() {
    int s = 0;
    for (int i = 0; i < r.size(); i++) {
      if (r[i] < 0) {
        s++;
      }
    }
    return s;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  union_find u(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    u.add(a, b);
  }
  cout << u.sum() - 1 << endl;
}

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

struct union_find {
  vector<int> r;
  union_find(int n) : r(n, -1) {}
  int root(int a) {
    if (r[a] < 0) {
      return a;
    } else {
      return root(r[a]);
    }
  }
  void add(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (r[ra] > r[rb]) {
      swap(ra, rb);
    } else if (ra == rb) {
      return;
    }
    r[ra] += r[rb];
    r[rb] = ra;
  }
  int sum() {
    int s = 0;
    for (int i = 0; i < r.size(); i++) {
      if (r[i] < 0) {
        s++;
      }
    }
    return s;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  union_find u(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    u.add(a, b);
  }
  cout << u.sum() - 1 << endl;
}
Problem p02536 (C++)

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

using namespace std;

int P(int i, vector<int> &p) {
  if (p[i] == i)
    return i;
  else
    return p[i] = P(p[i], p);
}

void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  int i;
  for (i = 0; i < N; i++)
    p[i] = i;
  int a, b;
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    C(a, b, p);
  }
  vector<bool> f(N, false);
  int cnt = 0;
  for (i = 0; i < N; i++) {
    if (!f[P(i, p)]) {
      f[p[i]] = true;
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}

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

using namespace std;

int P(int i, vector<int> &p) {
  if (p[i] == i)
    return i;
  else
    return p[i] = P(p[i], p);
}

void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  int i;
  for (i = 0; i < N; i++)
    p[i] = i;
  int a, b;
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    C(a - 1, b - 1, p);
  }
  vector<bool> f(N, false);
  int cnt = 0;
  for (i = 0; i < N; i++) {
    if (!f[P(i, p)]) {
      f[p[i]] = true;
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 1e5 + 5;

int n, m, a, b, ans;
int par[N];
bool vis[N];

void init() {
  for (int i = 1; i <= n; i++)
    par[i] = i;
}

int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

void join(int x, int y) { par[root(x)] = y; }

void solve() {
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    cin >> n >> m;
    init();
    while (m--) {
      cin >> a >> b;
      join(a, b);
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[root(i)]) {
        vis[root(i)] = true;
        ans++;
      }
    }
    cout << ans - 1 << "\n";
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 1e5 + 5;

int n, m, a, b, ans;
int par[N];
bool vis[N];

void init() {
  for (int i = 1; i <= n; i++)
    par[i] = i;
}

int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

void join(int x, int y) { par[root(x)] = root(y); }

void solve() {
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    cin >> n >> m;
    init();
    while (m--) {
      cin >> a >> b;
      join(a, b);
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[root(i)]) {
        vis[root(i)] = true;
        ans++;
      }
    }
    cout << ans - 1 << "\n";
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}