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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
const int MaxN = 4e5;
typedef long long LL;
using namespace std;
int pre[MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int all, n, m;
bool ok = true;
int col[MaxN + 5], cnt[3];
void Build(int u, int v) {
  pre[++all] = last[u];
  last[u] = all;
  other[all] = v;
}

void Dfs(int x, int fa, int d) {
  if (col[x] == -1)
    col[x] = d;
  else if (col[x] != d) {
    ok = false;
    return;
  }
  int ed = last[x], dr;
  while (ed != -1) {
    dr = other[ed];
    if (dr != fa)
      Dfs(dr, x, d ^ 1);
    ed = pre[ed];
  }
}
void Init() {
  ok = true;
  all = -1;
  memset(last, -1, sizeof(last));
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    Build(u, v);
    Build(v, u);
  }
}

void Solve() {
  int t = 1;
  LL res = n * (n - 1) / 2;
  Dfs(1, 0, 1);
  cnt[0] = cnt[1] = 0;
  for (int i = 1; i <= n; i++)
    cnt[col[i]]++;
  if (ok)
    printf("%lld\n", 1LL * cnt[0] * cnt[1] - m);
  else
    printf("%lld\n", 1LL * n * (n - 1) / 2 - m);
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
const int MaxN = 4e5;
typedef long long LL;
using namespace std;
int pre[MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int all, n, m;
bool ok = true;
int col[MaxN + 5], cnt[3];
void Build(int u, int v) {
  pre[++all] = last[u];
  last[u] = all;
  other[all] = v;
}

void Dfs(int x, int fa, int d) {
  if (col[x] == -1)
    col[x] = d;
  else if (col[x] != d) {
    ok = false;
    return;
  } else
    return;
  int ed = last[x], dr;
  while (ed != -1) {
    dr = other[ed];
    if (dr != fa)
      Dfs(dr, x, d ^ 1);
    ed = pre[ed];
  }
}
void Init() {
  ok = true;
  all = -1;
  memset(last, -1, sizeof(last));
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    Build(u, v);
    Build(v, u);
  }
}

void Solve() {
  int t = 1;
  LL res = n * (n - 1) / 2;
  Dfs(1, 0, 1);
  cnt[0] = cnt[1] = 0;
  for (int i = 1; i <= n; i++)
    cnt[col[i]]++;
  if (ok)
    printf("%lld\n", 1LL * cnt[0] * cnt[1] - m);
  else
    printf("%lld\n", 1LL * n * (n - 1) / 2 - m);
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
}
Problem p03579 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int iinf = 1 << 29;
const long long linf = 1ll << 61;

#define modp(x, y) (((y) + (x) % (y)) % (y))
template <typename T> inline T gcd(T x, T y) {
  if (x < y)
    swap(x, y);
  T mod = x % y;
  if (mod == 0)
    return y;
  return gcd(y, mod);
}
template <typename T> inline T lmc(T x, T y) { return x / gcd(x, y) * y; }

int N, M;
vector<int> edges[10000];
int color[10000];

int main(int argc, char *argv[]) {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  stack<int> s;
  s.push(0);
  color[0] = 1;
  int count[2] = {1, 0};
  while (!s.empty()) {
    int v = s.top();
    s.pop();
    for (int i = 0; i < (int)edges[v].size(); i++) {
      int next = edges[v][i];
      if (color[next] == 0) {
        color[next] = 3 - color[v];
        count[color[next] - 1]++;
        s.push(next);
      } else if (color[next] == color[v]) {
        printf("%lld\n", (ll)N * (ll)(N - 1) / 2 - M);
        return 0;
      }
    }
  }
  printf("%lld\n", (ll)count[0] * (ll)count[1] - M);

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int iinf = 1 << 29;
const long long linf = 1ll << 61;

#define modp(x, y) (((y) + (x) % (y)) % (y))
template <typename T> inline T gcd(T x, T y) {
  if (x < y)
    swap(x, y);
  T mod = x % y;
  if (mod == 0)
    return y;
  return gcd(y, mod);
}
template <typename T> inline T lmc(T x, T y) { return x / gcd(x, y) * y; }

int N, M;
vector<int> edges[100000];
int color[100000];

int main(int argc, char *argv[]) {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  stack<int> s;
  s.push(0);
  color[0] = 1;
  int count[2] = {1, 0};
  while (!s.empty()) {
    int v = s.top();
    s.pop();
    for (int i = 0; i < (int)edges[v].size(); i++) {
      int next = edges[v][i];
      if (color[next] == 0) {
        color[next] = 3 - color[v];
        count[color[next] - 1]++;
        s.push(next);
      } else if (color[next] == color[v]) {
        printf("%lld\n", (ll)N * (ll)(N - 1) / 2 - M);
        return 0;
      }
    }
  }
  printf("%lld\n", (ll)count[0] * (ll)count[1] - M);

  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
struct eg {
  int v, nx;
} e[N << 1];
int head[N], tot;
inline void add(R int u, R int v) { e[++tot] = {v, head[u]}, head[u] = tot; }
ll res;
int cnt[2], dis[N], deg[N], n, m, fl;
void dfs(int u, int d) {
  dis[u] = d, ++cnt[d];
  go(u) if (dis[v] == -1) dfs(v, d ^ 1);
  else if (dis[v] != d ^ 1) fl = 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (R int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
    ++deg[u], ++deg[v];
  }
  memset(dis, -1, (n + 1) << 2);
  dfs(1, 0);
  if (fl)
    return assert(false), printf("%lld\n", (1ll * n * (n - 1) >> 1) - m), 0;
  fp(i, 1, n) res += cnt[dis[i] ^ 1] - deg[i];
  printf("%lld\n", res >> 1);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
struct eg {
  int v, nx;
} e[N << 1];
int head[N], tot;
inline void add(R int u, R int v) { e[++tot] = {v, head[u]}, head[u] = tot; }
ll res;
int cnt[2], dis[N], deg[N], n, m, fl;
void dfs(int u, int d) {
  dis[u] = d, ++cnt[d];
  go(u) if (dis[v] == -1) dfs(v, d ^ 1);
  else if (dis[v] != d ^ 1) fl = 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (R int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
    ++deg[u], ++deg[v];
  }
  memset(dis, -1, (n + 1) << 2);
  dfs(1, 0);
  if (fl)
    return printf("%lld\n", (1ll * n * (n - 1) >> 1) - m), 0;
  fp(i, 1, n) res += cnt[dis[i] ^ 1] - deg[i];
  printf("%lld\n", res >> 1);
  return 0;
}
Problem p03579 (C++)

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

const int maxn = 3000010;
int col[maxn];
vector<int> G[maxn];
#define Eor(i, u) for (int i = 0, i_end = G[u].size(), v; i < i_end; ++i)

int n, ca, cb, m;
bool flag;

void dfs(int u, int c, int fa) {
  col[u] = c;
  if (c == 0)
    ++ca;
  else
    ++cb;
  // cerr<<u<<endl;
  Eor(i, u) if ((v = G[u][i]) != fa) {
    if (col[v] == col[u]) {
      flag = 1;
      return;
      continue;
    }
    dfs(v, c ^ 1, u);
    if (flag)
      return;
  }
}

int main() {
  memset(col, -1, sizeof col);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  flag = 0;
  // cerr<<flag<<endl;
  dfs(1, 0, -1);
  if (flag) {
    // printf("%d\n",n*(n-1)/2-m);
    cout << 1ll * n * (n - 1) / 2 - m << endl;
    // puts("wea");
  } else {
    // printf("%d\n",ca*cb-m);
    cout << 1ll * ca * cb - m << endl;
  }
  // cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
  return 0;
}

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

const int maxn = 3000010;
int col[maxn];
vector<int> G[maxn];
#define Eor(i, u) for (int i = 0, i_end = G[u].size(), v; i < i_end; ++i)

int n, ca, cb, m;
bool flag;

void dfs(int u, int c, int fa) {
  col[u] = c;
  if (c == 0)
    ++ca;
  else
    ++cb;
  // cerr<<u<<endl;
  Eor(i, u) if ((v = G[u][i]) != fa) {
    if (col[v] == col[u]) {
      flag = 1;
      return;
      continue;
    }
    if (!~col[v])
      dfs(v, c ^ 1, u);
    if (flag)
      return;
  }
}

int main() {
  memset(col, -1, sizeof col);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  flag = 0;
  // cerr<<flag<<endl;
  dfs(1, 0, -1);
  if (flag) {
    // printf("%d\n",n*(n-1)/2-m);
    cout << 1ll * n * (n - 1) / 2 - m << endl;
    // puts("wea");
  } else {
    // printf("%d\n",ca*cb-m);
    cout << 1ll * ca * cb - m << endl;
  }
  // cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
  return 0;
}
Problem p03579 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter


def dfs(x):
    res = True
    for e in edge[x]:
        if color[e] is None:
            color[e] = 0 if color[x] else 1
            res &= dfs(e)
        else:
            if color[e] == color[x]:
                res &= False
            else:
                res &= True
    return res


N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    edge[A].append(B)
    edge[B].append(A)

color = [None] * N
color[0] = 0
isBin = dfs(0)

if isBin:
    C = Counter(color)
    print(C[0] * C[1] - M)
else:
    print(N * (N - 1) // 2 - M)


Fixed submission:
from collections import Counter
import sys


sys.setrecursionlimit(10**6)


def dfs(x):
    res = True
    for e in edge[x]:
        if color[e] is None:
            color[e] = 0 if color[x] else 1
            res &= dfs(e)
        else:
            if color[e] == color[x]:
                res &= False
            else:
                res &= True
    return res


N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    edge[A].append(B)
    edge[B].append(A)

color = [None] * N
color[0] = 0
isBin = dfs(0)

if isBin:
    C = Counter(color)
    print(C[0] * C[1] - M)
else:
    print(N * (N - 1) // 2 - M)
Problem p03579 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3

import sys


# 再帰深度の制限
REC_LIMIT = 10000

# 頂点の色
UNDEF = 0
WHITE = 1
BLACK = -WHITE


# 所与のグラフが二部グラフならば，(True, color[])を返す
# さもなければ，(False, None)を返す
# 参考：『プログラミングコンテストチャレンジブック』（第2版）p.94
def is_bipartite_graph(adj_list):
    n = len(adj_list)  # 頂点数
    color = [UNDEF] * n

    # 頂点をWHITEとBLACKで塗っていく
    def dfs(v, c):
        color[v] = c
        for u in adj_list[v]:
            if color[u] == c:
                # 隣接頂点が同色で塗られていたら不可
                return False
            if color[u] == UNDEF and not dfs(u, -c):
                # 隣接頂点が未彩色なら，反対の色で塗れるか
                return False
        return True  # すべて色を塗れた

    for i in range(n):
        if color[i] == 0:
            # 未彩色なら，WHITEで塗ってみる
            if not dfs(i, WHITE):
                return (False, None)
    return (True, color)


def compute_max_additional_edges(n, m, adj_list):
    is_bi, color = is_bipartite_graph(adj_list)
    if not is_bi:
        # 二部グラフではない場合
        return n * (n - 1) // 2 - m
    else:
        # 二部グラフである場合
        w = sum(c == WHITE for c in color)
        b = sum(c == BLACK for c in color)
        return w * b - m


def main():
    # 最大再帰深度の再設定
    sys.setrecursionlimit(REC_LIMIT)
    # 入力の読み込み
    n, m = (int(x) for x in input().split())
    adj_list = [set() for _ in range(n)]
    for _ in range(m):
        a, b = (int(x) - 1 for x in input().split())
        adj_list[a].add(b)
        adj_list[b].add(a)
    res = compute_max_additional_edges(n, m, adj_list)
    print(res)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3

import sys


# 再帰深度の制限
REC_LIMIT = 100000

# 頂点の色
UNDEF = 0
WHITE = 1
BLACK = -WHITE


# 所与のグラフが二部グラフならば，(True, color[])を返す
# さもなければ，(False, None)を返す
# 参考：『プログラミングコンテストチャレンジブック』（第2版）p.94
def is_bipartite_graph(adj_list):
    n = len(adj_list)  # 頂点数
    color = [UNDEF] * n

    # 頂点をWHITEとBLACKで塗っていく
    def dfs(v, c):
        color[v] = c
        for u in adj_list[v]:
            if color[u] == c:
                # 隣接頂点が同色で塗られていたら不可
                return False
            if color[u] == UNDEF and not dfs(u, -c):
                # 隣接頂点が未彩色なら，反対の色で塗れるか
                return False
        return True  # すべて色を塗れた

    for i in range(n):
        if color[i] == 0:
            # 未彩色なら，WHITEで塗ってみる
            if not dfs(i, WHITE):
                return (False, None)
    return (True, color)


def compute_max_additional_edges(n, m, adj_list):
    is_bi, color = is_bipartite_graph(adj_list)
    if not is_bi:
        # 二部グラフではない場合
        return n * (n - 1) // 2 - m
    else:
        # 二部グラフである場合
        w = sum(c == WHITE for c in color)
        b = sum(c == BLACK for c in color)
        return w * b - m


def main():
    # 最大再帰深度の再設定
    sys.setrecursionlimit(REC_LIMIT)
    # 入力の読み込み
    n, m = (int(x) for x in input().split())
    adj_list = [set() for _ in range(n)]
    for _ in range(m):
        a, b = (int(x) - 1 for x in input().split())
        adj_list[a].add(b)
        adj_list[b].add(a)
    res = compute_max_additional_edges(n, m, adj_list)
    print(res)


if __name__ == "__main__":
    main()
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

vvl G = vv(200000, 0, 0, ll);
vector<pair<bool, bool>> t(200000, make_pair(false, false));

ll n, m;
void dfs(int now, int from, int mode) {
  if (mode == 1 ? t[now].second : t[now].first) {
    std::cout << (n * (n - 1)) / 2 - m << '\n';
    exit(0);
  }
  (mode == 1 ? t[now].first : t[now].second) = true;
  for (int i = 0; i < G[now].size(); i++) {
    if (G[now][i] == from)
      continue;
    dfs(G[now][i], now, -1 * mode);
  }
}
int main(int argc, char const *argv[]) {
  ll a, b;
  std::cin >> n >> m;
  re(i, m) {
    std::cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, -1, 1);
  ll x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i].second)
      x++;
    if (t[i].first)
      y++;
  }
  std::cout << x * y - m << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

vvl G = vv(200000, 0, 0, ll);
vector<pair<bool, bool>> t(200000, make_pair(false, false));

ll n, m;
void dfs(int now, int from, int mode) {
  if (mode == 1 ? t[now].second : t[now].first) {
    std::cout << (n * (n - 1)) / 2 - m << '\n';
    exit(0);
  }
  if (mode == 1 ? t[now].first : t[now].second)
    return;
  (mode == 1 ? t[now].first : t[now].second) = true;
  for (int i = 0; i < G[now].size(); i++) {
    if (G[now][i] == from)
      continue;
    dfs(G[now][i], now, -1 * mode);
  }
}
int main(int argc, char const *argv[]) {
  ll a, b;
  std::cin >> n >> m;
  re(i, m) {
    std::cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, -1, 1);
  ll x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i].second)
      x++;
    if (t[i].first)
      y++;
  }
  std::cout << x * y - m << '\n';
  return 0;
}
Problem p03579 (Python)

Buggy submission (status: Runtime Error):
# 二部グラフ判定
def dfs(u, c):
    color[u] = c  # 訪問したら指定の色で塗る
    for v in G[u]:  # uからつながっている頂点を順に取り出す
        if color[v] != -1:  # すでに訪問していた場合
            if color[v] == c:
                return False  # 隣接する頂点が同じ色のときはFalse
            else:
                continue
        else:  # 未訪問の時は色は違う色にしてDFSで再帰的に探索
            if not dfs(v, 1 - c):  # FalseだったらFalseを返す
                return False
    return True


# 頂点数と辺数を入力
N, M = map(int, input().split())
# グラフの入力
G = [[] for _ in range(N)]  # 頂点数Nだけ準備
color = [-1] * N  # 未訪問は-1とする
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
# 探索
flag = True
for i in range(N):
    if color[i] == -1:
        if not dfs(i, 0):
            flag = False
            break
if flag:
    ans = color.count(1) * color.count(0) - M
else:
    ans = N * (N - 1) // 2 - M
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(1000000)


# 二部グラフ判定
def dfs(u, c):
    color[u] = c  # 訪問したら指定の色で塗る
    for v in G[u]:  # uからつながっている頂点を順に取り出す
        if color[v] != -1:  # すでに訪問していた場合
            if color[v] == c:
                return False  # 隣接する頂点が同じ色のときはFalse
            else:
                continue
        else:  # 未訪問の時は色は違う色にしてDFSで再帰的に探索
            if not dfs(v, 1 - c):  # FalseだったらFalseを返す
                return False
    return True


# 頂点数と辺数を入力
N, M = map(int, input().split())
# グラフの入力
G = [[] for _ in range(N)]  # 頂点数Nだけ準備
color = [-1] * N  # 未訪問は-1とする
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
# 探索
flag = True
for i in range(N):
    if color[i] == -1:
        if not dfs(i, 0):
            flag = False
            break
if flag:
    ans = color.count(1) * color.count(0) - M
else:
    ans = N * (N - 1) // 2 - M
print(ans)
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vvi edges(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vi col(n, 0);
  col[0] = 1;
  bool nibu = true;
  queue<int> q, emp;
  q.push(0);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    for (auto &ne : edges[c]) {
      if (col[ne] == 0) {
        col[ne] = -col[c];
        q.push(ne);
      } else if (col[ne] == col[c]) {
        nibu = false;
        swap(q, emp);
        break;
      }
    }
  }
  if (nibu) {
    ll x = 0, y = 0;
    for (int i = 0; i < n; ++i) {
      if (col[i] == 1)
        x++;
      else
        y++;
    }
    cout << x * y - m << "\n";
  } else {
    assert(false);
    cout << n * (n - 1) / 2 - m << "\n";
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vvi edges(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vi col(n, 0);
  col[0] = 1;
  bool nibu = true;
  queue<int> q, emp;
  q.push(0);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    for (auto &ne : edges[c]) {
      if (col[ne] == 0) {
        col[ne] = -col[c];
        q.push(ne);
      } else if (col[ne] == col[c]) {
        nibu = false;
        swap(q, emp);
        break;
      }
    }
  }
  if (nibu) {
    ll x = 0, y = 0;
    for (int i = 0; i < n; ++i) {
      if (col[i] == 1)
        x++;
      else
        y++;
    }
    cout << x * y - m << "\n";
  } else {
    cout << n * (n - 1) / 2 - m << "\n";
  }
  return 0;
}
Problem p03579 (C++)

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

int first[maxn], col[maxn], cnt[3];
struct edg {
  int next, to;
} e[maxn];
int n, m, flag, e_sum;
long long ans;

inline 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 add_edg(int x, int y) {
  e_sum++;
  e[e_sum].next = first[x];
  first[x] = e_sum;
  e[e_sum].to = y;
}

void dfs(int x) {
  cnt[col[x]]++;
  for (int i = first[x]; i; i = e[i].next) {
    int w = e[i].to;
    if (!col[w])
      col[w] = 3 - col[x], dfs(w);
    else if (col[w] == col[x])
      flag = 1;
  }
}

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    add_edg(x, y);
    add_edg(y, x);
  }
  for (int i = 1; i <= n; i++)
    if (!col[i]) {
      cnt[1] = cnt[2] = 0;
      col[i] = 1;
      flag = 0;
      dfs(i);
      if (flag)
        ans += 1ll * (cnt[1] + cnt[2]) * (cnt[1] + cnt[2] - 1) / 2;
      else
        ans += 1ll * cnt[1] * cnt[2];
    }
  cout << ans - m << endl;
  return 0;
}


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

int first[maxn], col[maxn], cnt[3];
struct edg {
  int next, to;
} e[maxn << 1];
int n, m, flag, e_sum;
long long ans;

inline 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 add_edg(int x, int y) {
  e_sum++;
  e[e_sum].next = first[x];
  first[x] = e_sum;
  e[e_sum].to = y;
}

void dfs(int x) {
  cnt[col[x]]++;
  for (int i = first[x]; i; i = e[i].next) {
    int w = e[i].to;
    if (!col[w])
      col[w] = 3 - col[x], dfs(w);
    else if (col[w] == col[x])
      flag = 1;
  }
}

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    add_edg(x, y);
    add_edg(y, x);
  }
  for (int i = 1; i <= n; i++)
    if (!col[i]) {
      cnt[1] = cnt[2] = 0;
      col[i] = 1;
      flag = 0;
      dfs(i);
      if (flag)
        ans += 1ll * (cnt[1] + cnt[2]) * (cnt[1] + cnt[2] - 1) / 2;
      else
        ans += 1ll * cnt[1] * cnt[2];
    }
  cout << ans - m << endl;
  return 0;
}
Problem p03579 (C++)

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

const int MAXN = 1e5 + 10;
struct Edge {
  int to, next;
} edge[MAXN];
int head[MAXN];
int tot;
int col[MAXN];
long long c1, c2;
bool flag;

void addedge(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}

void dfs(int u, int color) {
  if (flag)
    return;
  col[u] = color;
  if (color == 1)
    ++c1;
  else
    ++c2;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (!col[v])
      dfs(v, 3 - color);
    else if (col[v] == color) {
      flag = true;
      return;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  int u, v;
  memset(head, -1, sizeof(head));
  memset(col, 0, sizeof(col));
  c1 = c2 = tot = 0;
  flag = false;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 1);
  if (flag)
    cout << n * (n - 1) / 2 - m;
  else
    cout << c1 * c2 - m << endl;
  return 0;
}


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

const int MAXN = 1e5 + 10;
struct Edge {
  int to, next;
} edge[MAXN * 2];
int head[MAXN];
int tot;
int col[MAXN];
long long c1, c2;
bool flag;

void addedge(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}

void dfs(int u, int color) {
  if (flag)
    return;
  col[u] = color;
  if (color == 1)
    ++c1;
  else
    ++c2;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (!col[v])
      dfs(v, 3 - color);
    else if (col[v] == color) {
      flag = true;
      return;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  int u, v;
  memset(head, -1, sizeof(head));
  memset(col, 0, sizeof(col));
  c1 = c2 = tot = 0;
  flag = false;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 1);
  if (flag)
    cout << n * (n - 1) / 2 - m;
  else
    cout << c1 * c2 - m << endl;
  return 0;
}
Problem p03579 (C++)

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

#define int long long
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;

template <class T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}

vector<int> A[10005];
int color[100005];
bool Odd = false;
int num = 1;

void odd(int pos) {
  for (auto x : A[pos]) {
    if (color[x] == -1) {
      color[x] = (color[pos] + 1) % 2;
      if (color[x] == 0)
        num++;
      odd(x);
    } else if (color[x] == color[pos]) {
      Odd = true;
    }
  }
  return;
}

signed main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int x = in() - 1, y = in() - 1;
    A[x].push_back(y);
    A[y].push_back(x);
  }
  REP(i, 100005) { color[i] = -1; }

  color[0] = 0;
  odd(0);

  int ans = 0;
  if (Odd) {
    ans = n * (n - 1) / 2 - m;
  } else {
    ans = (n - num) * num - m;
  }
  cout << ans << endl;
}

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

#define int long long
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;

template <class T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}

vector<int> A[100005];
int color[100005];
bool Odd = false;
int num = 1;

void odd(int pos) {
  for (auto x : A[pos]) {
    if (color[x] == -1) {
      color[x] = (color[pos] + 1) % 2;
      if (color[x] == 0)
        num++;
      odd(x);
    } else if (color[x] == color[pos]) {
      Odd = true;
    }
  }
  return;
}

signed main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int x = in() - 1, y = in() - 1;
    A[x].push_back(y);
    A[y].push_back(x);
  }
  REP(i, 100005) { color[i] = -1; }

  color[0] = 0;
  odd(0);

  int ans = 0;
  if (Odd) {
    ans = n * (n - 1) / 2 - m;
  } else {
    ans = (n - num) * num - m;
  }
  cout << ans << endl;
}
Problem p03580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
const int N = 5e5 + 5;
int n;
char str[N];
int dp[N];
int nxt[N];
int main() {
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = n; i >= 1; --i) {
    if (str[i] == '0') {
      nxt[i] = 0;
    } else {
      nxt[i] = 1 + nxt[i + 1];
    }
  }
  for (int i = n; i >= 1; --i) {
    if (str[i] == '0') {
      dp[i] = dp[i + 1];
      continue;
    }
    int r = i + nxt[i] - 1;
    if (r == n) {
      dp[i] = 0;
      continue;
    }
    int mid = r + 1;
    if (mid == n) {
      dp[i] = 0;
      continue;
    }
    dp[i] = dp[i + 1];
    if (str[mid + 1] == '0') {
      continue;
    }
    int lft = nxt[i];
    int rgt = nxt[mid + 1];
    dp[i] = max(dp[i], lft + dp[mid + 2]);
    for (int j = mid + 1; j <= mid + rgt; ++j) {
      dp[i] = max(dp[i], dp[j + 1] + j - mid);
    }
  }
  printf("%d\n", dp[1]);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int N = 5e5 + 5;
int n;
char str[N];
int dp[N];
int nxt[N];
int main() {
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = n; i >= 1; --i) {
    if (str[i] == '0') {
      nxt[i] = 0;
    } else {
      nxt[i] = 1 + nxt[i + 1];
    }
  }
  for (int i = n; i >= 1; --i) {
    if (str[i] == '0') {
      dp[i] = dp[i + 1];
      continue;
    }
    int r = i + nxt[i] - 1;
    if (r == n) {
      dp[i] = 0;
      continue;
    }
    int mid = r + 1;
    if (mid == n) {
      dp[i] = 0;
      continue;
    }
    dp[i] = dp[i + 1];
    if (str[mid + 1] == '0') {
      continue;
    }
    int lft = nxt[i];
    int rgt = nxt[mid + 1];
    dp[i] = max(dp[i], lft + dp[mid + 2]);
    if (str[i + 1] == '0') {
      for (int j = mid + 1; j <= mid + rgt; ++j) {
        dp[i] = max(dp[i], dp[j + 1] + j - mid);
      }
    }
  }
  printf("%d\n", dp[1]);
}
Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char str[100010];
int dp[100010], L[100010];
int main() {
  int n;
  cin >> n;
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) {
    if (str[i] == '1')
      L[i] = L[i - 1];
    else
      L[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (str[i] == '1') {
      if (str[i - 1] == '0' and str[i - 2] == '1') {
        dp[i] = max(dp[i], dp[L[i - 2]] + (i - 2) - L[i - 2]);
        dp[i] = max(dp[i], dp[L[i - 2] + 1] + (i - 2) - L[i - 2] - 1);
      } else if (L[i] > 1 and str[L[i] - 1] == '1') {
        dp[i] = max(dp[i], dp[L[i] - 2] + i - L[i]);
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char str[1000010];
int dp[1000010], L[1000010];
int main() {
  int n;
  cin >> n;
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) {
    if (str[i] == '1')
      L[i] = L[i - 1];
    else
      L[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (str[i] == '1') {
      if (str[i - 1] == '0' and str[i - 2] == '1') {
        dp[i] = max(dp[i], dp[L[i - 2]] + (i - 2) - L[i - 2]);
        dp[i] = max(dp[i], dp[L[i - 2] + 1] + (i - 2) - L[i - 2] - 1);
      } else if (L[i] > 1 and str[L[i] - 1] == '1') {
        dp[i] = max(dp[i], dp[L[i] - 2] + i - L[i]);
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 200005;
const int K = 31;
const int M = 10921000;
const int INF = 0x3f3f3f3f;
const LL MOD = 998244353;
int n, m;
int a, b;
int dp[N];
char s[N];
int main() {
  while (~scanf("%d", &n)) {
    scanf("%s", s + 1);
    int l = n;
    if (l <= 2) {
      printf("0\n");
      continue;
    }
    memset(dp, 0, sizeof dp);
    int f = 0;
    for (int i = 3; i <= l; i++) {
      dp[i] = dp[i - 1];
      if (s[i] == '0')
        f = 0;
      else if (s[i - 2] == '1' && s[i - 1] == '0') {
        f = i;
        for (int j = i - 2; s[j] != '0' && j > 0; j--)
          dp[i] = max(dp[i], dp[j - 1] + i - 1 - j);
      }
      if (f)
        dp[i] = max(dp[i], dp[f - 3] + i - f + 1);
    }
    // for (int i=1;i<=n;i++)
    //     cout<<dp[i]<<" ";
    // cout<<endl;
    printf("%d\n", dp[n]);
  }
  return 0;
}

/*

6 5
1 2
2 3
3 4
4 5
5 6

*/

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 500005;
const int K = 31;
const int M = 10921000;
const int INF = 0x3f3f3f3f;
const LL MOD = 998244353;
int n, m;
int a, b;
int dp[N];
char s[N];
int main() {
  while (~scanf("%d", &n)) {
    scanf("%s", s + 1);
    int l = n;
    if (l <= 2) {
      printf("0\n");
      continue;
    }
    memset(dp, 0, sizeof dp);
    int f = 0;
    for (int i = 3; i <= l; i++) {
      dp[i] = dp[i - 1];
      if (s[i] == '0')
        f = 0;
      else if (s[i - 2] == '1' && s[i - 1] == '0') {
        f = i;
        for (int j = i - 2; s[j] != '0' && j > 0; j--)
          dp[i] = max(dp[i], dp[j - 1] + i - 1 - j);
      }
      if (f)
        dp[i] = max(dp[i], dp[f - 3] + i - f + 1);
    }
    // for (int i=1;i<=n;i++)
    //     cout<<dp[i]<<" ";
    // cout<<endl;
    printf("%d\n", dp[n]);
  }
  return 0;
}

/*

6 5
1 2
2 3
3 4
4 5
5 6

*/
Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

ll A[100005];
ll dp[100005];

void I_m_Beast() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    A[i + 1] = s[i] - '0';
  }
  A[0] = -1;
  ll cum[n + 1], idx[n + 1];
  cum[0] = 0;
  idx[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (A[i] == 0)
      idx[i] = i;
    else
      idx[i] = idx[i - 1];
    cum[i] = cum[i - 1] + A[i];
    dp[i] = 0;
  }

  dp[0] = 0;
  for (int i = 3; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (A[i] != 0) {
      if (A[i - 1] == 0 && A[i - 2] != 0) {
        int x = idx[i - 2];
        dp[i] = max(dp[i], dp[x] + cum[i - 3] - cum[x] + max(A[i], A[i - 2]));
        dp[i] = max(dp[i],
                    dp[x + 1] + cum[i - 3] - cum[x + 1] + max(A[i], A[i - 2]));
      } else if (A[i - 1] != 0) {
        int x = idx[i];
        if (x > 1 && A[x - 1] != 0)
          dp[i] = max(dp[i], dp[x - 2] + cum[i] - cum[x + 1] +
                                 max(A[x - 1], A[x + 1]));
      }
    }
  }
  cout << dp[n] << endl;
  return;
}

int main() {
  fastio;
  int t = 1;
  // cin>>t;
  while (t--) {
    I_m_Beast();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

ll A[500005];
ll dp[500005];

void I_m_Beast() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    A[i + 1] = s[i] - '0';
  }
  A[0] = -1;
  ll cum[n + 1], idx[n + 1];
  cum[0] = 0;
  idx[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (A[i] == 0)
      idx[i] = i;
    else
      idx[i] = idx[i - 1];
    cum[i] = cum[i - 1] + A[i];
    dp[i] = 0;
  }

  dp[0] = 0;
  for (int i = 3; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (A[i] != 0) {
      if (A[i - 1] == 0 && A[i - 2] != 0) {
        int x = idx[i - 2];
        dp[i] = max(dp[i], dp[x] + cum[i - 3] - cum[x] + max(A[i], A[i - 2]));
        dp[i] = max(dp[i],
                    dp[x + 1] + cum[i - 3] - cum[x + 1] + max(A[i], A[i - 2]));
      } else if (A[i - 1] != 0) {
        int x = idx[i];
        if (x > 1 && A[x - 1] != 0)
          dp[i] = max(dp[i], dp[x - 2] + cum[i] - cum[x + 1] +
                                 max(A[x - 1], A[x + 1]));
      }
    }
  }
  cout << dp[n] << endl;
  return;
}

int main() {
  fastio;
  int t = 1;
  // cin>>t;
  while (t--) {
    I_m_Beast();
  }
  return 0;
}
Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
char str[MAX_N];
int dp[MAX_N], L[MAX_N];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) {
    if (str[i] == '1')
      L[i] = L[i - 1];
    else
      L[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (str[i] == '1') {
      if (str[i - 1] == '0' && str[i - 2] == '1') {
        dp[i] = max(dp[i], dp[L[i - 2]] + (i - 2) - L[i - 2]);
        dp[i] = max(dp[i], dp[L[i - 2] + 1] + (i - 2) - L[i - 2] - 1);
      } else if (L[i] > 1 && str[L[i] - 1] == '1') {
        dp[i] = max(dp[i], dp[L[i] - 2] + i - L[i]);
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500005;
char str[MAX_N];
int dp[MAX_N], L[MAX_N];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) {
    if (str[i] == '1')
      L[i] = L[i - 1];
    else
      L[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (str[i] == '1') {
      if (str[i - 1] == '0' && str[i - 2] == '1') {
        dp[i] = max(dp[i], dp[L[i - 2]] + (i - 2) - L[i - 2]);
        dp[i] = max(dp[i], dp[L[i - 2] + 1] + (i - 2) - L[i - 2] - 1);
      } else if (L[i] > 1 && str[L[i] - 1] == '1') {
        dp[i] = max(dp[i], dp[L[i] - 2] + i - L[i]);
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03580 (C++)

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

using namespace std;

typedef long long ll;

const int MAX_N = 5e5 + 5;

int N;
char s[MAX_N];
int pre[MAX_N], nxt[MAX_N];
int dp[MAX_N];

void solve() {
  assert(0);
  pre[0] = s[0] == '0' ? 0 : -1;
  for (int i = 1; i < N; ++i) {
    if (s[i] == '0') {
      pre[i] = i;
    } else {
      pre[i] = pre[i - 1];
    }
  }
  nxt[N - 1] = s[N - 1] == '0' ? N - 1 : N;
  for (int i = N - 2; i >= 0; --i) {
    if (s[i] == '0') {
      nxt[i] = i;
    } else {
      nxt[i] = nxt[i + 1];
    }
  }
  for (int i = 0; i < N; ++i) {
    dp[i + 1] = max(dp[i + 1], dp[i]);
    if (i - 2 >= 0 && s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '1') {
      int j = pre[i - 2];
      dp[i + 1] = max(dp[i + 1], dp[j + 1] + i - 2 - j);
      if (j != i - 3) {
        dp[i + 1] = max(dp[i + 1], dp[j + 2] + i - 3 - j);
      }
    }
    if (i + 2 < N && s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') {
      int j = nxt[i + 2];
      dp[j] = max(dp[j], dp[i] + j - i - 2);
      if (j != i + 3) {
        dp[j - 1] = max(dp[j - 1], dp[i] + j - i - 3);
      }
    }
  }
  printf("%d\n", dp[N]);
}

int main() {
#ifdef linjian
  freopen("C:\\Users\\linjian\\Documents\\in.txt", "r", stdin);
#endif
  scanf("%d%s", &N, s);
  solve();
  return 0;
}


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

using namespace std;

typedef long long ll;

const int MAX_N = 5e5 + 5;

int N;
char s[MAX_N];
int pre[MAX_N], nxt[MAX_N];
int dp[MAX_N];

void solve() {
  assert(0);
  pre[0] = s[0] == '0' ? 0 : -1;
  for (int i = 1; i < N; ++i) {
    if (s[i] == '0') {
      pre[i] = i;
    } else {
      pre[i] = pre[i - 1];
    }
  }
  nxt[N - 1] = s[N - 1] == '0' ? N - 1 : N;
  for (int i = N - 2; i >= 0; --i) {
    if (s[i] == '0') {
      nxt[i] = i;
    } else {
      nxt[i] = nxt[i + 1];
    }
  }
  for (int i = 0; i < N; ++i) {
    dp[i + 1] = max(dp[i + 1], dp[i]);
    if (i - 2 >= 0 && s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '1') {
      int j = pre[i - 2];
      dp[i + 1] = max(dp[i + 1], dp[j + 1] + i - 2 - j);
      if (j != i - 3) {
        dp[i + 1] = max(dp[i + 1], dp[j + 2] + i - 3 - j);
      }
    }
    if (i + 2 < N && s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') {
      int j = nxt[i + 2];
      dp[j] = max(dp[j], dp[i] + j - i - 2);
      if (j != i + 3) {
        dp[j - 1] = max(dp[j - 1], dp[i] + j - i - 3);
      }
    }
  }
  printf("%d\n", dp[N]);
}

int main() {
#ifdef linjian
  freopen("C:\\Users\\linjian\\Documents\\in.txt", "r", stdin);
#endif
  scanf("%d%s", &N, s);
  solve();
  return 0;
}
Problem p03581 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;
const int MAXN = 4005;

int n, m;
int bino[MAXN][MAXN];
int dp[MAXN][MAXN];
int ps[MAXN][MAXN];

int choice(int x, int y) {
  if (y > x || y < 0)
    return 0;
  return bino[x][y];
}

int main() {
  cin >> n >> m;
  for (int i = 0; i <= 4000; i++) {
    bino[i][0] = 1;
    ps[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      bino[i][j] = (bino[i - 1][j] + bino[i - 1][j - 1]) % mod;
      ps[i][j] = (ps[i][j - 1] + bino[i][j]) % mod;
    }
  }
  for (int i = 0; i < MAXN; i++) {
    for (int j = 0; j < MAXN; j++) {
      if (i == 0) {
        dp[i][j] = 1;
        continue;
      }
      dp[i][j] = (dp[i - 1][j] + ps[j - 1][min(j - 1, i)]) % mod;
    }
  }
  lint ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      int p = j, q = i - j;
      ans += 1ll * dp[p][q] * choice(m - 1, m - q - 1);
      ans %= mod;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;
const int MAXN = 4005;

int n, m;
int bino[MAXN][MAXN];
int dp[MAXN][MAXN];
int ps[MAXN][MAXN];

int choice(int x, int y) {
  if (y > x || y < 0)
    return 0;
  return bino[x][y];
}

int main() {
  cin >> n >> m;
  for (int i = 0; i <= 4000; i++) {
    bino[i][0] = 1;
    ps[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      bino[i][j] = (bino[i - 1][j] + bino[i - 1][j - 1]) % mod;
      ps[i][j] = (ps[i][j - 1] + bino[i][j]) % mod;
    }
  }
  for (int i = 0; i < MAXN; i++) {
    for (int j = 0; j < MAXN; j++) {
      if (i == 0) {
        dp[i][j] = 1;
        continue;
      }
      dp[i][j] = dp[i - 1][j];
      if (j > 0)
        dp[i][j] += ps[j - 1][min(j - 1, i)];
      dp[i][j] %= mod;
    }
  }
  lint ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      int p = j, q = i - j;
      ans += 1ll * dp[p][q] * choice(m - 1, m - q - 1);
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p03581 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <set>
const int mod = 1000000007;
int bf(int A, int B) {
  int ans = 0;
  for (int S = 0; S < 1 << A + B; S++) {
    bool fl = 0;
    int rt = 0;
    for (int i = 0; i < A + B; i++)
      if (S >> i & 1)
        rt++;
    if (rt != A)
      continue;
    int s = 0, p = A + B - 1, t = 0;
    while (!(S >> s & 1))
      s++;
    while (S >> s & 1)
      s++;
    while (S >> p & 1)
      p--;
    while (p--)
      if (S >> p & 1)
        t++;
    int r = A, b = B, i = 0;
    for (; i < A + B; i++) {
      if (S >> A + B - i - 1 & 1)
        r--;
      else if (0 >= r && 0 < r + b || s >= r && s < r + b ||
               t >= r && t < r + b)
        b--;
      else
        break;
    }
    if (i == A + B)
      fl = 1;
    if (fl)
      ans++;
  }
  return ans;
}
int C[2010][2010], f[2010][2010];
int calc(int A, int B) {
  for (int i = 0; i <= A || i <= B; i++)
    for (int j = *C[i] = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  int s = 0;
  for (int x = 0; x <= A; x++)
    for (int y = 0; x + y <= A; y++)
      f[x][y] = x && y ? (f[x - 1][y] + C[y - 1][x]) % mod : 1;
  for (int x = 0; x <= A; x++) {
    for (int y = 0; x + y <= A; y++) {
      if (y)
        (f[x][y] += f[x - 1][y]) %= mod;
      s = (s + 1ll * f[x][y] * C[B - 1][y]) % mod; //(x,y)->(x+y,B-1)
    }
  }
  return s;
}
int main() {
  int A, B;
  scanf("%d%d", &A, &B);
  printf("%d\n", calc(A, B));
}

Fixed submission:
#include <cstdio>
#include <set>
const int mod = 1000000007;
int bf(int A, int B) {
  int ans = 0;
  for (int S = 0; S < 1 << A + B; S++) {
    bool fl = 0;
    int rt = 0;
    for (int i = 0; i < A + B; i++)
      if (S >> i & 1)
        rt++;
    if (rt != A)
      continue;
    int s = 0, p = A + B - 1, t = 0;
    while (!(S >> s & 1))
      s++;
    while (S >> s & 1)
      s++;
    while (S >> p & 1)
      p--;
    while (p--)
      if (S >> p & 1)
        t++;
    int r = A, b = B, i = 0;
    for (; i < A + B; i++) {
      if (S >> A + B - i - 1 & 1)
        r--;
      else if (0 >= r && 0 < r + b || s >= r && s < r + b ||
               t >= r && t < r + b)
        b--;
      else
        break;
    }
    if (i == A + B)
      fl = 1;
    if (fl)
      ans++;
  }
  return ans;
}
int C[2010][2010], f[2010][2010];
int calc(int A, int B) {
  for (int i = 0; i <= A || i <= B; i++)
    for (int j = *C[i] = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  int s = 0;
  for (int x = 0; x <= A; x++)
    for (int y = 0; x + y <= A; y++)
      f[x][y] = x && y ? (f[x - 1][y] + C[y - 1][x]) % mod : 1;
  for (int x = 0; x <= A; x++) {
    for (int y = 0; x + y <= A; y++) {
      if (x && y)
        (f[x][y] += f[x - 1][y]) %= mod;
      s = (s + 1ll * f[x][y] * C[B - 1][y]) % mod; //(x,y)->(x+y,B-1)
    }
  }
  return s;
}
int main() {
  int A, B;
  scanf("%d%d", &A, &B);
  printf("%d\n", calc(A, B));
}