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

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

using namespace std;

struct Union_find {
  vector<int> par;
  vector<int> rank;
  Union_find(int p) {
    par = vector<int>(p + 1);
    rank = vector<int>(p + 1);
    for (int i = 1; i <= p; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    Union_find uf(n);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    int root = uf.find(1);
    for (int j = 2; j <= n; j++) {
      if (root != uf.find(j)) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

struct Union_find {
  vector<int> par;
  vector<int> rank;
  Union_find(int p) {
    par = vector<int>(p + 1);
    rank = vector<int>(p + 1);
    for (int i = 1; i <= p; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    Union_find uf(n);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    int root = uf.find(1);
    for (int j = 2; j <= n; j++) {
      if (root != uf.find(j)) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF (1 << 30) - 1
using ll = long long;
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vi> to(n);
  vi a(n), b(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    to[a[i]].push_back(b[i]);
    to[b[i]].push_back(a[i]);
  }
  int ans = 0;
  rep(i, m) {
    queue<int> q;
    q.push(0);
    vi node(n, -1);
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      for (int next : to[now]) {
        if (now == a[i] && next == b[i] || now == b[i] && next == a[i])
          continue;
        if (0 <= node[next])
          continue;
        node[next] = now;
        q.push(next);
      }
    }
    bool ok = false;
    rep(j, n) if (node[j] == -1) ok = true;
    if (ok)
      ans++;
  }
  cout << ans;
  cout << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF (1 << 30) - 1
using ll = long long;
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vi> to(n);
  vi a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    to[a[i]].push_back(b[i]);
    to[b[i]].push_back(a[i]);
  }
  int ans = 0;
  rep(i, m) {
    queue<int> q;
    q.push(0);
    vi node(n, -1);
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      for (int next : to[now]) {
        if (now == a[i] && next == b[i] || now == b[i] && next == a[i])
          continue;
        if (0 <= node[next])
          continue;
        node[next] = now;
        q.push(next);
      }
    }
    bool ok = false;
    rep(j, n) if (node[j] == -1) ok = true;
    if (ok)
      ans++;
  }
  cout << ans;
  cout << "\n";
  return 0;
}
Problem p03575 (C++)

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

class UnionFind {
public:
  vector<int> par;  // 各ノードの親
  vector<int> rank; // 各ノードの深さ
  int sz;           // 独立した集合の個数

  // コンストラクタ
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      par.push_back(i);
      rank.push_back(0);
    }
    sz = n;
  }

  // 木の根を求める
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  // x と y の属する集合を併合
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    // 深さが小さいものから大きい方の根へ辺を張る
    if (rank[x] < rank[y])
      par[x] = y;
    else if (rank[x] > rank[y])
      par[y] = x;
    else {
      par[y] = x;
      ++rank[x];
    }
    --sz;
  }

  // x と y が同じ木に属するかの判定
  bool is_same(int x, int y) { return find(x) == find(y); }

  // 独立した集合の個数
  int size() { return sz; }
};

class edge {
public:
  int from, to;
  edge(int f, int t) {
    from = f;
    to = t;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<edge> G;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G.push_back(edge(a - 1, b - 1));
  }

  // 各辺に対して取り除く場合を考える
  int ans = 0;
  for (int i = 0; i < M; ++i) {
    UnionFind uf(N);
    for (int j = 0; j < M; ++j) {
      if (j != i) {
        edge e = G[j];
        uf.unite(e.from, e.to);
      }
    }
    bool connected = true;
    for (int j = 0; j < N - 1; ++j) {
      if (!uf.is_same(i, i + 1)) {
        connected = false;
      }
    }
    if (!connected)
      ++ans;
  }

  cout << ans << endl;
}


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

class UnionFind {
public:
  vector<int> par;  // 各ノードの親
  vector<int> rank; // 各ノードの深さ
  int sz;           // 独立した集合の個数

  // コンストラクタ
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      par.push_back(i);
      rank.push_back(0);
    }
    sz = n;
  }

  // 木の根を求める
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  // x と y の属する集合を併合
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    // 深さが小さいものから大きい方の根へ辺を張る
    if (rank[x] < rank[y])
      par[x] = y;
    else if (rank[x] > rank[y])
      par[y] = x;
    else {
      par[y] = x;
      ++rank[x];
    }
    --sz;
  }

  // x と y が同じ木に属するかの判定
  bool is_same(int x, int y) { return find(x) == find(y); }

  // 独立した集合の個数
  int size() { return sz; }
};

class edge {
public:
  int from, to;
  edge(int f, int t) {
    from = f;
    to = t;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<edge> G;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G.push_back(edge(a - 1, b - 1));
  }

  // 各辺に対して取り除く場合を考える
  int ans = 0;
  for (int i = 0; i < M; ++i) {
    UnionFind uf(N);
    for (int j = 0; j < M; ++j) {
      if (j != i) {
        edge e = G[j];
        uf.unite(e.from, e.to);
      }
    }
    bool connected = true;
    for (int j = 0; j < N - 1; ++j) {
      if (!uf.is_same(j, j + 1)) {
        connected = false;
      }
    }
    if (!connected)
      ++ans;
  }

  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
vector<bool> seen;

class union_find {
public:
  vector<int> par, rank;
  union_find(int n) : par(n), rank(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> w(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    pair<int, int> p = make_pair(x, y);
    w[i] = p;
  }
  int count = 0;
  int ans = 0;
  for (int j = 0; j < m; j++) {
    union_find g(n);
    for (int k = 0; k < m; k++) {
      if (j == k)
        continue;
      pair<int, int> p = w[k];
      g.unite(p.first, p.second);
    }
    count = 0;
    for (int i = 0; i < n; i++) {
      if (g.par[i] == i)
        count++;
    }
    if (count != 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
vector<bool> seen;

class union_find {
public:
  vector<int> par, rank;
  union_find(int n) : par(n), rank(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> w(m);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    pair<int, int> p = make_pair(x, y);
    w[i] = p;
  }
  int count = 0;
  int ans = 0;
  for (int j = 0; j < m; j++) {
    union_find g(n);
    for (int k = 0; k < m; k++) {
      if (j == k)
        continue;
      pair<int, int> p = w[k];
      g.unite(p.first, p.second);
    }
    count = 0;
    for (int i = 0; i < n; i++) {
      if (g.par[i] == i)
        count++;
    }
    if (count != 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

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

vector<vector<int>> graph;
vector<int> a, b;
vector<int> flag;
int n, m;

void dfs(int v) {
  flag[v] = 1;
  for (int i = 0; i < n; i++) {
    if (graph[v][i] == 1 && flag[i] == 0) {
      dfs(i);
    } else {
      continue;
    }
  }
}

int main() {
  cin >> n >> m;
  int ans = 0;
  a = vector<int>(n + 2, 0);
  b = vector<int>(n + 2, 0);
  graph = vector<vector<int>>(n + 2, vector<int>(n + 2, 0));
  flag = vector<int>(n, 0);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    graph[a[i]][b[i]] = 0;
    graph[b[i]][a[i]] = 0;
    for (int l = 0; l < n; l++) {
      flag[l] = 0;
    }
    dfs(0);
    int f = 0;
    for (int l = 0; l < n; l++) {
      if (flag[l] == 0) {
        f = 1;
        break;
      }
    }
    ans += f;
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }
  cout << ans << endl;
}

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

vector<vector<int>> graph;
vector<int> a, b;
vector<int> flag;
int n, m;

void dfs(int v) {
  flag[v] = 1;
  for (int i = 0; i < n; i++) {
    if (graph[v][i] == 1 && flag[i] == 0) {
      dfs(i);
    } else {
      continue;
    }
  }
}

int main() {
  cin >> n >> m;
  int ans = 0;
  a = vector<int>(51, 0);
  b = vector<int>(51, 0);
  graph = vector<vector<int>>(51, vector<int>(51, 0));
  flag = vector<int>(51, 0);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    graph[a[i]][b[i]] = 0;
    graph[b[i]][a[i]] = 0;
    for (int l = 0; l < n; l++) {
      flag[l] = 0;
    }
    dfs(0);
    int f = 0;
    for (int l = 0; l < n; l++) {
      if (flag[l] == 0) {
        f = 1;
        break;
      }
    }
    ans += f;
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
// q086.cpp
// Sun Sep  6 16:35:59 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

int par[60]; // 親。1個上の親でしか無いことに注意。グループの根はfindで呼び出す。
int treerank[60]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    treerank[i] = 0;
  }
}

// 木の根（親）を見つける。再帰することで、グループの根まで辿る。
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合の併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }

  if (treerank[x] < treerank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (treerank[x] == treerank[y]) {
      treerank[x]++;
    }
  }
}

// xとyが同じグループに属するか判定
bool same(int x, int y) { return find(x) == find(y); }

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

  vector<int> a(m, 0);
  vector<int> b(m, 0);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  int count = 0;
  int ans = 0;
  while (count < m) {
    init(n);
    rep(i, n) {
      if (i == count)
        continue;
      unite(a[i], b[i]);
    }
    set<int> st;
    rep(i, n) { st.insert(find(i)); }
    if (st.size() != 1) {
      ans++;
    }
    count++;
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}

Fixed submission:
// q086.cpp
// Sun Sep  6 16:35:59 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

int par[60]; // 親。1個上の親でしか無いことに注意。グループの根はfindで呼び出す。
int treerank[60]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    treerank[i] = 0;
  }
}

// 木の根（親）を見つける。再帰することで、グループの根まで辿る。
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合の併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }

  if (treerank[x] < treerank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (treerank[x] == treerank[y]) {
      treerank[x]++;
    }
  }
}

// xとyが同じグループに属するか判定
bool same(int x, int y) { return find(x) == find(y); }

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

  vector<int> a(m, 0);
  vector<int> b(m, 0);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  int count = 0;
  int ans = 0;
  while (count < m) {
    init(n);
    rep(i, m) {
      if (i == count)
        continue;
      unite(a[i], b[i]);
    }
    set<int> st;
    rep(i, n) { st.insert(find(i)); }
    if (st.size() != 1) {
      ans++;
    }
    count++;
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}
Problem p03575 (C++)

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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  vector<vector<bool>> edge(n, vector<bool>(n, false));
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    edge[a[i]][b[i]] = true;
    edge[b[i]][a[i]] = true;
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    edge[a[i]][b[i]] = false;
    edge[b[i]][a[i]] = false;

    vector<bool> vis(n, false);
    vis[0] = true;
    queue<int> que;
    que.push(0);
    while (que.size()) {
      int now = que.front();
      que.pop();
      for (int j = 0; j < m; j++)
        if (edge[now][j] && !vis[j]) {
          vis[j] = true;
          que.push(j);
        }
    }
    bool ok = false;
    for (int j = 0; j < n; j++)
      if (!vis[j])
        ok = true;
    if (ok)
      ans++;

    edge[a[i]][b[i]] = true;
    edge[b[i]][a[i]] = true;
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  vector<vector<bool>> edge(n, vector<bool>(n, false));
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    edge[a[i]][b[i]] = true;
    edge[b[i]][a[i]] = true;
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    edge[a[i]][b[i]] = false;
    edge[b[i]][a[i]] = false;

    vector<bool> vis(n, false);
    vis[0] = true;
    queue<int> que;
    que.push(0);
    while (que.size()) {
      int now = que.front();
      que.pop();
      for (int j = 0; j < m; j++)
        if (edge[now][j] && !vis[j]) {
          vis[j] = true;
          que.push(j);
        }
    }
    bool ok = false;
    for (int j = 0; j < n; j++)
      if (!vis[j])
        ok = true;
    if (ok)
      ans++;

    edge[a[i]][b[i]] = true;
    edge[b[i]][a[i]] = true;
  }
  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define inf LLONG_MAX
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

class UnionFind {
  vector<ll> par;

public:
  UnionFind(ll n) {
    par.resize(n);
    rep(i, n) { par[i] = i; }
  }

  ll root(ll a) {
    if (par[a] == a) {
      return a;
    } else {
      return par[a] = root(par[a]);
    }
  }

  bool is_same(ll a, ll b) { return root(a) == root(b); }

  void unite(ll a, ll b) {
    ll ra = root(a);
    ll rb = root(b);
    if (ra == rb)
      return;
    par[ra] = rb;
  }

  void printall() {
    rep(i, this->par.size()) { cout << i << ": " << this->par[i] << endl; }
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> ab(n);
  ll count = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    ab[i] = make_pair(a - 1, b - 1);
  }

  rep(i, m) {
    UnionFind uf = UnionFind(n);
    rep(j, m) {
      if (j == i)
        continue;
      auto elem = ab[j];
      uf.unite(elem.first, elem.second);
    }

    if (!uf.is_same(ab[i].first, ab[i].second)) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define inf LLONG_MAX
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

class UnionFind {
  vector<ll> par;

public:
  UnionFind(ll n) {
    par.resize(n);
    rep(i, n) { par[i] = i; }
  }

  ll root(ll a) {
    if (par[a] == a) {
      return a;
    } else {
      return par[a] = root(par[a]);
    }
  }

  bool is_same(ll a, ll b) { return root(a) == root(b); }

  void unite(ll a, ll b) {
    ll ra = root(a);
    ll rb = root(b);
    if (ra == rb)
      return;
    par[ra] = rb;
  }

  void printall() {
    rep(i, this->par.size()) { cout << i << ": " << this->par[i] << endl; }
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> ab(m);
  ll count = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    ab[i] = make_pair(a - 1, b - 1);
  }

  rep(i, m) {
    UnionFind uf = UnionFind(n);
    rep(j, m) {
      if (j == i)
        continue;
      auto elem = ab[j];
      uf.unite(elem.first, elem.second);
    }

    if (!uf.is_same(ab[i].first, ab[i].second)) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

bool seen[55];

void dfs(vector<int> g[55], int v) {
  seen[v] = true;
  for (auto to : g[v]) {
    if (seen[to])
      continue;
    dfs(g, to);
  }
}

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  int a[n], b[n];
  rep(i, m) cin >> a[i] >> b[i];
  rep(i, m) {
    vector<int> g[55];
    rep(j, m) {
      if (j != i) {
        g[a[j] - 1].push_back(b[j] - 1);
        g[b[j] - 1].push_back(a[j] - 1);
      }
    }
    memset(seen, 0, sizeof(seen));
    dfs(g, 0);
    bool flag = true;
    rep(i, n) if (!seen[i]) flag = false;
    if (!flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

bool seen[55];

void dfs(vector<int> g[55], int v) {
  seen[v] = true;
  for (auto to : g[v]) {
    if (seen[to])
      continue;
    dfs(g, to);
  }
}

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  int a[m], b[m];
  rep(i, m) cin >> a[i] >> b[i];
  rep(i, m) {
    vector<int> g[55];
    rep(j, m) {
      if (j != i) {
        g[a[j] - 1].push_back(b[j] - 1);
        g[b[j] - 1].push_back(a[j] - 1);
      }
    }
    memset(seen, 0, sizeof(seen));
    dfs(g, 0);
    bool flag = true;
    rep(i, n) if (!seen[i]) flag = false;
    if (!flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n, m;
int g[51][51];
bool visited[51];
bool dfs(int s) {
  for (int i = 0; i < n; i++) {
    if (s != i && (g[s][i] || g[i][s])) {
      g[s][i] = 0;
      g[i][s] = 0;
      visited[i] = true;
      dfs(i);
      g[s][i] = 1;
      g[i][s] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i])
      return false;
  }
  return true;
}
int main(void) {
  int a[50], b[50];
  int ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      g[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]][b[i]] = 1;
    g[b[i]][a[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    g[a[i]][b[i]] = 0;
    g[b[i]][a[i]] = 0;
    for (int j = 0; j < n; j++)
      visited[j] = false;
    visited[0] = true;
    if (!dfs(0))
      ans++;
    g[a[i]][b[i]] = 1;
    g[b[i]][a[i]] = 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, m;
int g[51][51];
bool visited[51];
bool dfs(int s) {
  for (int i = 0; i < n; i++) {
    if (s != i && (g[s][i] || g[i][s]) && !visited[i]) {
      g[s][i] = 0;
      g[i][s] = 0;
      visited[i] = true;
      dfs(i);
      g[s][i] = 1;
      g[i][s] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i])
      return false;
  }
  return true;
}
int main(void) {
  int a[50], b[50];
  int ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      g[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]][b[i]] = 1;
    g[b[i]][a[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    g[a[i]][b[i]] = 0;
    g[b[i]][a[i]] = 0;
    for (int j = 0; j < n; j++)
      visited[j] = false;
    visited[0] = true;
    if (!dfs(0))
      ans++;
    g[a[i]][b[i]] = 1;
    g[b[i]][a[i]] = 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03576 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

const int N = 50;
ll n, k, x[N], y[N], res, x2, x3, sz, t;
vector<ll> v;

bool cmp(int i, int j) { return y[i] < y[j]; }

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> y[i];
  res = -1;
  for (int a = 1; a <= n; a++) {
    for (int b = 1; b <= n; b++) {
      x2 = min(x[a], x[b]);
      x3 = max(x[a], x[b]);
      v.clear();
      for (int i = 1; i <= n; i++) {
        if (x[i] >= x2 && x[i] <= x3)
          v.pb(i);
      }
      sz = v.size();
      if (sz < k)
        continue;
      sort(v.begin(), v.end(), cmp);
      for (int i = k - 1, j = 0; i < sz; i++, j++) {
        t = (x3 - x2) * (y[v[i]] - y[v[j]]);
        if (res == -1 || res > t)
          res = t;
      }
    }
  }
  cout << res << "\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef localProject
  freopen("in.txt", "r", stdin);
#endif
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

const int N = 60;
ll n, k, x[N], y[N], res, x2, x3, sz, t;
vector<ll> v;

bool cmp(int i, int j) { return y[i] < y[j]; }

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> y[i];
  res = -1;
  for (int a = 1; a <= n; a++) {
    for (int b = 1; b <= n; b++) {
      x2 = min(x[a], x[b]);
      x3 = max(x[a], x[b]);
      v.clear();
      for (int i = 1; i <= n; i++) {
        if (x[i] >= x2 && x[i] <= x3)
          v.pb(i);
      }
      sz = v.size();
      if (sz < k)
        continue;
      sort(v.begin(), v.end(), cmp);
      for (int i = k - 1, j = 0; i < sz; i++, j++) {
        t = (x3 - x2) * (y[v[i]] - y[v[j]]);
        if (res == -1 || res > t)
          res = t;
      }
    }
  }
  cout << res << "\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef localProject
  freopen("in.txt", "r", stdin);
#endif
  solve();
  return 0;
}
Problem p03576 (C++)

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

typedef unsigned long long ull;
typedef long long ll;

struct point {
  ll x, y;
  bool operator<(const point &t) const {
    if (x == t.x)
      return y < t.y;
    return x < t.y;
  }
};

ull k_in(ll xmin, ll xmax, ll ymin, ll ymax, vector<point> ps, ll K) {
  ll in = 0;
  for (auto &e : ps) {
    if (xmin <= e.x and e.x <= xmax and ymin <= e.y and e.y <= ymax)
      in++;
  }
  if (K <= in) {
    return (ull)(xmax - xmin) * (ull)(ymax - ymin);
  }
  return 18446744073709551610ULL;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> x;
  vector<ll> y;

  vector<point> ps;
  for (ll i = 0; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    x.push_back(a);
    y.push_back(b);
    ps.push_back(point{a, b});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  sort(ps.begin(), ps.end());

  ull ans = 18446744073709551610ULL;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      for (int k = 0; k < N; k++) {
        for (int l = k + 1; l < N; l++) {
          ans = min(k_in(x[i], x[j], y[k], y[l], ps, K), ans);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef unsigned long long ull;
typedef long long ll;

struct point {
  ll x, y;
  bool operator<(const point &t) const {
    if (x == t.x)
      return y < t.y;
    return x < t.x;
  }
};

ull k_in(ll xmin, ll xmax, ll ymin, ll ymax, vector<point> ps, ll K) {
  ll in = 0;
  for (auto &e : ps) {
    if (xmin <= e.x and e.x <= xmax and ymin <= e.y and e.y <= ymax)
      in++;
  }
  if (K <= in) {
    return (ull)(xmax - xmin) * (ull)(ymax - ymin);
  }
  return 18446744073709551610ULL;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> x;
  vector<ll> y;

  vector<point> ps;
  for (ll i = 0; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    x.push_back(a);
    y.push_back(b);
    ps.push_back(point{a, b});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  sort(ps.begin(), ps.end());

  ull ans = 18446744073709551610ULL;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      for (int k = 0; k < N; k++) {
        for (int l = k + 1; l < N; l++) {
          ans = min(k_in(x[i], x[j], y[k], y[l], ps, K), ans);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03576 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll x[55], y[55];
int n, k;

vector<vector<int>> ret;

void prep(vector<int> v) {
  if (v.size() == 5) {
    ret.push_back(v);
    return;
  }
  int b = v.back();
  rep(i, n) {
    if (i < b)
      continue;
    v.push_back(i);
    prep(v);
    v.pop_back();
  }
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> x[i] >> y[i];
  vector<int> ini;
  ini.push_back(0);

  prep(ini);
  ll ans = 5e18;

  rep(i, ret.size()) {
    ll max_x = 0, max_y = 0, min_x = LINF, min_y = LINF;

    for (int j = 1; j <= max(k, 4); j++) {
      max_x = max(max_x, x[ret[i][j]]);
      min_x = min(min_x, x[ret[i][j]]);
      max_y = max(max_y, y[ret[i][j]]);
      min_y = min(min_y, y[ret[i][j]]);
    }
    int cnt = 0;
    rep(j, n) if (min_x <= x[j] && x[j] <= max_x && min_y <= y[j] &&
                  y[j] <= max_y) cnt++;
    if (cnt < k)
      continue;
    ans = min(ans, (max_x - min_x) * (max_y - min_y));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll x[55], y[55];
int n, k;

vector<vector<int>> ret;

void prep(vector<int> v) {
  if (v.size() == 5) {
    ret.push_back(v);
    return;
  }
  int b = v.back();
  rep(i, n) {
    if (i < b)
      continue;
    v.push_back(i);
    prep(v);
    v.pop_back();
  }
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> x[i] >> y[i];
  vector<int> ini;
  ini.push_back(0);

  prep(ini);
  ll ans = 5e18;

  rep(i, ret.size()) {
    ll max_x = -LINF, max_y = -LINF, min_x = LINF, min_y = LINF;
    for (int j = 1; j <= min(k, 4); j++) {
      max_x = max(max_x, x[ret[i][j]]);
      min_x = min(min_x, x[ret[i][j]]);
      max_y = max(max_y, y[ret[i][j]]);
      min_y = min(min_y, y[ret[i][j]]);
    }
    int cnt = 0;
    rep(j, n) if (min_x <= x[j] && x[j] <= max_x && min_y <= y[j] &&
                  y[j] <= max_y) cnt++;
    if (cnt < k)
      continue;
    ans = min(ans, (max_x - min_x) * (max_y - min_y));
  }
  cout << ans << endl;

  return 0;
}
Problem p03576 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;

int main() {
  int N, K;
  cin >> N >> K;
  ll x[N], y[N];
  vector<vector<ll>> point(N, vector<ll>(2));
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
    point[i][0] = x[i];
    point[i][1] = y[i];
  }
  sort(x, x + N);
  sort(y, y + N);

  ll result = LLINF;
  for (int x1 = 0; x1 < N; x1++) {
    for (int x2 = 0; x2 <= x1; x2++) {
      for (int y1 = 0; y1 < N; y1++) {
        for (int y2 = 0; y2 <= y1; y2++) {
          int k_num = 0;
          for (auto p : point) {
            if ((p[0] <= x[x1] && p[0] >= x[x2]) &&
                (p[1] <= y[y1] && p[1] >= y[y2]))
              k_num++;
          }
          if (k_num >= K)
            result = min(result, (x[x1] - x[x2]) * (y[y1] - y[y2]));
          // if (k_num >= K) cout << x[x1] << x[x2] << y[y1] << y[y2] << k_num
          // << endl;
        }
      }
    }
  }
  cout << result << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;

int main() {
  int N, K;
  cin >> N >> K;
  ll x[N], y[N];
  vector<vector<ll>> point(N, vector<ll>(2));
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
    point[i][0] = x[i];
    point[i][1] = y[i];
  }
  sort(x, x + N);
  sort(y, y + N);

  ll result = LLINF;
  for (int x1 = 0; x1 < N; x1++) {
    for (int x2 = 0; x2 <= x1; x2++) {
      for (int y1 = 0; y1 < N; y1++) {
        for (int y2 = 0; y2 <= y1; y2++) {
          int k_num = 0;
          for (auto &p : point) {
            if ((p[0] <= x[x1] && p[0] >= x[x2]) &&
                (p[1] <= y[y1] && p[1] >= y[y2]))
              k_num++;
          }
          if (k_num >= K)
            result = min(result, (x[x1] - x[x2]) * (y[y1] - y[y2]));
          // if (k_num >= K) cout << x[x1] << x[x2] << y[y1] << y[y2] << k_num
          // << endl;
        }
      }
    }
  }
  cout << result << endl;
  return 0;
}
Problem p03576 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 4e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

void solve() {
  int N, K;
  cin >> N >> K;

  vector<int> x(N), y(N), xs(N), ys(N);
  rep(i, 0, N) {
    cin >> x[i] >> y[i];
    xs.push_back(x[i]);
    ys.push_back(y[i]);
  }

  sort(begin(xs), end(xs));
  sort(begin(ys), end(ys));

  i64 ans = inf64;
  rep(i, 0, xs.size()) rep(j, i + 1, xs.size()) rep(k, 0, ys.size())
      rep(l, k + 1, ys.size()) {
    int x1 = xs[i], y1 = ys[k], x2 = xs[j], y2 = ys[l];
    i64 s = i64(x2 - x1) * i64(y2 - y1);
    if (s >= ans)
      continue;

    int cnt = 0;
    rep(i, 0,
        N) if (x1 <= x[i] and x[i] <= x2 and y1 <= y[i] and y[i] <= y2)++ cnt;

    if (cnt >= K)
      ans = s;
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 4e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

void solve() {
  int N, K;
  cin >> N >> K;

  vector<int> x(N), y(N), xs(N), ys(N);
  rep(i, 0, N) {
    cin >> x[i] >> y[i];
    xs.push_back(x[i]);
    ys.push_back(y[i]);
  }

  sort(begin(xs), end(xs));
  sort(begin(ys), end(ys));

  i64 ans = inf64;
  rep(i, 0, xs.size()) rep(j, i + 1, xs.size()) rep(k, 0, ys.size())
      rep(l, k + 1, ys.size()) {
    int x1 = xs[i], y1 = ys[k], x2 = xs[j], y2 = ys[l];
    i64 s = i64(x2 - x1) * i64(y2 - y1);
    if (s >= ans)
      continue;

    int cnt = 0;
    rep(i, 0,
        N) if (x1 <= x[i] and x[i] <= x2 and y1 <= y[i] and y[i] <= y2)++ cnt;

    if (cnt >= K)
      ans = s;
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}
Problem p03576 (C++)

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

struct BIT {
  BIT() {}
  BIT(int n) : n(n + 1), v(n + 1, vector<int>(n + 1, 0)) {}

  int n;
  vector<vector<int>> v;

  long long sum(int y, int x) {
    long long ret = 0;
    for (int i = y; i; i -= i & -i) {
      for (int j = x; j; j -= j & -j) {
        ret += v[i][j];
      }
    }

    return ret;
  }

  long long sum(int y1, int x1, int y2, int x2) {
    return sum(y1, x1) - sum(y1, x2) - sum(y2, x1) + sum(y2, x2);
  }

  void add(int y, int x) {
    for (int i = y; i <= n; i += i & -i) {
      for (int j = x; j <= n; j += j & -j) {
        v[i][j]++;
      }
    }
  }
};

int main() {
  int n, K;
  cin >> n >> K;

  vector<long long> x, y, a, b;
  map<long long, int> X, Y;
  for (int i = 0; i < n; ++i) {
    int s, t;
    cin >> s >> t;
    x.push_back(s);
    y.push_back(t);
  }

  a = x;
  b = y;

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; ++i) {
    X[a[i]] = i + 1;
    Y[b[i]] = i + 1;
  }

  BIT bit(n);
  for (int i = 0; i < n; ++i) {
    bit.add(Y[y[i]], X[x[i]]);
  }

  long long ans = 5001001001001001001;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        for (int l = k + 1; l < n; ++l) {
          if (bit.sum(Y[b[l]], X[a[j]], Y[b[k]] - 1, X[a[i]] - 1) >= K) {
            ans = min(ans, (b[l] - b[k]) * (a[j] - a[i]));
          }
        }
      }
    }
  }

  cout << ans << endl;
}


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

struct BIT {
  BIT() {}
  BIT(int n) : n(n + 1), v(n + 10, vector<int>(n + 10, 0)) {}

  int n;
  vector<vector<int>> v;

  long long sum(int y, int x) {
    long long ret = 0;
    for (int i = y; i; i -= i & -i) {
      for (int j = x; j; j -= j & -j) {
        ret += v[i][j];
      }
    }

    return ret;
  }

  long long sum(int y1, int x1, int y2, int x2) {
    return sum(y1, x1) - sum(y1, x2) - sum(y2, x1) + sum(y2, x2);
  }

  void add(int y, int x) {
    for (int i = y; i <= n; i += i & -i) {
      for (int j = x; j <= n; j += j & -j) {
        v[i][j]++;
      }
    }
  }
};

int main() {
  int n, K;
  cin >> n >> K;

  vector<long long> x, y, a, b;
  map<long long, int> X, Y;
  for (int i = 0; i < n; ++i) {
    int s, t;
    cin >> s >> t;
    x.push_back(s);
    y.push_back(t);
  }

  a = x;
  b = y;

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; ++i) {
    X[a[i]] = i + 1;
    Y[b[i]] = i + 1;
  }

  BIT bit(n);
  for (int i = 0; i < n; ++i) {
    bit.add(Y[y[i]], X[x[i]]);
  }

  long long ans = 5001001001001001001;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        for (int l = k + 1; l < n; ++l) {
          if (bit.sum(Y[b[l]], X[a[j]], Y[b[k]] - 1, X[a[i]] - 1) >= K) {
            ans = min(ans, (b[l] - b[k]) * (a[j] - a[i]));
          }
        }
      }
    }
  }

  cout << ans << endl;
}
Problem p03577 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  vector<char> s;
  char a = 'a';
  while (a != 'NULL') {
    cin >> a;
    s.push_back(a);
  }
  int b = s.size();
  for (int i = 0; i < b - 8; i++) {
    cout << s[i] << flush;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s, s2;
  cin >> s;
  int a;
  a = s.size();
  s2 = s.substr(0, a - 8);
  cout << s2 << endl;
  return 0;
}
Problem p03577 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//////////////////
// Aditya Kumar //
//  BITS H      //
//////////////////
#define mod 1000000007
#define ss second
#define ff first
#define endl '\n'
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define pb push_back

int32_t main() {
  FastIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  for (int i = 0; i < s.length() - 8; i++)
    cout << s[i];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//////////////////
// Aditya Kumar //
//  BITS H      //
//////////////////
#define mod 1000000007
#define ss second
#define ff first
#define endl '\n'
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define pb push_back

int32_t main() {
  FastIO;
  string s;
  cin >> s;
  for (int i = 0; i < s.length() - 8; i++)
    cout << s[i];
  return 0;
}
Problem p03577 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  for (int i = 0; i = S.size() - 8; i++) {
    cout << S.at(i);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  for (int i = 0; i < S.size() - 8; i++) {
    cout << S.at(i);
  }
}
Problem p03578 (C++)

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

#define For(a, b, c) for (int a = b; a <= c; ++a)
using namespace std;
int n, m;
map<int, int> s;

inline int read() {
  int u = 0;
  char x = getchar();
  while (x < '0' || x > '9')
    x = getchar();
  while (x >= '0' && x <= '9')
    u = (u << 3) + (u << 1) + (x ^ 48), x = getchar();
  return u;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("pro.in", "r", stdin);
  freopen("pro.out", "w", stdout);
#endif
  n = read();
  For(i, 1, n)++ s[read()];
  m = read();
  For(i, 1, m) {
    if (--s[read()] == -1) {
      printf("NO");
      return 0;
    }
  }
  printf("YES");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>

#define For(a, b, c) for (int a = b; a <= c; ++a)
using namespace std;
int n, m;
map<int, int> s;

inline int read() {
  int u = 0;
  char x = getchar();
  while (x < '0' || x > '9')
    x = getchar();
  while (x >= '0' && x <= '9')
    u = (u << 3) + (u << 1) + (x ^ 48), x = getchar();
  return u;
}

int main() {
  n = read();
  For(i, 1, n)++ s[read()];
  m = read();
  For(i, 1, m) {
    if (--s[read()] == -1) {
      printf("NO");
      return 0;
    }
  }
  printf("YES");
  return 0;
}
Problem p03578 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
const int INF = 2147483647;
const int MAX = 100;

int main() {
  int N, M;
  int D[MAX];
  int T[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }

  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> T[i];
  }

  sort(D, D + N);
  sort(T, T + M);

  int d = 0;
  int t = 0;
  while (d < N && t < M) {
    if (D[d] == T[t]) {
      d++;
      t++;
    } else {
      d++;
    }
  }

  if (t == M) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
const int INF = 2147483647;
const int MAX = 200000;

int main() {
  int N, M;
  int D[MAX];
  int T[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }

  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> T[i];
  }

  sort(D, D + N);
  sort(T, T + M);

  int d = 0;
  int t = 0;
  while (d < N && t < M) {
    if (D[d] == T[t]) {
      d++;
      t++;
    } else {
      d++;
    }
  }

  if (t == M) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}