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

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int N, K;
map<int, vector<int>> g;
vector<pair<int, int>> e;

void dfs(int s, int &cnt, set<int> &v, int ignore = -1, int d = 0) {
  v.insert(s);
  if (d > K / 2)
    ++cnt;
  for (const auto &i : g[s]) {
    if (v.find(i) != v.end() || i == ignore)
      continue;
    dfs(i, cnt, v, ignore, d + 1);
  }
}

int main() {
  scanf("%d%d", &N, &K);

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
    e.push_back({a, b});
  }

  int ans = 2001;
  if (K % 2 == 0) {
    for (int i = 1; i <= N; ++i) {
      int cnt = 0;
      set<int> v;
      dfs(i, cnt, v);
      ans = min(ans, cnt);
    }
  } else {
    --K;
    for (const auto &i : e) {
      int cnt = 0;
      set<int> v;
      dfs(i.first, cnt, v, i.second);
      v.clear();
      dfs(i.second, cnt, v, i.first);
      ans = min(ans, cnt);
    }
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int N, K;
vector<int> g[2001];
vector<pair<int, int>> e;

void dfs(int s, int &cnt, set<int> &v, int ignore = -1, int d = 0) {
  v.insert(s);
  if (d > K / 2)
    ++cnt;
  for (const auto &i : g[s]) {
    if (v.find(i) != v.end() || i == ignore)
      continue;
    dfs(i, cnt, v, ignore, d + 1);
  }
}

int main() {
  scanf("%d%d", &N, &K);

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
    e.push_back({a, b});
  }

  int ans = 2001;
  if (K % 2 == 0) {
    for (int i = 1; i <= N; ++i) {
      int cnt = 0;
      set<int> v;
      dfs(i, cnt, v);
      ans = min(ans, cnt);
    }
  } else {
    --K;
    for (const auto &i : e) {
      int cnt = 0;
      set<int> v;
      dfs(i.first, cnt, v, i.second);
      v.clear();
      dfs(i.second, cnt, v, i.first);
      ans = min(ans, cnt);
    }
  }
  printf("%d\n", ans);
}
Problem p04049 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
using namespace std;
#define REP(i, st, ed) for (int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (int i = st, i##end = ed; i >= i##end; --i)
#define Runtime 1.0 * clock() / CLOCKS_PER_SEC
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
const int maxn = 2005;
int n, m, eu[maxn], ev[maxn];
int Begin[maxn], Next[maxn << 1], to[maxn << 1], e;
int sum = 0, ans = 1e9;
void add_edge(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
void dfs(int u, int fa, int dep) {
  ++sum;
  if (dep == m)
    return;
  for (int i = Begin[u]; i; i = Next[i])
    if (to[i] != fa)
      dfs(to[i], u, dep + 1);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
#endif
  int u, v;
  n = read(), m = read();
  REP(i, 1, n - 1) {
    u = read(), v = read();
    add_edge(u, v), add_edge(v, u);
    eu[i] = u, ev[i] = v;
  }
  if (m & 1) {
    m >>= 1;
    REP(i, 1, n - 1) {
      sum = 0;
      dfs(eu[i], ev[i], 0);
      dfs(ev[i], eu[i], 0);
      ans = min(ans, n - sum);
    }
  } else {
    m >>= 1;
    REP(i, 1, n) {
      sum = 0;
      dfs(i, 0, 0);
      ans = min(ans, n - sum);
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
using namespace std;
#define REP(i, st, ed) for (int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (int i = st, i##end = ed; i >= i##end; --i)
#define Runtime 1.0 * clock() / CLOCKS_PER_SEC
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
const int maxn = 2005;
int n, m, eu[maxn], ev[maxn];
int Begin[maxn], Next[maxn << 1], to[maxn << 1], e;
int sum = 0, ans = 1e9;
void add_edge(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
void dfs(int u, int fa, int dep) {
  ++sum;
  if (dep == m)
    return;
  for (int i = Begin[u]; i; i = Next[i])
    if (to[i] != fa)
      dfs(to[i], u, dep + 1);
}
int main() {
  int u, v;
  n = read(), m = read();
  REP(i, 1, n - 1) {
    u = read(), v = read();
    add_edge(u, v), add_edge(v, u);
    eu[i] = u, ev[i] = v;
  }
  if (m & 1) {
    m >>= 1;
    REP(i, 1, n - 1) {
      sum = 0;
      dfs(eu[i], ev[i], 0);
      dfs(ev[i], eu[i], 0);
      ans = min(ans, n - sum);
    }
  } else {
    m >>= 1;
    REP(i, 1, n) {
      sum = 0;
      dfs(i, 0, 0);
      ans = min(ans, n - sum);
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p04049 (C++)

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

const int maxn = int(1e3) + 5;

pair<int, int> edge[maxn];
vector<int> graph[maxn];

int dfs(int node, int par, int d) {
  int res = 1;
  if (!d)
    return res;
  for (auto it : graph[node]) {
    if (it != par)
      res += dfs(it, node, d - 1);
  }
  return res;
}

int main(void) {
  int n, k, u, v;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    u--, v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
    edge[i] = {u, v};
  }

  int res = 0;
  if (k % 2) {
    for (int i = 1; i < n; i++) {
      res = max(res, dfs(edge[i].first, edge[i].second, k / 2) +
                         dfs(edge[i].second, edge[i].first, k / 2));
    }
  } else {
    for (int i = 0; i < n; i++) {
      res = max(res, dfs(i, -1, k / 2));
    }
  }
  printf("%d\n", n - res);
}

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

const int maxn = int(2e3) + 5;

pair<int, int> edge[maxn];
vector<int> graph[maxn];

int dfs(int node, int par, int d) {
  int res = 1;
  if (!d)
    return res;
  for (auto it : graph[node]) {
    if (it != par)
      res += dfs(it, node, d - 1);
  }
  return res;
}

int main(void) {
  int n, k, u, v;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    u--, v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
    edge[i] = {u, v};
  }

  int res = 0;
  if (k % 2) {
    for (int i = 1; i < n; i++) {
      res = max(res, dfs(edge[i].first, edge[i].second, k / 2) +
                         dfs(edge[i].second, edge[i].first, k / 2));
    }
  } else {
    for (int i = 0; i < n; i++) {
      res = max(res, dfs(i, -1, k / 2));
    }
  }
  printf("%d\n", n - res);
}
Problem p04049 (C++)

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

typedef const int &cint;

const int MAXN = 2010;

struct Edge {
  int nxt, to;

  Edge() {}

  Edge(cint nxt_, cint to_) { nxt = nxt_, to = to_; }
} ed[MAXN];
int head[MAXN], tot;
int eg[2][MAXN];
bool vis[MAXN];
int dep[MAXN], cnt[MAXN];

inline void chkmin(int &a, int b) {
  if (b < a)
    a = b;
}

inline void add(cint u, cint v) {
  ed[++tot] = Edge(head[u], v), head[u] = tot;
  ed[++tot] = Edge(head[v], u), head[v] = tot;
}

void dfs(int u) {
  vis[u] = 1, ++cnt[dep[u]];
  int tmp, v;
  for (tmp = head[u], v = ed[tmp].to; tmp; tmp = ed[tmp].nxt, v = ed[tmp].to)
    if (!vis[v])
      dep[v] = dep[u] + 1, dfs(v);
}

int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  for (int i = 1; i < N; ++i)
    scanf("%d %d", &eg[0][i], &eg[1][i]), add(eg[0][i], eg[1][i]);
  int ans = N;
  if (K & 1) {
    for (int i = 1, sum; i < N; ++i) {
      memset(vis, 0, sizeof vis);
      memset(cnt, 0, sizeof cnt);
      dep[eg[0][i]] = dep[eg[1][i]] = 0;
      vis[eg[0][i]] = vis[eg[1][i]] = 1;
      dfs(eg[0][i]), dfs(eg[1][i]);
      sum = 0;
      for (int i = K / 2; i >= 0; --i)
        sum += cnt[i];
      chkmin(ans, N - sum);
    }
  } else {
    for (int i = 1, sum; i <= N; ++i) {
      memset(vis, 0, sizeof vis);
      memset(cnt, 0, sizeof cnt);
      vis[i] = 1, dep[i] = 0, dfs(i);
      sum = 0;
      for (int i = K / 2; i >= 0; --i)
        sum += cnt[i];
      chkmin(ans, N - sum);
    }
  }
  printf("%d", ans);
  return 0;
}


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

typedef const int &cint;

const int MAXN = 2010;

struct Edge {
  int nxt, to;

  Edge() {}

  Edge(cint nxt_, cint to_) { nxt = nxt_, to = to_; }
} ed[MAXN << 1];
int head[MAXN], tot;
int eg[2][MAXN];
bool vis[MAXN];
int dep[MAXN], cnt[MAXN];

inline void chkmin(int &a, int b) {
  if (b < a)
    a = b;
}

inline void add(cint u, cint v) {
  ed[++tot] = Edge(head[u], v), head[u] = tot;
  ed[++tot] = Edge(head[v], u), head[v] = tot;
}

void dfs(int u) {
  vis[u] = 1, ++cnt[dep[u]];
  int tmp, v;
  for (tmp = head[u], v = ed[tmp].to; tmp; tmp = ed[tmp].nxt, v = ed[tmp].to)
    if (!vis[v])
      dep[v] = dep[u] + 1, dfs(v);
}

int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  for (int i = 1; i < N; ++i)
    scanf("%d %d", &eg[0][i], &eg[1][i]), add(eg[0][i], eg[1][i]);
  int ans = N;
  if (K & 1) {
    for (int i = 1, sum; i < N; ++i) {
      memset(vis, 0, sizeof vis);
      memset(cnt, 0, sizeof cnt);
      dep[eg[0][i]] = dep[eg[1][i]] = 0;
      vis[eg[0][i]] = vis[eg[1][i]] = 1;
      dfs(eg[0][i]), dfs(eg[1][i]);
      sum = 0;
      for (int i = K / 2; i >= 0; --i)
        sum += cnt[i];
      chkmin(ans, N - sum);
    }
  } else {
    for (int i = 1, sum; i <= N; ++i) {
      memset(vis, 0, sizeof vis);
      memset(cnt, 0, sizeof cnt);
      vis[i] = 1, dep[i] = 0, dfs(i);
      sum = 0;
      for (int i = K / 2; i >= 0; --i)
        sum += cnt[i];
      chkmin(ans, N - sum);
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1005;
int Head[N], Next[N << 1], Adj[N << 1], tot = 0, dis[N];
int u[N], v[N];
inline void addedge(int u, int v) {
  Next[++tot] = Head[u];
  Head[u] = tot;
  Adj[tot] = v;
  Next[++tot] = Head[v];
  Head[v] = tot;
  Adj[tot] = u;
}
inline void dfs(int x, int f) {
  for (int e = Head[x]; e; e = Next[e])
    if (Adj[e] != f)
      dis[Adj[e]] = dis[x] + 1, dfs(Adj[e], x);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u[i], &v[i]);
    addedge(u[i], v[i]);
  }
  int ans = n;
  if (k & 1) {
    for (int i = 1; i < n; i++) {
      dis[u[i]] = dis[v[i]] = 0;
      dfs(u[i], v[i]), dfs(v[i], u[i]);
      int cnt = 0;
      for (int j = 1; j <= n; j++)
        if (dis[j] > (k / 2))
          cnt++;
      ans = min(ans, cnt);
    }
  } else {
    for (int i = 1; i <= n; i++) {
      dis[i] = 0;
      int cnt = 0;
      dfs(i, 0);
      for (int j = 1; j <= n; j++)
        if (dis[j] > (k / 2))
          cnt++;
      ans = min(ans, cnt);
    }
  }
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2005;
int Head[N], Next[N << 1], Adj[N << 1], tot = 0, dis[N];
int u[N], v[N];
inline void addedge(int u, int v) {
  Next[++tot] = Head[u];
  Head[u] = tot;
  Adj[tot] = v;
  Next[++tot] = Head[v];
  Head[v] = tot;
  Adj[tot] = u;
}
inline void dfs(int x, int f) {
  for (int e = Head[x]; e; e = Next[e])
    if (Adj[e] != f)
      dis[Adj[e]] = dis[x] + 1, dfs(Adj[e], x);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u[i], &v[i]);
    addedge(u[i], v[i]);
  }
  int ans = n;
  if (k & 1) {
    for (int i = 1; i < n; i++) {
      dis[u[i]] = dis[v[i]] = 0;
      dfs(u[i], v[i]), dfs(v[i], u[i]);
      int cnt = 0;
      for (int j = 1; j <= n; j++)
        if (dis[j] > (k / 2))
          cnt++;
      ans = min(ans, cnt);
    }
  } else {
    for (int i = 1; i <= n; i++) {
      dis[i] = 0;
      int cnt = 0;
      dfs(i, 0);
      for (int j = 1; j <= n; j++)
        if (dis[j] > (k / 2))
          cnt++;
      ans = min(ans, cnt);
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p04049 (C++)

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

const int kN = 2e3 + 5;
const int kInf = 0x3f3f3f3f;

bool vis[kN];
std::vector<int> E[kN];
int k, n, ans = kInf;
void Dfs(int cur, int d) {
  if (d > k / 2)
    return;
  vis[cur] = true;
  for (int x : E[cur])
    if (!vis[x])
      Dfs(x, d + 1);
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    E[x].push_back(y);
    E[y].push_back(x);
  }
  if (k & 1) {
    for (int i = 1; i <= n; ++i)
      for (int x : E[i]) {
        memset(vis, false, sizeof(vis));
        int tot = 0;
        vis[x] = true;
        Dfs(i, 0);
        Dfs(x, 0);
        for (int j = 1; j <= n; ++j)
          if (!vis[j])
            ++tot;
        ans = std::min(ans, tot);
      }
  } else {
    for (int i = 1; i <= n; ++i) {
      memset(vis, false, sizeof(vis));
      int tot = 0;
      Dfs(i, 0);
      for (int j = 1; j <= n; ++j)
        if (!vis[j])
          ++tot;
      ans = std::min(ans, tot);
    }
  }
  printf("%d", ans);
  return 0;
}

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

const int kN = 2e3 + 5;
const int kInf = 0x3f3f3f3f;

bool vis[kN];
std::vector<int> E[kN];
int k, n, ans = kInf;
void Dfs(int cur, int d) {
  if (d > k / 2)
    return;
  vis[cur] = true;
  for (int x : E[cur])
    if (!vis[x])
      Dfs(x, d + 1);
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    E[x].push_back(y);
    E[y].push_back(x);
  }
  if (k & 1) {
    for (int i = 1; i <= n; ++i)
      for (int x : E[i])
        if (x < i) {
          memset(vis, false, sizeof(vis));
          int tot = 0;
          vis[x] = true;
          Dfs(i, 0);
          Dfs(x, 0);
          for (int j = 1; j <= n; ++j)
            if (!vis[j])
              ++tot;
          ans = std::min(ans, tot);
        }
  } else {
    for (int i = 1; i <= n; ++i) {
      memset(vis, false, sizeof(vis));
      int tot = 0;
      Dfs(i, 0);
      for (int j = 1; j <= n; ++j)
        if (!vis[j])
          ++tot;
      ans = std::min(ans, tot);
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int N, K;
int dp[1005][1005];
int maxi[1005];
int s[1005];
int nr[1005];
vector<int> G[1005];
void dfs(int nod, int tata) {
  dp[nod][0] = 1;
  maxi[nod] = nr[nod] = 1;
  for (auto it : G[nod]) {
    if (tata != it) {
      dfs(it, nod);
      nr[nod] += nr[it];
    }
  }
  for (auto it : G[nod])
    if (tata != it) {
      fill_n(s + 1, K + 1, 1 << 30);
      s[0] = dp[nod][0] + dp[it][0];
      for (int j = 0; j <= maxi[it] && j < K + 1; j++)
        for (int k = 1; k <= maxi[nod] && k <= K + 1 - j; k++)
          s[max(k, j + 1)] = min(s[max(k, j + 1)], dp[nod][k] + dp[it][j]);
      for (int i = 1; i <= K + 1; i++)
        s[i] = min(s[i - 1], s[i]);
      memcpy(dp[nod], s, sizeof(s));
      maxi[nod] = max(maxi[nod], maxi[it] + 1);
    }
}
int main() {
  cin >> N >> K;
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, 0);
  int ans = 1 << 30;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= K + 1; j++)
      ans = min(ans, dp[i][j] + nr[1] - nr[i]);
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int N, K;
int dp[2005][2005];
int maxi[2005];
int s[2005];
int nr[2005];
vector<int> G[2005];
void dfs(int nod, int tata) {
  dp[nod][0] = 1;
  maxi[nod] = nr[nod] = 1;
  for (auto it : G[nod]) {
    if (tata != it) {
      dfs(it, nod);
      nr[nod] += nr[it];
    }
  }
  for (auto it : G[nod])
    if (tata != it) {
      fill_n(s + 1, K + 1, 1 << 30);
      s[0] = dp[nod][0] + dp[it][0];
      for (int j = 0; j <= maxi[it] && j < K + 1; j++)
        for (int k = 1; k <= maxi[nod] && k <= K + 1 - j; k++)
          s[max(k, j + 1)] = min(s[max(k, j + 1)], dp[nod][k] + dp[it][j]);
      for (int i = 1; i <= K + 1; i++)
        s[i] = min(s[i - 1], s[i]);
      memcpy(dp[nod], s, sizeof(s));
      maxi[nod] = max(maxi[nod], maxi[it] + 1);
    }
}
int main() {
  cin >> N >> K;
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, 0);
  int ans = 1 << 30;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= K + 1; j++)
      ans = min(ans, dp[i][j] + nr[1] - nr[i]);
  cout << ans;
  return 0;
}
Problem p04049 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep2(i, j, n) for (LL i = j; i <= n; ++i)
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define boost                                                                  \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
#define all(x) x.begin(), x.end()

using namespace std;

const int N = 2111;

int n, k, a, b;
vector<int> v[N];

int ans;
int siz[N];
int dp[N][N]; // dp[i][j] = ile minimalnie musze usunac zeby srednica byla <= j
int dp2[N];

void dfs(int u, int p) {
  siz[u] = 1;
  rep(i, 0, n) dp[u][i] = 0;
  for (auto it : v[u]) {
    if (it == p)
      continue;
    dfs(it, u);
    siz[u] += siz[it];

    rep(i, 0, n) dp2[i] = n;

    rep(ja, 0, n) rep(on, 0, n) if (ja + on <= k) dp2[max(ja, on)] =
        min(dp2[max(ja, on)], dp[u][ja] + dp[it][on]);

    rep(i, 0, n) dp[u][i] = dp2[i];
    rep(i, 1, n) dp[u][i] = min(dp[u][i], dp[u][i - 1]);
  }
  ans = min(ans, dp[u][k] + (n - siz[u]));
  per(i, 0, n - 1) dp[u][i + 1] = dp[u][i];
  dp[u][0] = siz[u];
}

int main() {
  scanf("%d%d", &n, &k);
  rep(i, 1, n - 1) {
    scanf("%d%d", &a, &b);
    v[a].pb(b);
    v[b].pb(a);
  }
  ans = n;
  dfs(1, 0);
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep2(i, j, n) for (LL i = j; i <= n; ++i)
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define boost                                                                  \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
#define all(x) x.begin(), x.end()

using namespace std;

const int N = 2111;

int n, k, a, b;
vector<int> v[N];

int ans;
int siz[N];
int dp[N][N]; // dp[i][j] = ile minimalnie musze usunac zeby srednica byla <= j
int dp2[N];

void dfs(int u, int p) {
  siz[u] = 1;
  rep(i, 0, n) dp[u][i] = 0;
  for (auto it : v[u]) {
    if (it == p)
      continue;
    dfs(it, u);
    siz[u] += siz[it];

    rep(i, 0, n) dp2[i] = n;

    rep(ja, 0, n) {
      dp2[ja] = min(dp2[ja], dp[u][ja] + dp[it][min(ja, k - ja)]);
      dp2[ja] = min(dp2[ja], dp[it][ja] + dp[u][min(ja, k - ja)]);
    }

    rep(i, 0, n) dp[u][i] = dp2[i];
    rep(i, 1, n) dp[u][i] = min(dp[u][i], dp[u][i - 1]);
  }
  ans = min(ans, dp[u][k] + (n - siz[u]));
  per(i, 0, n - 1) dp[u][i + 1] = dp[u][i];
  dp[u][0] = siz[u];
}

int main() {
  scanf("%d%d", &n, &k);
  rep(i, 1, n - 1) {
    scanf("%d%d", &a, &b);
    v[a].pb(b);
    v[b].pb(a);
  }
  ans = n;
  dfs(1, 0);
  printf("%d\n", ans);

  return 0;
}
Problem p04049 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from collections import deque


def bfs(links, s, limit, bits):
    not_reachable = (1 << len(links)) - 1
    q = deque([(0, s, -1)])
    while q:
        cost, v, p = q.popleft()
        if cost > limit:
            break
        not_reachable ^= bits[v]
        cost += 1
        q.extend((cost, u, v) for u in links[v] if u != p)
    return not_reachable


def solve(n, k, ab):
    if k == 1:
        return n - 2

    links = [set() for _ in range(n)]
    for a, b in ab:
        a -= 1
        b -= 1
        links[a].add(b)
        links[b].add(a)

    limit = k // 2
    bits = [1 << i for i in range(n)]
    if k % 2 == 0:
        ans = min(bin(bfs(links, v, limit, bits)).count("1") for v in range(n))
    else:
        dists = [bfs(links, v, limit, bits) for v in range(n)]
        ans = min(bin(dists[a - 1] & dists[b - 1]).count("1") for a, b in ab)
    return ans


n, k = map(int, sys.stdin.buffer.readline().split())
ab = map(int, sys.stdin.buffer.read().split())
ab = list(zip(ab, ab))
print(solve(n, k, ab))


Fixed submission:
import sys
from collections import deque


def bfs(links, s, limit, bits):
    not_reachable = (1 << len(links)) - 1
    q = deque([(0, s, -1)])
    while q:
        cost, v, p = q.popleft()
        if cost > limit:
            break
        not_reachable ^= bits[v]
        cost += 1
        for u in links[v]:
            if u != p:
                q.append((cost, u, v))
    return not_reachable


def solve(n, k, ab):
    if k == 1:
        return n - 2

    links = [set() for _ in range(n)]
    for a, b in ab:
        a -= 1
        b -= 1
        links[a].add(b)
        links[b].add(a)

    limit = k // 2
    bits = [1 << i for i in range(n)]
    if k % 2 == 0:
        ans = min(bin(bfs(links, v, limit, bits)).count("1") for v in range(n))
    else:
        dists = [bfs(links, v, limit, bits) for v in range(n)]
        ans = min(bin(dists[a - 1] & dists[b - 1]).count("1") for a, b in ab)
    return ans


n, k = map(int, sys.stdin.buffer.readline().split())
ab = map(int, sys.stdin.buffer.read().split())
ab = list(zip(ab, ab))
print(solve(n, k, ab))
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void err() {
  std::cout << "Impossible" << '\n';
  exit(0);
}
int main(int argc, char const *argv[]) {
  ll n, m;
  std::cin >> n >> m;
  vll a(m), b;
  ll o = 0;
  for (int i = 0; i < m; i++) {
    scanf("%lld", &a[i]);
    if (a[i] % 2)
      o++;
  }
  if ((n % 2 && o >= 2) || (n % 2 == 0 && o > 2))
    err();

  sort(all(a), [](ll x, ll y) { // 奇数を右に
    if (x % 2)
      return false;
    if (y % 2)
      return true;
    return x < y;
  });

  if (m == 1 && a[0] == 1)
    b = vll{1};
  else if (m == 1)
    b = vll{1, a[0] - 1};
  else {
    if (o == 2)
      swap(a[1], a[n - 1]);
    for (int i = 0; i < m; i++) {
      ll x = 0;
      if (i == 0)
        x = -1;
      if (i == m - 1)
        x = 1;
      if (a[i] + x != 0)
        b.push_back(a[i] + x);
    }
  }
  re(i, m) std::cout << a[i] << (i == m - 1 ? "\n" : " ");
  std::cout << b.size() << '\n';
  re(i, b.size()) std::cout << b[i] << (i == b.size() - 1 ? "\n" : " ");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void err() {
  std::cout << "Impossible" << '\n';
  exit(0);
}
int main(int argc, char const *argv[]) {
  ll n, m;
  std::cin >> n >> m;
  vll a(m), b;
  ll o = 0;
  for (int i = 0; i < m; i++) {
    scanf("%lld", &a[i]);
    if (a[i] % 2)
      o++;
  }
  if ((n % 2 && o >= 2) || (n % 2 == 0 && o > 2))
    err();

  sort(all(a), [](ll x, ll y) { // 奇数を右に
    if (x % 2)
      return false;
    if (y % 2)
      return true;
    return x < y;
  });

  if (m == 1 && a[0] == 1)
    b = vll{1};
  else if (m == 1)
    b = vll{1, a[0] - 1};
  else {
    if (o == 2)
      swap(a[0], a[m - 2]);
    for (int i = 0; i < m; i++) {
      ll x = 0;
      if (i == 0)
        x = -1;
      if (i == m - 1)
        x = 1;
      if (a[i] + x != 0)
        b.push_back(a[i] + x);
    }
  }
  re(i, m) std::cout << a[i] << (i == m - 1 ? "\n" : " ");
  std::cout << b.size() << '\n';
  re(i, b.size()) std::cout << b[i] << (i == b.size() - 1 ? "\n" : " ");
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 100010
int a[N], n, m, num;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &a[i]);
    num += a[i] & 1;
  }
  if (num > 2) {
    printf("Impossible\n");
    return 0;
  }
  for (int i = 2; i < m; i++)
    if (a[i] & 1)
      swap(a[(a[1] & 1) ? m : 1], a[i]);
  for (int i = 1; i < m; i++)
    printf("%d ", a[i]);
  printf("%d\n", a[m]);
  if (m == 1)
    a[++m] = a[1], a[1] = 0;
  a[1]++;
  printf("%d\n", (--a[m]) ? m : --m);
  for (int i = 1; i < m; i++)
    printf("%d ", a[i]);
  printf("%d\n", a[m]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100010
int a[N], n, m, num;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    num += a[i] & 1;
  }
  if (num > 2) {
    printf("Impossible\n");
    return 0;
  }
  for (int i = 2; i < m; i++)
    if (a[i] & 1)
      swap(a[(a[1] & 1) ? m : 1], a[i]);
  for (int i = 1; i < m; i++)
    printf("%d ", a[i]);
  printf("%d\n", a[m]);
  if (m == 1)
    a[++m] = a[1], a[1] = 0;
  a[1]++;
  printf("%d\n", (--a[m]) ? m : --m);
  for (int i = 1; i < m; i++)
    printf("%d ", a[i]);
  printf("%d\n", a[m]);
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, M, a[1005];
int main() {
  scanf("%d%d", &N, &M);
  int cnt = 0;
  for (int i = 1; i <= M; i++)
    scanf("%d", &a[i]), cnt += a[i] % 2;
  if (M == 1)
    return a[1] == 1 ? puts("1\n1") : printf("%d\n2\n%d 1", a[1], a[1] - 1);
  if (cnt > 2)
    return puts("Impossible"), 0;
  int pos = 1;
  for (int i = 1; i <= M; i++)
    if (a[i] % 2)
      swap(a[i], a[pos]), pos = M;

  if (a[M] == 1)
    swap(a[1], a[M]);
  for (int i = 1; i <= M; i++)
    printf("%d ", a[i]);
  puts("");

  a[1]++;
  a[M]--;
  if (a[M] == 0)
    M--;
  printf("%d\n", M);
  for (int i = 1; i <= M; i++)
    printf("%d ", a[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, M, a[1005];
int main() {
  scanf("%d%d", &N, &M);
  int cnt = 0;
  for (int i = 1; i <= M; i++)
    scanf("%d", &a[i]), cnt += a[i] % 2;
  if (M == 1)
    return (a[1] == 1 ? puts("1\n1\n1")
                      : printf("%d\n2\n%d 1", a[1], a[1] - 1)),
           0;
  if (cnt > 2)
    return puts("Impossible"), 0;
  int pos = 1;
  for (int i = 1; i <= M; i++)
    if (a[i] % 2)
      swap(a[i], a[pos]), pos = M;

  if (a[M] == 1)
    swap(a[1], a[M]);
  for (int i = 1; i <= M; i++)
    printf("%d ", a[i]);
  puts("");

  a[1]++;
  a[M]--;
  if (a[M] == 0)
    M--;
  printf("%d\n", M);
  for (int i = 1; i <= M; i++)
    printf("%d ", a[i]);
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#define rg register
#define ll long long
#define LDB long double
#define ull unsigned long long
#define view(i, x) for (rg int i = hd[x]; i != -1; i = e[i].nt)
#define go(i, x, a) for (rg int i = a; i < x; i++)
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
using namespace std;

const int maxn = 105;
int n, a[maxn], m, b[maxn];

inline int rd() {
  int ret = 0, af = 1;
  char gc = getchar();
  while (gc < '0' || gc > '9') {
    if (gc == '-')
      af = -af;
    gc = getchar();
  }
  while (gc >= '0' && gc <= '9')
    ret = ret * 10 + gc - '0', gc = getchar();
  return ret * af;
}

inline bool cmp(int a, int b) { return a % 2 > b % 2; }

int main() {
  n = rd();
  m = rd();
  go(i, m + 1, 1) a[i] = rd();
  sort(a + 1, a + n + 1, cmp);
  int t = 0;
  while (a[t + 1] % 2 == 1)
    t++;
  if (t > 2) {
    printf("Impossible");
    return 0;
  }
  if (t == 2)
    swap(a[m], a[2]);
  go(i, m + 1, 1) printf("%d ", a[i]);
  puts("");
  go(i, m + 1, 1) b[i] = a[i];
  if (m == 1)
    m++;
  b[1]--;
  b[m]++;
  if (b[1] == 0) {
    printf("%d\n", m - 1);
    go(i, m + 1, 2) printf("%d ", b[i]);
  } else {
    printf("%d\n", m);
    go(i, m + 1, 1) printf("%d ", b[i]);
  }
  return 0;
} // Faze


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#define rg register
#define ll long long
#define LDB long double
#define ull unsigned long long
#define view(i, x) for (rg int i = hd[x]; i != -1; i = e[i].nt)
#define go(i, x, a) for (rg int i = a; i < x; i++)
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
using namespace std;

const int maxn = 105;
int n, a[maxn], m, b[maxn];

inline int rd() {
  int ret = 0, af = 1;
  char gc = getchar();
  while (gc < '0' || gc > '9') {
    if (gc == '-')
      af = -af;
    gc = getchar();
  }
  while (gc >= '0' && gc <= '9')
    ret = ret * 10 + gc - '0', gc = getchar();
  return ret * af;
}

inline bool cmp(int a, int b) { return a % 2 > b % 2; }

int main() {
  n = rd();
  m = rd();
  go(i, m + 1, 1) a[i] = rd();
  sort(a + 1, a + m + 1, cmp);
  int t = 0;
  while (a[t + 1] % 2 == 1)
    t++;
  if (t > 2) {
    printf("Impossible");
    return 0;
  }
  if (t == 2)
    swap(a[m], a[2]);
  go(i, m + 1, 1) printf("%d ", a[i]);
  puts("");
  go(i, m + 1, 1) b[i] = a[i];
  if (m == 1)
    m++;
  b[1]--;
  b[m]++;
  if (b[1] == 0) {
    printf("%d\n", m - 1);
    go(i, m + 1, 2) printf("%d ", b[i]);
  } else {
    printf("%d\n", m);
    go(i, m + 1, 1) printf("%d ", b[i]);
  }
  return 0;
} // Faze
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int v[110], n, m, i, nr;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> v[i];
    nr += v[i] % 2;
  }
  if (nr > 2) {
    cout << "Impossible";
    return 0;
  }
  if (m == 1) {
    if (v[1] == 1)
      cout << "1\n1\n1\n";
    else
      cout << v[1] << "\n" << 2 << "\n" << 1 << " " << v[1] - 1 << "\n";
    return 0;
  }
  for (i = 1; i <= m; i++)
    if (v[i] % 2 == 1)
      swap(v[i], v[1]);
  for (i = 2; i <= m; i++)
    if (v[i] % 2 == 1)
      swap(v[i], v[n]);
  for (i = 1; i <= m; i++)
    cout << v[i] << " ";
  cout << "\n";
  if (v[1] == 1)
    cout << m - 1 << "\n";
  else
    cout << m << "\n" << v[1] - 1 << " ";
  for (i = 2; i < m; i++)
    cout << v[i] << " ";
  cout << v[m] + 1 << "\n";
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int v[110], n, m, i, nr;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> v[i];
    nr += v[i] % 2;
  }
  if (nr > 2) {
    cout << "Impossible";
    return 0;
  }
  if (m == 1) {
    if (v[1] == 1)
      cout << "1\n1\n1\n";
    else
      cout << v[1] << "\n" << 2 << "\n" << 1 << " " << v[1] - 1 << "\n";
    return 0;
  }
  for (i = 1; i <= m; i++)
    if (v[i] % 2 == 1)
      swap(v[i], v[1]);
  for (i = 2; i <= m; i++)
    if (v[i] % 2 == 1)
      swap(v[i], v[m]);
  for (i = 1; i <= m; i++)
    cout << v[i] << " ";
  cout << "\n";
  if (v[1] == 1)
    cout << m - 1 << "\n";
  else
    cout << m << "\n" << v[1] - 1 << " ";
  for (i = 2; i < m; i++)
    cout << v[i] << " ";
  cout << v[m] + 1 << "\n";
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
int n, m, a[100009];
vector<int> A, B, C, D;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
    if (a[i] % 2 == 0)
      A.push_back(a[i]);
    if (a[i] % 2 == 1)
      B.push_back(a[i]);
  }
  if (B.size() >= 3) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (m == 1) {
    assert(false);
  } else {
    if (B.size() >= 1)
      C.push_back(B[0]);
    for (int i = 0; i < A.size(); i++)
      C.push_back(A[i]);
    if (B.size() >= 2)
      C.push_back(B[1]);
    if (C[0] >= 2)
      D.push_back(C[0] - 1);
    for (int i = 1; i < (int)C.size() - 1; i++)
      D.push_back(C[i]);
    D.push_back(C[C.size() - 1] + 1);

    for (int i = 0; i < C.size(); i++) {
      if (i)
        cout << " ";
      cout << C[i];
    }
    cout << endl;
    cout << D.size() << endl;
    for (int i = 0; i < D.size(); i++) {
      if (i)
        cout << " ";
      cout << D[i];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
int n, m, a[100009];
vector<int> A, B, C, D;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
    if (a[i] % 2 == 0)
      A.push_back(a[i]);
    if (a[i] % 2 == 1)
      B.push_back(a[i]);
  }
  if (B.size() >= 3) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (m == 1) {
    if (n == 1)
      cout << "1" << endl << "1" << endl << "1" << endl;
    else {
      cout << n << endl;
      cout << "2" << endl;
      cout << n - 1 << " " << 1 << endl;
    }
  } else {
    if (B.size() >= 1)
      C.push_back(B[0]);
    for (int i = 0; i < A.size(); i++)
      C.push_back(A[i]);
    if (B.size() >= 2)
      C.push_back(B[1]);
    if (C[0] >= 2)
      D.push_back(C[0] - 1);
    for (int i = 1; i < (int)C.size() - 1; i++)
      D.push_back(C[i]);
    D.push_back(C[C.size() - 1] + 1);

    for (int i = 0; i < C.size(); i++) {
      if (i)
        cout << " ";
      cout << C[i];
    }
    cout << endl;
    cout << D.size() << endl;
    for (int i = 0; i < D.size(); i++) {
      if (i)
        cout << " ";
      cout << D[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 1010
using namespace std;
int n, m;
int a[N];
vector<int> s, w;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    if (a[i] & 1)
      s.push_back(a[i]);
    else
      w.push_back(a[i]);
  }
  if (s.size() > 2)
    puts("Impossible");
  else if (m == 1) {
    if (s.size()) {
      if (s[0] == 1)
        printf("1\n1\n1\n");
      else
        printf("%d\n2\n%d %d\n", s[0], s[0] / 2, s[0] - s[0] / 2);
    } else
      printf("%d\n2\n%d %d\n", w[0], w[0] / 2 + 1, w[0] - w[0] / 2 - 1);
  } else if (s.size() == 0) {
    for (int i = 0; i < n; i++)
      printf("%d ", a[i]);
    puts("");
    printf("%d\n", m);
    a[0]--;
    a[m - 1]++;
    for (int i = 0; i < m; i++)
      printf("%d ", a[i]);
    puts("");
  } else if (s.size() == 1) {
    printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    puts("");
    printf("%d\n", m);
    printf("%d ", s[0] + 1);
    w[w.size() - 1]--;
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    puts("");
  } else if (s.size() == 2) {
    printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    printf("%d\n", s[1]);
    s[0]--, s[1]++;
    m -= (s[0] == 0);
    printf("%d\n", m);
    if (s[0])
      printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    printf("%d\n", s[1]);
  } else
    while (1)
      ;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 1010
using namespace std;
int n, m;
int a[N];
vector<int> s, w;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    if (a[i] & 1)
      s.push_back(a[i]);
    else
      w.push_back(a[i]);
  }
  if (s.size() > 2)
    puts("Impossible");
  else if (m == 1) {
    if (s.size()) {
      if (s[0] == 1)
        printf("1\n1\n1\n");
      else
        printf("%d\n2\n%d %d\n", s[0], s[0] / 2, s[0] - s[0] / 2);
    } else
      printf("%d\n2\n%d %d\n", w[0], w[0] / 2 + 1, w[0] - w[0] / 2 - 1);
  } else if (s.size() == 0) {
    for (int i = 0; i < m; i++)
      printf("%d ", a[i]);
    puts("");
    printf("%d\n", m);
    a[0]--;
    a[m - 1]++;
    for (int i = 0; i < m; i++)
      printf("%d ", a[i]);
    puts("");
  } else if (s.size() == 1) {
    printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    puts("");
    printf("%d\n", m);
    printf("%d ", s[0] + 1);
    w[w.size() - 1]--;
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    puts("");
  } else if (s.size() == 2) {
    printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    printf("%d\n", s[1]);
    s[0]--, s[1]++;
    m -= (s[0] == 0);
    printf("%d\n", m);
    if (s[0])
      printf("%d ", s[0]);
    for (int i = 0; i < w.size(); i++)
      printf("%d ", w[i]);
    printf("%d\n", s[1]);
  } else
    while (1)
      ;
  return 0;
}
Problem p04050 (C++)

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

const int N = 108;

int n;
int a[N], b[N];

int main() {
  int i, mid;
  scanf("%*d%d", &n);
  for (i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (n == 1)
    return *a == 1 ? puts("1\n1\n1") : printf("%d\n2\n1 %d\n", *a, *a - 1), 0;
  mid = std::partition(a, a + n, [](const int x) -> bool { return x % 2; }) - a;
  if (mid > 2)
    return puts("Impossible");
  a[n] = *a;
  for (i = 1; i <= n; ++i)
    printf("%d%c", a[i], i == n ? 10 : 32);
  memcpy(b + 1, a + 1, n << 2), ++b[1], --b[n], n -= !b[n];
  printf("%d\n", n);
  for (i = 1; i <= n; ++i)
    printf("%d%c", b[i], i == n ? 10 : 32);
  return 0;
}

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

const int N = 108;

int n;
int a[N], b[N];

int main() {
  int i, mid;
  scanf("%*d%d", &n);
  for (i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (n == 1)
    return *a == 1 ? puts("1\n1\n1") : printf("%d\n2\n1 %d\n", *a, *a - 1), 0;
  mid = std::partition(a, a + n, [](const int x) -> bool { return x % 2; }) - a;
  if (mid > 2)
    return puts("Impossible"), 0;
  a[n] = *a;
  for (i = 1; i <= n; ++i)
    printf("%d%c", a[i], i == n ? 10 : 32);
  memcpy(b + 1, a + 1, n << 2), ++b[1], --b[n], n -= !b[n];
  printf("%d\n", n);
  for (i = 1; i <= n; ++i)
    printf("%d%c", b[i], i == n ? 10 : 32);
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define maxn 100005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
int a[maxn];
int n, m;
int f[maxn], cnt = 0;
bool cmp(int a, int b) { return (a % 2) > (b % 2); }
int pl;
void push(int a) {
  if (pl + a - 1 >= n)
    a = n + 1 - pl;
  f[cnt++] = a, pl += a;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  if (m <= 2)
    return 1;
  sort(a, a + m);
  int ans = 0;
  for (int i = 0; i < m; i++)
    ans += a[i] % 2;
  if (n % 2 == 0 && ans > 2)
    cout << "Impossible" << endl;
  else if (n % 2 == 1 && ans > 1)
    cout << "Impossible" << endl;
  else {
    pl = 1;
    if (a[1] % 2)
      swap(a[1], a[m - 1]);
    for (int i = 0; i < m; i++) {
      if (a[i] % 2) {
        if (i == 0)
          for (int j = 0; j < a[i] / 2 + 1; j++)
            push(2);
        else
          for (int j = 0; j < a[i] / 2; j++)
            push(2);
      } else {
        if (i == 0)
          push(1);
        push(a[i]);
      }
    }
    for (int i = 0; i < m; i++)
      cout << a[i] << ' ';
    cout << endl;
    cout << cnt << endl;
    for (int i = 0; i < cnt; i++)
      printf("%d ", f[i]);
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define maxn 100005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
int a[maxn];
int n, m;
int f[maxn], cnt = 0;
bool cmp(int a, int b) { return (a % 2) > (b % 2); }
int pl;
void push(int a) {
  if (pl + a - 1 >= n)
    a = n + 1 - pl;
  f[cnt++] = a, pl += a;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  //	if (m <= 2) return 1;
  sort(a, a + m, cmp);
  int ans = 0;
  for (int i = 0; i < m; i++)
    ans += a[i] % 2;
  if (n % 2 == 0 && ans > 2)
    cout << "Impossible" << endl;
  else if (n % 2 == 1 && ans > 1)
    cout << "Impossible" << endl;
  else {
    pl = 1;
    if (a[1] % 2)
      swap(a[1], a[m - 1]);
    for (int i = 0; i < m; i++) {
      if (a[i] % 2) {
        if (i == 0)
          for (int j = 0; j < a[i] / 2 + 1; j++)
            push(2);
        else
          for (int j = 0; j < a[i] / 2; j++)
            push(2);
      } else {
        if (i == 0)
          push(1);
        push(a[i]);
      }
    }
    for (int i = 0; i < m; i++)
      cout << a[i] << ' ';
    cout << endl;
    cout << cnt << endl;
    for (int i = 0; i < cnt; i++)
      printf("%d ", f[i]);
    printf("\n");
  }
  return 0;
}
Problem p04050 (C++)

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

int n, m, num, cnt;
vector<int> a, b;

void kill() {
  cout << "Impossible";
  exit(0);
}

void add(int l) {
  if (l)
    b.push_back(l);
}

void print() {
  for (int num : a)
    cout << num << ' ';
  cout << '\n' << b.size() << '\n';
  for (int num : b)
    cout << num << ' ';
}

void solve_odd() {
  if (cnt > 1)
    kill();
  for (int i = 0; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[m - 1]);
  for (int i = 0; i < m; i++)
    if (i == 0)
      add(a[i] + 1);
    else if (i == m - 1)
      add(a[i] - 1);
    else
      add(a[i]);
  print();
}

void solve_even() {
  if (cnt > 2)
    kill();
  for (int i = 0; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[0]);
  for (int i = 1; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[m - 1]);
  for (int i = 0; i < m; i++)
    if (i == m - 1)
      add(a[i] - 1);
    else if (i == 0)
      add(a[i] + 1);
    else
      add(a[i]);
  print();
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> num;
    cnt += (num & 1);
    a.push_back(num);
  }
  if (m == 1) {
    if (n % 2 == 1 || n % 4 == 0)
      abort();
    // add(n >> 1), add((n + 1) >> 1);
    else
      add(n / 2 + 1), add(n / 2 - 1);
    print();
  } else if (n & 1)
    solve_odd();
  else
    solve_even();
  return 0;
}

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

int n, m, num, cnt;
vector<int> a, b;

void kill() {
  cout << "Impossible";
  exit(0);
}

void add(int l) {
  if (l)
    b.push_back(l);
}

void print() {
  for (int num : a)
    cout << num << ' ';
  cout << '\n' << b.size() << '\n';
  for (int num : b)
    cout << num << ' ';
}

void solve_odd() {
  if (cnt > 1)
    kill();
  for (int i = 0; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[m - 1]);
  for (int i = 0; i < m; i++)
    if (i == 0)
      add(a[i] + 1);
    else if (i == m - 1)
      add(a[i] - 1);
    else
      add(a[i]);
  print();
}

void solve_even() {
  if (cnt > 2)
    kill();
  for (int i = 0; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[0]);
  for (int i = 1; i < m; i++)
    if (a[i] & 1)
      swap(a[i], a[m - 1]);
  for (int i = 0; i < m; i++)
    if (i == m - 1)
      add(a[i] - 1);
    else if (i == 0)
      add(a[i] + 1);
    else
      add(a[i]);
  print();
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> num;
    cnt += (num & 1);
    a.push_back(num);
  }
  if (m == 1) {
    if (n % 2 == 1)
      add(n >> 1), add((n + 1) >> 1);
    else
      add(n / 2 + 1), add(n / 2 - 1);
    print();
  } else if (n & 1)
    solve_odd();
  else
    solve_even();
  return 0;
}
Problem p04050 (C++)

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

#define rep(...) overload_rep(__VA_ARGS__, rep_1, rep_0)(__VA_ARGS__)
#define rep_0(i, end) for (int i = 0; i < (end); ++i)
#define rep_1(i, fr, ba) for (int i = (fr); i <= (ba); ++i)
#define overload_rep(arg0, arg1, arg2, name, ...) name
#define all(box) box.begin(), box.end()

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  vector<int> odd_place;
  rep(i, M) {
    cin >> A[i];
    if (A[i] % 2 == 1)
      odd_place.push_back(i);
  }

  if (odd_place.size() >= 3) {
    cout << "Impossible\n";
    return 0;
  }

  if (odd_place.size() >= 1)
    swap(A[0], A[odd_place[0]]);
  if (odd_place.size() >= 2)
    swap(A[M - 1], A[odd_place[1]]);
  vector<int> answer;
  if (A[0] > 1)
    answer.push_back(A[0] - 1);
  rep(i, M - 1) answer.push_back(A[i + 1]);
  ++answer.back();

  rep(i, M) {
    cout << A[i];
    cout << (i + 1 == M ? '\n' : ' ');
  }
  cout << answer.size() << '\n';
  rep(i, answer.size()) {
    cout << answer[i];
    cout << (i + 1 == answer.size() ? '\n' : ' ');
  }
}


Fixed submission:
#ifndef __cplusplus
cat << EOF
#endif
#include <bits/stdc++.h>
    using namespace std;

#define rep(...) overload_rep(__VA_ARGS__, rep_1, rep_0)(__VA_ARGS__)
#define rep_0(i, end) for (int i = 0; i < (end); ++i)
#define rep_1(i, fr, ba) for (int i = (fr); i <= (ba); ++i)
#define overload_rep(arg0, arg1, arg2, name, ...) name
#define all(box) box.begin(), box.end()

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}

int main() {

  int N, M;
  cin >> N >> M;

  if (M == 1) {
    cout << N << '\n';
    if (N >= 2)
      cout << "2\n" << N - 1 << " 1\n";
    else
      cout << "1\n1\n";
    return 0;
  }

  vector<int> A(M);
  vector<int> odd_place;
  rep(i, M) {
    cin >> A[i];
    if (A[i] % 2 == 1)
      odd_place.push_back(i);
  }

  if (odd_place.size() >= 3) {
    cout << "Impossible\n";
    return 0;
  }

  if (odd_place.size() >= 1)
    swap(A[0], A[odd_place[0]]);
  if (odd_place.size() >= 2)
    swap(A[M - 1], A[odd_place[1]]);
  vector<int> answer;
  if (A[0] > 1)
    answer.push_back(A[0] - 1);
  rep(i, M - 1) answer.push_back(A[i + 1]);
  ++answer.back();

  rep(i, M) {
    cout << A[i];
    cout << (i + 1 == M ? '\n' : ' ');
  }
  cout << answer.size() << '\n';
  rep(i, answer.size()) {
    cout << answer[i];
    cout << (i + 1 == answer.size() ? '\n' : ' ');
  }
}