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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g, mn;
vector<int> a, vis;

void dfs(int x, int id) {
  vis[x] = 1;
  mn[id].push_back(a[x]);
  for (auto v : g[x]) {
    if (vis[v])
      continue;
    dfs(v, id);
  }
}

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

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

  a.resize(n);
  g.resize(n);
  mn.resize(n - m);
  vis.resize(n, 0);

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

  if (n < 2 * (n - m - 1)) {
    cout << "Impossible\n";
    return 0;
  }
  if (m == n - 1) {
    cout << 0 << '\n';
    return 0;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;

    g[u].push_back(v);
    g[v].push_back(u);
  }

  long long ans = 0;
  for (int i = 0, cur = 0; i < n; ++i) {
    if (!vis[i]) {
      dfs(i, cur++);
    }
  }

  vector<int> rem;
  for (int i = 0; i < (int)mn.size(); ++i) {
    sort(mn[i].begin(), mn[i].end());
    if ((int)mn[i].size() > 1) {
      rem.push_back(mn[i][1]);
    }
    ans += mn[i][0];
  }

  sort(rem.begin(), rem.end());
  for (int i = 0; i < n - m - 2; ++i) {
    ans += rem[i];
  }

  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g, mn;
vector<int> a, vis;

void dfs(int x, int id) {
  vis[x] = 1;
  mn[id].push_back(a[x]);
  for (auto v : g[x]) {
    if (vis[v])
      continue;
    dfs(v, id);
  }
}

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

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

  a.resize(n);
  g.resize(n);
  mn.resize(n - m);
  vis.resize(n, 0);

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

  if (n < 2 * (n - m - 1)) {
    cout << "Impossible\n";
    return 0;
  }
  if (m == n - 1) {
    cout << 0 << '\n';
    return 0;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;

    g[u].push_back(v);
    g[v].push_back(u);
  }

  long long ans = 0;
  for (int i = 0, cur = 0; i < n; ++i) {
    if (!vis[i]) {
      dfs(i, cur++);
    }
  }

  vector<int> rem;
  for (int i = 0; i < (int)mn.size(); ++i) {
    sort(mn[i].begin(), mn[i].end());
    for (int j = 1; j < (int)mn[i].size(); ++j) {
      rem.push_back(mn[i][j]);
    }
    ans += mn[i][0];
  }

  sort(rem.begin(), rem.end());
  for (int i = 0; i < n - m - 2; ++i) {
    ans += rem[i];
  }

  cout << ans << '\n';
}
Problem p03440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<long long> l, q[100005];
long long n, m, x, y, ans, tot, w[100005], f[100005], cnt[100005];
long long find(long long x) { return f[x] == x ? f[x] : find(f[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    cin >> w[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    f[find(x + 1)] = f[find(x + 1)] == find(y + 1)
                         ? f[find(x + 1)]
                         : f[find(x + 1)] = find(y + 1);
  }
  for (int i = 1; i <= n; i++) {
    q[(cnt[find(i)] = cnt[find(i)] ? cnt[find(i)] : ++tot)].push_back(w[i]);
  }
  if (tot == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= tot; i++) {
    sort(q[i].begin(), q[i].end());
    ans += q[i][0];
    for (int j = 1; j < q[i].size(); j++) {
      l.push_back(q[i][j]);
    }
  }
  sort(l.begin(), l.end());
  if (l.size() < tot - 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < tot - 2; i++) {
    ans += l[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<long long> l, q[100005];
long long n, m, x, y, ans, tot, w[100005], f[100005], cnt[100005];
long long find(long long x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    cin >> w[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    f[find(x + 1)] = f[find(x + 1)] == find(y + 1)
                         ? f[find(x + 1)]
                         : f[find(x + 1)] = find(y + 1);
  }
  for (int i = 1; i <= n; i++) {
    q[(cnt[find(i)] = cnt[find(i)] ? cnt[find(i)] : ++tot)].push_back(w[i]);
  }
  if (tot == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= tot; i++) {
    sort(q[i].begin(), q[i].end());
    ans += q[i][0];
    for (int j = 1; j < q[i].size(); j++) {
      l.push_back(q[i][j]);
    }
  }
  sort(l.begin(), l.end());
  if (l.size() < tot - 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < tot - 2; i++) {
    ans += l[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03440 (C++)

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

using namespace std;

const int maxn = 100100;

struct dsu {
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    int num = 0;
    for (int i = 0; i < n; i++)
      p[i] = num++;
  }

  int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }

  bool unite(int x, int y) {
    // cout<<x<<" "<<y<<" "<<get(x)<<" "<<get(y)<<endl;
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

int a[maxn], n, m;
long long res;
vector<int> all[maxn];
vector<int> min2;

int main() {
  freopen("in.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dsu T(n);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    T.unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    all[T.get(i)].push_back(a[i]);
    // cout<<i<<" "<<T.get(i)<<endl;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (!all[i].empty());
  if (cnt <= 1) {
    cout << 0;
    return 0;
  }
  // cout<<cnt;
  for (int i = 0; i < n; i++)
    if (!all[i].empty()) {
      sort(all[i].begin(), all[i].end());
      res += all[i][0];
      // cout<<all[i][0]<<endl;
      for (int j = 1; j < all[i].size(); j++)
        min2.push_back(all[i][j]);
    }
  if (min2.size() < cnt - 2) {
    cout << "Impossible";
    return 0;
  }
  sort(min2.begin(), min2.end());
  for (int i = 0; i < cnt - 2; i++)
    res += min2[i];
  cout << res;
}


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

using namespace std;

const int maxn = 100100;

struct dsu {
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    int num = 0;
    for (int i = 0; i < n; i++)
      p[i] = num++;
  }

  int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }

  bool unite(int x, int y) {
    // cout<<x<<" "<<y<<" "<<get(x)<<" "<<get(y)<<endl;
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

int a[maxn], n, m;
long long res;
vector<int> all[maxn];
vector<int> min2;

int main() {
  //   freopen("in.txt","r",stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dsu T(n);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    T.unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    all[T.get(i)].push_back(a[i]);
    // cout<<i<<" "<<T.get(i)<<endl;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (!all[i].empty());
  if (cnt <= 1) {
    cout << 0;
    return 0;
  }
  // cout<<cnt;
  for (int i = 0; i < n; i++)
    if (!all[i].empty()) {
      sort(all[i].begin(), all[i].end());
      res += all[i][0];
      // cout<<all[i][0]<<endl;
      for (int j = 1; j < all[i].size(); j++)
        min2.push_back(all[i][j]);
    }
  if (min2.size() < cnt - 2) {
    cout << "Impossible";
    return 0;
  }
  sort(min2.begin(), min2.end());
  for (int i = 0; i < cnt - 2; i++)
    res += min2[i];
  cout << res;
}
Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define L long long

using namespace std;

L n, start, ans = 1, realans = 1000000;
vector<L> v[100010];
L chk[100010];
L dwn[100010];
L bel[100010];

void dfs1(L x) {
  L i;
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      chk[v[x][i]] = 1;
      dwn[x]++;
      dfs1(v[x][i]);
      bel[x] |= bel[v[x][i]];
    }
  }
  if (dwn[x] >= 2)
    bel[x] = 1;
}

void dfs2(L x, L bury) {
  L i, passed = 0;
  // printf("%lld %lld\n",x,bury);
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      if (!bel[v[x][i]]) {
        if (!passed) {
          passed = 1;
        } else {
          // printf("*%lld*\n",x);
          ans++;
          bury = 0;
        }
      } else {
        chk[v[x][i]] = 1;
        dfs2(v[x][i], 1);
        bury = 0;
      }
    }
  }
  if (bury)
    ans++;
}

int main() {
  srand((int)time(NULL));
  scanf("%lld", &n);
  L i;
  for (i = 1; i < n; i++) {
    L s, e;
    scanf("%lld %lld", &s, &e);
    s++;
    e++;
    v[s].push_back(e);
    v[e].push_back(s);
  }
  vector<L> starts;
  for (i = 1; i <= n; i++) {
    if (v[i].size() == 1)
      starts.push_back(i);
  }
  L lop = 400;
  while (lop--) {
    ans = 1;
    for (i = 1; i <= n; i++) {
      chk[i] = dwn[i] = bel[i] = 0;
    }
    start = starts[rand() % starts.size()];
    chk[start] = 1;
    dfs1(start);
    for (i = 1; i <= n; i++) {
      chk[i] = 0;
    }
    chk[start] = 1;
    dfs2(start, 0);
    realans = min(realans, ans);
  }
  printf("%lld", realans);
}

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

using namespace std;

L n, start, ans = 1, realans = 1000000;
vector<L> v[100010];
L chk[100010];
L dwn[100010];
L bel[100010];

void dfs1(L x) {
  L i;
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      chk[v[x][i]] = 1;
      dwn[x]++;
      dfs1(v[x][i]);
      bel[x] |= bel[v[x][i]];
    }
  }
  if (dwn[x] >= 2)
    bel[x] = 1;
}

void dfs2(L x, L bury) {
  L i, passed = 0;
  // printf("%lld %lld\n",x,bury);
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      if (!bel[v[x][i]]) {
        if (!passed) {
          passed = 1;
        } else {
          // printf("*%lld*\n",x);
          ans++;
          bury = 0;
        }
      } else {
        chk[v[x][i]] = 1;
        dfs2(v[x][i], 1);
        bury = 0;
      }
    }
  }
  if (bury)
    ans++;
}

int main() {
  srand((int)time(NULL));
  scanf("%lld", &n);
  L i;
  for (i = 1; i < n; i++) {
    L s, e;
    scanf("%lld %lld", &s, &e);
    s++;
    e++;
    v[s].push_back(e);
    v[e].push_back(s);
  }
  vector<L> starts;
  for (i = 1; i <= n; i++) {
    if (v[i].size() == 1)
      starts.push_back(i);
  }
  L lop = 100;
  while (lop--) {
    ans = 1;
    for (i = 1; i <= n; i++) {
      chk[i] = dwn[i] = bel[i] = 0;
    }
    start = starts[rand() % starts.size()];
    chk[start] = 1;
    dfs1(start);
    for (i = 1; i <= n; i++) {
      chk[i] = 0;
    }
    chk[start] = 1;
    dfs2(start, 0);
    realans = min(realans, ans);
  }
  printf("%lld", realans);
}
Problem p03441 (C++)

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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N;
vint G[11111];
bool used[111111];
int deg[111111];

signed main() {
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  queue<int> que;
  rep(i, N) if (G[i].size() == 1) que.push(i), used[i] = true;
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto u : G[v]) {
      if (used[u])
        continue;
      if (G[u].size() == 2) {
        used[u] = true;
        que.push(u);
      }
      deg[u]++;
    }
  }

  int ans = 0;
  if (count(used, used + N, 1) == N)
    ans = 1;
  else {
    rep(i, N) {
      if (used[i])
        continue;
      ans += max(0ll, deg[i] - 1);
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N;
vint G[111111];
bool used[111111];
int deg[111111];

signed main() {
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }

  queue<int> que;
  rep(i, N) if (G[i].size() == 1) que.push(i), used[i] = true;
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto u : G[v]) {
      if (used[u])
        continue;
      if (G[u].size() == 2) {
        used[u] = true;
        que.push(u);
      }
      deg[u]++;
    }
  }

  int ans = 0;
  if (count(used, used + N, 1) == N)
    ans = 1;
  else {
    rep(i, N) {
      if (used[i])
        continue;
      ans += max(0ll, deg[i] - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03441 (C++)

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

template <typename t> inline t min_(t a, t b) { return a <= b ? a : b; }
template <typename t> inline t max_(t a, t b) { return a >= b ? a : b; }
template <typename t> inline void chkmin(t &a, t b) {
  if (a > b)
    a = b;
}
template <typename t> inline void chkmax(t &a, t b) {
  if (a < b)
    a = b;
}

typedef long long ll;
const int maxn = 1e5 + 10;
int n, rt = -1, ans;
vector<int> g[maxn];

bool dfs(int pos, int fa) {
  int cnt0 = 0, cnt1 = 0;
  for (int i = 0, v; i < g[pos].size(); ++i)
    if ((v = g[pos][i]) != fa) {
      if (dfs(v, pos))
        ++cnt1;
      else
        ++cnt0;
    }
  if (cnt0 > 1) {
    ans += cnt0 - 1;
    return true;
  } else
    return cnt1 > 0;
}

int main() {
  cin >> n;
  for (int i = 1, u, v; i < n; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; ++i)
    if (g[i].size() > 2) {
      rt = i;
      break;
    }
  if (!rt) {
    puts("1");
    return 0;
  }
  dfs(rt, -1);
  printf("%d\n", ans);
  return 0;
}


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

template <typename t> inline t min_(t a, t b) { return a <= b ? a : b; }
template <typename t> inline t max_(t a, t b) { return a >= b ? a : b; }
template <typename t> inline void chkmin(t &a, t b) {
  if (a > b)
    a = b;
}
template <typename t> inline void chkmax(t &a, t b) {
  if (a < b)
    a = b;
}

typedef long long ll;
const int maxn = 1e5 + 10;
int n, rt = -1, ans;
vector<int> g[maxn];

bool dfs(int pos, int fa) {
  int cnt0 = 0, cnt1 = 0;
  for (int i = 0, v; i < g[pos].size(); ++i)
    if ((v = g[pos][i]) != fa) {
      if (dfs(v, pos))
        ++cnt1;
      else
        ++cnt0;
    }
  if (cnt0 > 1) {
    ans += cnt0 - 1;
    return true;
  } else
    return cnt1 > 0;
}

int main() {
  cin >> n;
  for (int i = 1, u, v; i < n; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; ++i)
    if (g[i].size() > 2) {
      rt = i;
      break;
    }
  if (rt == -1) {
    puts("1");
    return 0;
  }
  dfs(rt, -1);
  printf("%d\n", ans);
  return 0;
}
Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, n, m) for (int i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = std::int_fast64_t;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-15;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << to_string(p.first) << " " << to_string(p.second);
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  REP(i, v.size()) {
    if (i)
      os << " ";
    os << to_string(v[i]);
  }
  return os;
}

vector<vector<int>> t;
vector<int> dp;

bool is_path(int v, int p) {
  bool ret = true;
  if ((int)t[v].size() >= 3)
    ret = false;
  for (auto c : t[v]) {
    if (c == p)
      continue;
    ret = ret & is_path(c, v);
  }
  return ret;
}

int dfs(int v, int p) {
  int ret = 0;
  bool b = false;
  for (auto c : t[v]) {
    if (c == p)
      continue;
    if (is_path(c, v)) {
      if (!b)
        b = true;
      else
        ret++;
    } else {
      ret += dfs(c, v);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  t.resize(n);
  dp.resize(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    t[a].pb(b);
    t[b].pb(a);
  }
  int root = -1;
  REP(i, n) if ((int)t[i].size() >= 3) root = i;
  if (root == -1)
    cout << 1 << endl;
  else
    cout << dfs(root, -1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, n, m) for (int i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = std::int_fast64_t;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-15;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << to_string(p.first) << " " << to_string(p.second);
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  REP(i, v.size()) {
    if (i)
      os << " ";
    os << to_string(v[i]);
  }
  return os;
}

vector<vector<int>> t;
vector<int> dp;

bool is_path(int v, int p) {
  bool ret = true;
  if ((int)t[v].size() >= 3)
    return false;
  for (auto c : t[v]) {
    if (c == p)
      continue;
    ret = ret & is_path(c, v);
  }
  return ret;
}

int dfs(int v, int p) {
  int ret = 0;
  bool b = false;
  for (auto c : t[v]) {
    if (c == p)
      continue;
    if (is_path(c, v)) {
      if (!b)
        b = true;
      else
        ret++;
    } else {
      ret += dfs(c, v);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  t.resize(n);
  dp.resize(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    t[a].pb(b);
    t[b].pb(a);
  }
  int root = -1;
  REP(i, n) if ((int)t[i].size() >= 3) root = i;
  if (root == -1)
    cout << 1 << endl;
  else
    cout << dfs(root, -1) << endl;
  return 0;
}
Problem p03441 (C++)

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

using namespace std;

#define N 120000

int n, x, y, tot, f[N], head[N], sz[N], d[N];
struct edge {
  int v, nxt;
} e[N];

void add(int x, int y) {
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}

void dfs(int u, int fa) {
  int sum = 0;
  for (int i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa) {
      dfs(v, u);
      ++sz[u];
      if (f[v])
        sum++;
      f[u] += f[v];
    }
  if (sz[u] > sum)
    f[u] += sz[u] - sum - 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    ++x;
    ++y;
    add(x, y);
    add(y, x);
    ++d[x];
    ++d[y];
  }
  for (int i = 1; i < n; i++)
    if (d[i] >= 3) {
      dfs(i, 0);
      printf("%d\n", f[i]);
      return 0;
    }
  puts("1");

  return 0;
}


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

using namespace std;

#define N 300000

int n, x, y, tot, f[N], head[N], sz[N], d[N];
struct edge {
  int v, nxt;
} e[N];

void add(int x, int y) {
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}

void dfs(int u, int fa) {
  int sum = 0;
  for (int i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa) {
      dfs(v, u);
      ++sz[u];
      if (f[v])
        sum++;
      f[u] += f[v];
    }
  if (sz[u] > sum)
    f[u] += sz[u] - sum - 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    ++x;
    ++y;
    add(x, y);
    add(y, x);
    ++d[x];
    ++d[y];
  }
  for (int i = 1; i < n; i++)
    if (d[i] >= 3) {
      dfs(i, 0);
      printf("%d\n", f[i]);
      return 0;
    }
  puts("1");

  return 0;
}
Problem p03441 (C++)

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

int n;
vector<vector<int>> e;

vector<int> dp;

void dfs(int i, int p) {
  dp[i] = 0;
  bool F = 0;
  for (int x = 0; x < e[i].size(); x++) {
    int j = e[i][x];
    if (j == p)
      continue;
    dfs(j, i);
    dp[i] += dp[j];
    if (dp[j] == 0) {
      if (!F)
        F = 1;
      else
        dp[i]++;
    }
  }
}

int main() {
  scanf("%d", &n);
  e = vector<vector<int>>(n);
  for (int t = 0; t < n - 1; t++) {
    int a, b;
    scanf("%d%d", &a, &b);
    e[a].push_back(b);
    e[b].push_back(a);
  }
  dp = vector<int>(n);
  int r = -1;
  for (int i = 0; i < n; i++) {
    if (e[i].size() > 2) {
      r = i;
      break;
    }
  }
  dfs(r, -1);
  printf("%d\n", dp[r]);
  return 0;
}


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

int n;
vector<vector<int>> e;

vector<int> dp;

void dfs(int i, int p) {
  dp[i] = 0;
  bool F = 0;
  for (int x = 0; x < e[i].size(); x++) {
    int j = e[i][x];
    if (j == p)
      continue;
    dfs(j, i);
    dp[i] += dp[j];
    if (dp[j] == 0) {
      if (!F)
        F = 1;
      else
        dp[i]++;
    }
  }
}

int main() {
  scanf("%d", &n);
  e = vector<vector<int>>(n);
  for (int t = 0; t < n - 1; t++) {
    int a, b;
    scanf("%d%d", &a, &b);
    e[a].push_back(b);
    e[b].push_back(a);
  }
  dp = vector<int>(n);
  int r = -1;
  for (int i = 0; i < n; i++) {
    if (e[i].size() > 2) {
      r = i;
      break;
    }
  }
  if (r == -1) {
    printf("1\n");
    return 0;
  }
  dfs(r, -1);
  printf("%d\n", dp[r]);
  return 0;
}
Problem p03442 (C++)

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

using namespace std;

typedef vector<int> Vec;

vector<int> gay;

int Dfs(int msk) {
  if (!msk)
    return 0;
  assert((msk & -msk) != msk);
  int &res = gay[msk];
  res = 2233;
  for (int i = 0; i < 16; ++i) {
    if (msk >> i & 1) {
      for (int j = i + 1; j < 16; ++j) {
        if (msk >> j & 1) {
          int g = i ^ j;
          res = min(res, 1 + (msk >> g & 1) +
                             Dfs(msk ^ (1 << i) ^ (1 << j) ^ (1 << g)));
        }
      }
    }
  }
  return res;
}

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

  int n;
  cin >> n;
  Vec w(n), cnt(16);

  for (int i = 1, x, y, z; i < n; ++i) {
    cin >> x >> y >> z;
    w[x] ^= z;
    w[y] ^= z;
  }
  for (int i = 0; i < n; ++i) {
    ++cnt[w[i]];
  }

  int ans = 0, msk = 0;
  for (int i = 1; i < 16; ++i) {
    ans += cnt[i] / 2;
    cnt[i] %= 2;
    msk |= cnt[i] << i;
  }

  gay.resize(1 << 16);
  cout << ans + Dfs(msk) << endl;

  return 0;
}


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

using namespace std;

typedef vector<int> Vec;

vector<int> gay;

int Dfs(int msk) {
  if (!msk)
    return 0;
  assert((msk & -msk) != msk);
  int &res = gay[msk];
  if (res)
    return res;
  res = 2233;
  for (int i = 0; i < 16; ++i) {
    if (msk >> i & 1) {
      for (int j = i + 1; j < 16; ++j) {
        if (msk >> j & 1) {
          int g = i ^ j;
          res = min(res, 1 + (msk >> g & 1) +
                             Dfs(msk ^ (1 << i) ^ (1 << j) ^ (1 << g)));
        }
      }
    }
  }
  return res;
}

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

  int n;
  cin >> n;
  Vec w(n), cnt(16);

  for (int i = 1, x, y, z; i < n; ++i) {
    cin >> x >> y >> z;
    w[x] ^= z;
    w[y] ^= z;
  }
  for (int i = 0; i < n; ++i) {
    ++cnt[w[i]];
  }

  int ans = 0, msk = 0;
  for (int i = 1; i < 16; ++i) {
    ans += cnt[i] / 2;
    cnt[i] %= 2;
    msk |= cnt[i] << i;
  }

  gay.resize(1 << 16);
  cout << ans + Dfs(msk) << endl;

  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
void chkmin(int &x, int y) { x = x < y ? x : y; }
int beg[maxn], tto[maxn << 1], nex[maxn << 1], w[maxn << 1], e;
void putin(int s, int t, int v) {
  tto[++e] = t;
  nex[e] = beg[s];
  beg[s] = e;
  w[e] = v;
}
int cnt[20], ans;
void dfs(int u, int fa, int fv) {
  int sum = fv;
  for (int i = beg[u]; i; i = nex[i]) {
    if (tto[i] == fa)
      continue;
    dfs(tto[i], u, w[i]);
    sum ^= w[i];
  }
  if (sum == 0)
    return;
  ans += cnt[sum];
  cnt[sum] ^= 1;
}
int dp[1 << 15];
int popcnt[1 << 15];
int main() {
  //	freopen("F.in","r",stdin);
  int n;
  scanf("%d", &n);
  int s, t, v;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &s, &t, &v);
    s++, t++;
    putin(s, t, v);
    putin(t, s, v);
  }
  dfs(1, 0, 0);
  for (int i = 1; i < (1 << 15); i++)
    popcnt[i] = popcnt[i >> 1] + (i & 1);

  int val;
  for (int i = 1; i < (1 << 15); i++) {
    dp[i] = popcnt[i];
    val = 0;
    for (int j = 0; j < 15; j++)
      if (i & (1 << j))
        val ^= j + 1;
    if (val == 0)
      dp[i]--;
  }
  for (int i = 1; i < (1 << 15); i++)
    for (int j = i; j; j = i & (j - 1))
      chkmin(dp[i], dp[j] + dp[i ^ j]);
  val = 0;
  for (int i = 1; i <= 15; i++)
    if (cnt[i])
      val |= 1 << (i - 1);
  ans = ans + dp[val];
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
void chkmin(int &x, int y) { x = x < y ? x : y; }
int beg[maxn], tto[maxn << 1], nex[maxn << 1], w[maxn << 1], e;
void putin(int s, int t, int v) {
  tto[++e] = t;
  nex[e] = beg[s];
  beg[s] = e;
  w[e] = v;
}
int cnt[20], ans;
void dfs(int u, int fa, int fv) {
  int sum = fv;
  for (int i = beg[u]; i; i = nex[i]) {
    if (tto[i] == fa)
      continue;
    dfs(tto[i], u, w[i]);
    sum ^= w[i];
  }
  if (sum == 0)
    return;
  ans += cnt[sum];
  cnt[sum] ^= 1;
}
int dp[1 << 15];
int popcnt[1 << 15];
int main() {
  //	freopen("F.in","r",stdin);
  int n;
  scanf("%d", &n);
  int s, t, v;
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &s, &t, &v);
    s++, t++;
    putin(s, t, v);
    putin(t, s, v);
  }
  dfs(1, 0, 0);
  for (int i = 1; i < (1 << 15); i++)
    popcnt[i] = popcnt[i >> 1] + (i & 1);

  int val;
  for (int i = 1; i < (1 << 15); i++) {
    dp[i] = popcnt[i];
    val = 0;
    for (int j = 0; j < 15; j++)
      if (i & (1 << j))
        val ^= j + 1;
    if (val == 0)
      dp[i]--;
  }
  for (int i = 1; i < (1 << 15); i++)
    for (int j = i; j; j = i & (j - 1))
      chkmin(dp[i], dp[j] + dp[i ^ j]);
  val = 0;
  for (int i = 1; i <= 15; i++)
    if (cnt[i])
      val |= 1 << (i - 1);
  ans = ans + dp[val];
  printf("%d\n", ans);
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int cnt[20], v[20];
int dp[N];
int dfs(int s) {
  if (s == 0)
    return 0;
  if (dp[s] != -1)
    return dp[s];
  dp[s] = 0x3f3f3f3f;
  for (int i = 0; i < 16; i++) {
    if (s >> i & 1) {
      for (int j = 0; j < 16; j++) {
        if (s >> j & 1) {
          int x = j ^ i;
          int ns = s ^ (1 << j) ^ (1 << i) ^ (1 << x);
          if (__builtin_popcount(ns) < __builtin_popcount(s))
            dp[s] = min(dp[s], dfs(ns) + 1 + ((s & (1 << x)) == (1 << x)));
        }
      }
    }
  }
  return dp[s];
}
int main() {
  int n;
  cin >> n;
  for (int i = 1, x, y, z; i < n; i++) {
    cin >> x >> y >> z;
    v[x] ^= z;
    v[y] ^= z;
  }
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++)
    if (v[i])
      cnt[v[i]]++;
  int ans = 0, st = 0;
  ;
  for (int i = 1; i < 16; i++) {
    ans += cnt[i] / 2;
    st |= (cnt[i] & 1) << i;
  }
  ans += dfs(st);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int cnt[20], v[N];
int dp[1 << 20];
int dfs(int s) {
  if (s == 0)
    return 0;
  if (dp[s] != -1)
    return dp[s];
  dp[s] = 0x3f3f3f3f;
  for (int i = 0; i < 16; i++) {
    if (s >> i & 1) {
      for (int j = 0; j < 16; j++) {
        if (s >> j & 1) {
          int x = j ^ i;
          int ns = s ^ (1 << j) ^ (1 << i) ^ (1 << x);
          if (__builtin_popcount(ns) < __builtin_popcount(s))
            dp[s] = min(dp[s], dfs(ns) + 1 + ((s & (1 << x)) == (1 << x)));
        }
      }
    }
  }
  return dp[s];
}
int main() {
  int n;
  cin >> n;
  for (int i = 1, x, y, z; i < n; i++) {
    cin >> x >> y >> z;
    v[x] ^= z;
    v[y] ^= z;
  }
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++)
    if (v[i])
      cnt[v[i]]++;
  int ans = 0, st = 0;
  ;
  for (int i = 1; i < 16; i++) {
    ans += cnt[i] / 2;
    st |= (cnt[i] & 1) << i;
  }
  ans += dfs(st);
  cout << ans << endl;
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(998244353);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

int a[100100], cnt[16], memo[1 << 16];
bool done[1 << 16];

int solve(int st) {
  int i, j, k;
  if (done[st]) {
    return memo[st];
  }
  done[st] = 0;
  if (!st) {
    return memo[st] = 0;
  }
  memo[st] = INF;
  for (i = 1; i < 16; ++i) {
    if (!(st & (1 << i))) {
      continue;
    }
    for (j = 1; j < i; ++j) {
      if (!(st & (1 << j))) {
        continue;
      }
      k = i ^ j;
      if (st & (1 << k)) {
        memo[st] =
            min(memo[st], solve(st - (1 << i) - (1 << j) - (1 << k)) + 2);
      } else {
        memo[st] =
            min(memo[st], solve(st - (1 << i) - (1 << j) + (1 << k)) + 1);
      }
    }
  }
  return memo[st];
}

int main(void) {
  int n, i, x, y, z, ans = 0, left = 0;
  cin >> n;
  for (i = 1; i < n; ++i) {
    cin >> x >> y >> z;
    a[x] ^= z;
    a[y] ^= z;
  }
  for (i = 0; i < n; ++i) {
    ++cnt[a[i]];
  }
  for (i = 1; i < 16; ++i) {
    ans += cnt[i] / 2;
    if (cnt[i] % 2) {
      left += (1 << i);
    }
  }
  ans += solve(left);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(998244353);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

int a[100100], cnt[16], memo[1 << 16];
bool done[1 << 16];

int solve(int st) {
  int i, j, k;
  if (done[st]) {
    return memo[st];
  }
  done[st] = true;
  if (!st) {
    return memo[st] = 0;
  }
  memo[st] = INF;
  for (i = 1; i < 16; ++i) {
    if (!(st & (1 << i))) {
      continue;
    }
    for (j = 1; j < i; ++j) {
      if (!(st & (1 << j))) {
        continue;
      }
      k = i ^ j;
      if (st & (1 << k)) {
        memo[st] =
            min(memo[st], solve(st - (1 << i) - (1 << j) - (1 << k)) + 2);
      } else {
        memo[st] =
            min(memo[st], solve(st - (1 << i) - (1 << j) + (1 << k)) + 1);
      }
    }
  }
  return memo[st];
}

int main(void) {
  int n, i, x, y, z, ans = 0, left = 0;
  cin >> n;
  for (i = 1; i < n; ++i) {
    cin >> x >> y >> z;
    a[x] ^= z;
    a[y] ^= z;
  }
  for (i = 0; i < n; ++i) {
    ++cnt[a[i]];
  }
  for (i = 1; i < 16; ++i) {
    ans += cnt[i] / 2;
    if (cnt[i] % 2) {
      left += (1 << i);
    }
  }
  ans += solve(left);
  cout << ans << endl;
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxs = 25;
const int maxn = 1e5 + 5;
int n, ans, cnt;
int sum[maxs], pw[maxs], F[maxs], G[maxs], B[maxs];
int A[maxn], id[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int a = 0, b = 0, c = 0;
    scanf("%d%d%d", &a, &b, &c);
    A[a + 1] ^= c, A[b + 1] ^= c;
  }
  for (int i = 1; i <= n; ++i)
    ++sum[A[i]];
  for (int i = 1; i <= 15; ++i) {
    ans += sum[i] / 2;
    sum[i] %= 2;
    if (sum[i])
      B[++cnt] = i;
  }
  pw[0] = 1;
  for (int i = 1; i <= cnt; ++i)
    pw[i] = pw[i - 1] << 1, id[pw[i]] = i;
  for (int mask = 1; mask < pw[cnt]; ++mask) {
    F[mask] = 10000;
    for (int i = mask; i; i -= i & -i) {
      int res = mask ^ (i & -i), s = id[i & -i];
      G[mask] = G[res] ^ B[s + 1];
      if (!G[res])
        F[mask] = min(F[mask], F[res]);
      else
        F[mask] = min(F[mask], F[res] + 1);
      // cout<<mask<<" "<<res<<" "<<B[s+1]<<" "<<s<<" "<<F[mask]<<"
      // "<<G[mask]<<endl;
    }
  }
  printf("%d\n", ans + F[pw[cnt] - 1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxm = 25;
const int maxs = (1 << 15) + 1;
const int maxn = 2e5 + 5;
int n, ans, cnt;
int sum[maxs], pw[maxs], F[maxs], G[maxs], B[maxs];
int A[maxn], id[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int a = 0, b = 0, c = 0;
    scanf("%d%d%d", &a, &b, &c);
    A[a + 1] ^= c, A[b + 1] ^= c;
  }
  for (int i = 1; i <= n; ++i)
    ++sum[A[i]];
  for (int i = 1; i <= 15; ++i) {
    ans += sum[i] / 2;
    sum[i] %= 2;
    if (sum[i])
      B[++cnt] = i;
  }
  pw[0] = 1;
  for (int i = 1; i <= cnt; ++i)
    pw[i] = pw[i - 1] << 1, id[pw[i]] = i;
  for (int mask = 1; mask < pw[cnt]; ++mask) {
    F[mask] = 10000;
    for (int i = mask; i; i -= i & -i) {
      int res = mask ^ (i & -i), s = id[i & -i];
      G[mask] = G[res] ^ B[s + 1];
      if (!G[res])
        F[mask] = min(F[mask], F[res]);
      else
        F[mask] = min(F[mask], F[res] + 1);
      // cout<<mask<<" "<<res<<" "<<B[s+1]<<" "<<s<<" "<<F[mask]<<"
      // "<<G[mask]<<endl;
    }
  }
  printf("%d\n", ans + F[pw[cnt] - 1]);
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define MIN(x, y) x = min(x, y)
using namespace std;

const int N = 100005;
const int inf = 1000000000;

int n, a[N], bin[20], cnt[70005], f[70005], w[20];
vector<int> vec[16];

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

void dp(int S) {
  for (int i = 0; i < bin[16]; i++)
    cnt[i] = cnt[i >> 1] + (i & 1), vec[cnt[i]].push_back(i), f[i] = inf;
  f[S] = 0;
  for (int i = 16; i > 0; i--)
    for (int j = 0; j < vec[i].size(); j++) {
      int s = vec[i][j];
      if (f[s] == inf)
        continue;
      for (int x = 0; x < 16; x++)
        for (int y = 0; y < 16; y++) {
          if (!(s & bin[x]) || !(s & bin[y]) || x == y)
            continue;
          MIN(f[s ^ bin[x] ^ bin[x ^ y] ^ bin[y]],
              f[s] + 1 + ((s & bin[x ^ y]) > 0));
        }
    }
}

int main() {
  bin[0] = 1;
  for (int i = 1; i <= 16; i++)
    bin[i] = bin[i - 1] * 2;
  n = read();
  for (int i = 1; i < n; i++) {
    int x = read(), y = read(), z = read();
    a[x + 1] ^= z;
    a[y + 1] ^= z;
  }
  for (int i = 1; i <= n; i++)
    w[a[i]]++;
  int S = 0, ans = 0;
  for (int i = 1; i <= 15; i++)
    ans += w[i] / 2, w[i] %= 2, S += bin[i] * w[i];
  dp(S);
  printf("%d", ans + f[0]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define MIN(x, y) x = min(x, y)
using namespace std;

const int N = 100005;
const int inf = 1000000000;

int n, a[N], bin[20], cnt[70005], f[70005], w[20];
vector<int> vec[20];

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

void dp(int S) {
  for (int i = 0; i < bin[16]; i++)
    cnt[i] = cnt[i >> 1] + (i & 1), vec[cnt[i]].push_back(i), f[i] = inf;
  f[S] = 0;
  for (int i = 16; i > 0; i--)
    for (int j = 0; j < vec[i].size(); j++) {
      int s = vec[i][j];
      if (f[s] == inf)
        continue;
      for (int x = 0; x < 16; x++)
        for (int y = 0; y < 16; y++) {
          if (!(s & bin[x]) || !(s & bin[y]) || x == y)
            continue;
          MIN(f[s ^ bin[x] ^ bin[x ^ y] ^ bin[y]],
              f[s] + 1 + ((s & bin[x ^ y]) > 0));
        }
    }
}

int main() {
  bin[0] = 1;
  for (int i = 1; i <= 16; i++)
    bin[i] = bin[i - 1] * 2;
  n = read();
  for (int i = 1; i < n; i++) {
    int x = read(), y = read(), z = read();
    a[x + 1] ^= z;
    a[y + 1] ^= z;
  }
  for (int i = 1; i <= n; i++)
    w[a[i]]++;
  int S = 0, ans = 0;
  for (int i = 1; i <= 15; i++)
    ans += w[i] / 2, w[i] %= 2, S += bin[i] * w[i];
  dp(S);
  printf("%d", ans + f[0]);
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 16
using namespace std;
int n, x, y, z, ans, val[1 << (N + 1)], cnt[N], dp[1 << N];
vector<int> G[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    x++;
    y++;
    val[x] ^= z;
    val[y] ^= z;
  }
  for (int i = 1; i <= n; i++)
    cnt[val[i]]++;
  for (int i = 1; i <= 15; i++)
    ans += cnt[i] / 2, val[i] = cnt[i] % 2;
  int now = 0, tot = 0;
  for (int i = 1; i <= 15; i++)
    if (val[i])
      now |= (1 << i), tot++;
  memset(dp, 127, sizeof(dp));
  int inf = dp[now];
  dp[now] = 0;
  for (int i = 0; i < (1 << 16); i++)
    G[__builtin_popcount(i)].push_back(i);
  for (int i = tot; i; i--) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      int S = G[i][j];
      if (dp[S] == inf)
        continue;
      for (int x = 1; x <= 15; x++) {
        for (int y = 1; y <= 15; y++) {
          if (x == y || (!(S >> x & 1)) || (!(S >> y & 1)))
            continue;
          int tmp = x ^ y;
          int S1 = S ^ (1 << x) ^ (1 << y);
          if (S1 & (1 << tmp))
            dp[S1 ^ (1 << tmp)] = min(dp[S1 ^ (1 << tmp)], dp[S] + 2);
          else
            dp[S1 | (1 << tmp)] = min(dp[S1 | (1 << tmp)], dp[S] + 1);
        }
      }
    }
  }
  // cerr<<inf<<endl;
  printf("%d\n", min(dp[1], dp[0]) + ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 16
using namespace std;
int n, x, y, z, ans, val[1 << (N + 1)], cnt[N * 2], dp[1 << N];
vector<int> G[N * 2];
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    x++;
    y++;
    val[x] ^= z;
    val[y] ^= z;
  }
  for (int i = 1; i <= n; i++)
    cnt[val[i]]++;
  for (int i = 1; i <= 15; i++)
    ans += cnt[i] / 2, val[i] = cnt[i] % 2;
  int now = 0, tot = 0;
  for (int i = 1; i <= 15; i++)
    if (val[i])
      now |= (1 << i), tot++;
  memset(dp, 127, sizeof(dp));
  int inf = dp[now];
  dp[now] = 0;
  for (int i = 0; i < (1 << 16); i++)
    G[__builtin_popcount(i)].push_back(i);
  for (int i = tot; i; i--) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      int S = G[i][j];
      if (dp[S] == inf)
        continue;
      for (int x = 1; x <= 15; x++) {
        for (int y = 1; y <= 15; y++) {
          if (x == y || (!(S >> x & 1)) || (!(S >> y & 1)))
            continue;
          int tmp = x ^ y;
          int S1 = S ^ (1 << x) ^ (1 << y);
          if (S1 & (1 << tmp))
            dp[S1 ^ (1 << tmp)] = min(dp[S1 ^ (1 << tmp)], dp[S] + 2);
          else
            dp[S1 | (1 << tmp)] = min(dp[S1 | (1 << tmp)], dp[S] + 1);
        }
      }
    }
  }
  // cerr<<inf<<endl;
  printf("%d\n", min(dp[1], dp[0]) + ans);
  return 0;
}
Problem p03442 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ri rd<int>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
const int maxN = 1e5 + 7;
const int maxS = (1 << 15) + 3;

template <class T> T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return f ? x : -x;
}

int n, m;
int a[maxN];
bool ok[maxS];
int f[maxS];

int main() {

  m = n = ri();
  For(i, 1, n) {
    int x = ri() + 1, y = ri() + 1, d = ri();
    a[x] ^= d;
    a[y] ^= d;
  }

  sort(a + 1, a + n + 1, greater<int>());
  int res = 0, mask = 0;
  for (; n > 0 && a[n] == 0; --n)
    ++res;
  while (n > 0) {
    for (; n >= 2 && a[n] == a[n - 1]; n -= 2)
      ++res;
    if (n > 1)
      mask |= 1 << (a[n--] - 1);
  }

  for (int s = mask; s; (--s) &= mask) {
    int tp = 0;
    rep(i, 1, 15) if (s >> (i - 1) & 1) tp ^= i;
    ok[s] = tp == 0;
  }
  ok[0] = true;
  rep(s, 1, mask) if ((mask & s) == s && ok[s]) {
    for (int t = s; t; (--t) &= s)
      if (ok[t] && ok[s ^ t]) {
        f[s] = max(f[s], f[s ^ t] + 1);
      }
  }
  res += f[mask];
  printf("%d\n", m - res);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ri rd<int>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
const int maxN = 1e5 + 7;
const int maxS = (1 << 15) + 3;

template <class T> T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return f ? x : -x;
}

int n, m;
int a[maxN];
bool ok[maxS];
int f[maxS];

int main() {

  m = n = ri();
  For(i, 1, n) {
    int x = ri() + 1, y = ri() + 1, d = ri();
    a[x] ^= d;
    a[y] ^= d;
  }

  sort(a + 1, a + n + 1, greater<int>());
  int res = 0, mask = 0;
  for (; n > 0 && a[n] == 0; --n)
    ++res;
  while (n > 0) {
    for (; n >= 2 && a[n] == a[n - 1]; n -= 2)
      ++res;
    if (n >= 1)
      mask |= 1 << (a[n--] - 1);
  }

  for (int s = mask; s; (--s) &= mask) {
    int tp = 0;
    rep(i, 1, 15) if (s >> (i - 1) & 1) tp ^= i;
    ok[s] = tp == 0;
  }
  ok[0] = true;
  rep(s, 1, mask) if ((mask & s) == s && ok[s]) {
    for (int t = s; t; (--t) &= s)
      if (ok[t] && ok[s ^ t]) {
        f[s] = max(f[s], f[s ^ t] + 1);
      }
  }
  res += f[mask];
  printf("%d\n", m - res);

  return 0;
}
Problem p03444 (C++)

Buggy submission (status: Runtime Error):
/**
 *    author:  tourist
 *    created: 09.12.2019 15:16:41
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n);
  p[0] = -1;
  for (int i = 1; i < n; i++) {
    cin >> p[i];
  }
  vector<int> d(n);
  for (int i = 1; i < n; i++) {
    d[i] = d[p[i]] + 1;
  }
  vector<vector<int>> is_parent(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int x = i;
    while (x >= 0) {
      is_parent[x][i] = 1;
      x = p[x];
    }
  }
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> ret;
  while (true) {
    int to = 0;
    for (int i = 1; i < n; i++) {
      if (is_parent[a[0]][i] && (!is_parent[a[i]][i] || d[a[i]] < d[a[0]])) {
        if (d[i] > d[to]) {
          to = i;
        }
      }
    }
    if (to == 0) {
      break;
    }
    ret.push_back(to);
    int v = to;
    while (v > 0) {
      swap(a[0], a[v]);
      v = p[v];
    }
  }
  assert(ret.size() <= 7500);
  cout << ret.size() << '\n';
  for (int x : ret) {
    cout << x << '\n';
  }
  return 0;
}


Fixed submission:
/**
 *    author:  tourist
 *    created: 09.12.2019 15:16:41
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n);
  p[0] = -1;
  for (int i = 1; i < n; i++) {
    cin >> p[i];
  }
  vector<int> d(n);
  for (int i = 1; i < n; i++) {
    d[i] = d[p[i]] + 1;
  }
  vector<vector<int>> is_parent(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int x = i;
    while (x >= 0) {
      is_parent[x][i] = 1;
      x = p[x];
    }
  }
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> ret;
  while (true) {
    int to = 0;
    for (int i = 1; i < n; i++) {
      if (is_parent[a[0]][i] && (!is_parent[a[i]][i] || d[a[i]] < d[a[0]])) {
        if (d[i] > d[to]) {
          to = i;
        }
      }
    }
    if (to == 0) {
      break;
    }
    ret.push_back(to);
    int v = to;
    while (v > 0) {
      swap(a[0], a[v]);
      v = p[v];
    }
  }
  assert(ret.size() <= 8750);
  cout << ret.size() << '\n';
  for (int x : ret) {
    cout << x << '\n';
  }
  return 0;
}
Problem p03447 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, a, b;
  cin >> x >> a >> b;
  x -= a;

  while (x >= 0) {
    x -= b;
    return x;
  }
  cout << x << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, a, b;
  cin >> x >> a >> b;
  x -= a;
  x %= b;
  cout << x << endl;
}
Problem p03447 (C++)

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

int main() {
  int X, A, B;
  cin >> X >> A >> B;

  return (X - A) % B;
}

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

int main() {
  int X, A, B;
  cin >> X >> A >> B;

  cout << (X - A) % B << endl;
}
Problem p03447 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int A, B, X, Y, Z;
  scanf("%d,%d,%d", &X, &A, &B);
  Y = X - A;
  Z = Y % B;
  printf("%d\n", Z);
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int A, B, X, Y, Z;
  scanf("%d%d%d", &X, &A, &B);
  Y = X - A;
  Z = Y % B;
  printf("%d\n", Z);
  return 0;
}
Problem p03447 (C++)

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

int main(void) {
  int X, A, B;
  cin >> X;

  int money = X;

  money -= A;

  while (money >= B) {
    money -= B;
  }

  cout << money << endl;
}

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

int main(void) {
  int X, A, B;
  cin >> X >> A >> B;

  int money = X;

  money -= A;

  while (money >= B) {
    money -= B;
  }

  cout << money << endl;
}
Problem p03447 (C++)

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

int main() {
  int X, A, B;
  cin >> X, A, B;

  int ans = (X - A) % B;

  cout << ans << endl;
}

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

int main() {
  int X, A, B;
  cin >> X >> A >> B;

  int ans = (X - A) % B;

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

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

int main() {
  int N, ar[2][100], sum = 0, max = -1;
  cin >> N;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < N; j++) {
      cin >> ar[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += ar[0][j];
    }
    for (int k = i; k < N; k++) {
      sum += ar[1][k];
    }
    if (sum > max) {
      max = sum;
    }
  }
  cout << max << endl;
  return 0;
}


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

int main() {
  int x, a, b;
  cin >> x >> a >> b;
  cout << (x - a) % b << endl;
  return 0;
}
Problem p03447 (C++)

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

int main() {
  int x, a, b;
  cin >> x, a, b;
  cout << (x - a) % b << endl;
}

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

int main() {
  int x, a, b;
  cin >> x >> a >> b;
  cout << (x - a) % b << endl;
}