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

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef vector<int> vi;
typedef long long ll;

int main() {
  ll N, M, D[200100], T[200100];
  cin >> N;
  rep(i, 0, N) { cin >> D[i]; }
  cin >> M;
  rep(i, 0, M) { cin >> T[i]; }
  sort(D, D + N);
  sort(T, T + M);
  ll now = 0, ok = 0;
  rep(i, 0, M) {
    rep(j, now, N) {
      if (T[i] == D[j]) {
        D[j] = 0;
        ok = 1;
        break;
      }
    }
    if (!ok) {
      cout << "NO" << endl;
      return 0;
    }
    ok = 0;
  }
  cout << "YES" << endl;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef vector<int> vi;
typedef long long ll;

int main() {
  ll N, M, D[200100], T[200100];
  cin >> N;
  rep(i, 0, N) { cin >> D[i]; }
  cin >> M;
  rep(i, 0, M) { cin >> T[i]; }
  sort(D, D + N);
  sort(T, T + M);
  ll now = 0, ok = 0;
  rep(i, 0, M) {
    rep(j, now, N) {
      if (T[i] == D[j]) {
        D[j] = 0;
        ok = 1;
        now = j;
        break;
      }
    }
    if (!ok) {
      cout << "NO" << endl;
      return 0;
    }
    ok = 0;
  }
  cout << "YES" << endl;
}
Problem p03578 (C++)

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

typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1000000007;
const double pi = 3.141592653589793238;

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}
// a と b の最小公倍数を返す関数
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  unordered_map<int, int> p;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (p.count(a) > 0) {
      p.at(a)++;
    } else {
      p.emplace(a, 1);
    }
  }

  int M;
  cin >> M;
  if (M > N) {
    cout << "NO" << endl;
    return 0;
  }

  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    if (p.count(a) < 0) {
      cout << "NO" << endl;
      return 0;
    } else {
      p.at(a)--;
      if (p.at(a) < 0) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  cout << "YES" << endl;
}


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

typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1000000007;
const double pi = 3.141592653589793238;

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}
// a と b の最小公倍数を返す関数
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  unordered_map<int, int> p;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (p.count(a) > 0) {
      p.at(a)++;
    } else {
      p.emplace(a, 1);
    }
  }

  int M;
  cin >> M;
  if (M > N) {
    cout << "NO" << endl;
    return 0;
  }

  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    if (p.count(a) == 0) {
      cout << "NO" << endl;
      return 0;
    } else {
      p.at(a)--;
      if (p.at(a) < 0) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  cout << "YES" << endl;
}
Problem p03578 (C++)

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

int32_t main() {
  FastIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, mw;
  cin >> n;
  unordered_map<int, int> m;
  int tmp;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    m[tmp]++;
  }
  cin >> mw;

  for (int j = 0; j < mw; j++) {
    cin >> tmp;
    if (m[tmp] == 0) {
      cout << "NO";
      return 0;
    }
    m[tmp]--;
  }
  cout << "YES" << endl;
  return 0;
}

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

int32_t main() {
  FastIO;
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
  int n, mw;
  cin >> n;
  unordered_map<int, int> m;
  int tmp;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    m[tmp]++;
  }
  cin >> mw;

  for (int j = 0; j < mw; j++) {
    cin >> tmp;
    if (m[tmp] == 0) {
      cout << "NO";
      return 0;
    }
    m[tmp]--;
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03578 (C++)

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

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

int main() {
  int n;
  cin >> n;
  vector<LL> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  int m;
  cin >> m;
  vector<LL> t(m);
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }

  if (n < m) {
    cout << "NO" << endl;
    return 0;
  }
  sort(d.begin(), d.end());
  sort(t.begin(), t.end());

  int a, b;
  while (a < n || b <= m) {
    if (b == m) {
      cout << "YES" << endl;
      return 0;
    }

    if (d[a] == t[b]) {
      a++;
      b++;
    } else if (d[a] > t[b]) {
      cout << "NO" << endl;
      return 0;
    } else {
      a++;
    }
  }

  cout << "NO" << endl;

  return 0;
}


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

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

int main() {
  int n;
  cin >> n;
  vector<LL> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  int m;
  cin >> m;
  vector<LL> t(m);
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }

  if (n < m) {
    cout << "NO" << endl;
    return 0;
  }
  sort(d.begin(), d.end());
  sort(t.begin(), t.end());

  int a = 0, b = 0;
  while (a < n || b <= m) {
    if (b == m) {
      cout << "YES" << endl;
      return 0;
    }

    if (d[a] == t[b]) {
      a++;
      b++;
    } else if (d[a] > t[b]) {
      cout << "NO" << endl;
      return 0;
    } else {
      a++;
    }
  }

  cout << "NO" << endl;

  return 0;
}
Problem p03578 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define vector2(a, b, c)                                                       \
  vector<vector<int>> a(b /*縦！w*/, vector<int>(c /*横！w*/))
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
struct edge {
  int from, to, cost;
};
signed main() {
  int a;
  cin >> a;
  vector<int> c(a);
  rep(i, a) cin >> c[i];
  int b;
  cin >> b;
  vector<int> d(b);
  rep(i, b) cin >> d[i];
  vector<int> e(2000010, 0), f(20000010, 0);
  rep(i, a) e[c[i] - 1]++;
  rep(i, b) f[d[i] - 1]++;
  bool ans = 1;
  rep(i, b) {
    if (e[d[i] - 1] < f[d[i] - 1])
      ans = 0;
  }
  if (ans)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define vector2(a, b, c)                                                       \
  vector<vector<int>> a(b /*縦！w*/, vector<int>(c /*横！w*/))
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
struct edge {
  int from, to, cost;
};
signed main() {
  int a;
  cin >> a;
  vector<int> c(a);
  rep(i, a) cin >> c[i];
  int b;
  cin >> b;
  vector<int> d(b);
  rep(i, b) cin >> d[i];
  map<int, int> e, f;
  rep(i, a) e[c[i] - 1]++;
  rep(i, b) f[d[i] - 1]++;
  bool ans = 1;
  rep(i, b) {
    if (e[d[i] - 1] < f[d[i] - 1])
      ans = 0;
  }
  if (ans)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03578 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
int n, m;
unsigned long long ans;
int D[200001];
int x;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  bool good = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    D[x]++;
  }
  cin >> m;
  for (int i = 0; (i < m) && good; i++) {
    cin >> x;
    if (D[x] > 0) {
      D[x]--;
    } else {
      good = false;
    }
  }
  if (good) {
    cout << "YES";
  } else {
    cout << "NO";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
int n, m;
unsigned long long ans;
map<int, int> D;
int x;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  bool good = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    D[x]++;
  }
  cin >> m;
  for (int i = 0; (i < m) && good; i++) {
    cin >> x;
    if (D[x] > 0) {
      D[x]--;
    } else {
      good = false;
    }
  }
  if (good) {
    cout << "YES";
  } else {
    cout << "NO";
  }
  return 0;
}
Problem p03578 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n, d[SIZE], m, t[SIZE];
  map<int, int> md, mt;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", d + i);
    md[d[i]]++;
  }

  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    scanf("%d", t + i);
    mt[t[i]]++;
  }

  for (auto it = mt.begin(); it != mt.end(); it++) {
    int p = it->first;
    int q = it->second;

    if (md[p] < q) {
      puts("NO");
      return 0;
    }
  }

  puts("YES");

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int n, d[SIZE], m, t[SIZE];
  map<int, int> md, mt;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", d + i);
    md[d[i]]++;
  }

  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    scanf("%d", t + i);
    mt[t[i]]++;
  }

  for (auto it = mt.begin(); it != mt.end(); it++) {
    int p = it->first;
    int q = it->second;

    if (md[p] < q) {
      puts("NO");
      return 0;
    }
  }

  puts("YES");

  return 0;
}
Problem p03578 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// const int mod = int(1e9) + 7;
// const int SZ = 1010;
const int MX = 100500;

#define pii pair<int, int>

int a[MX];
int b[MX];

int main(int argc, char **argv) {
#ifdef LOCAL
  freopen("/Users/melnykroman/CLionProjects/cf/in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(b, b + m);
  int pos = 0;
  for (int i = 0; i < m; i++) {
    while (pos < n && a[pos] < b[i])
      pos++;
    if (pos == n || a[pos] != b[i]) {
      cout << "NO" << endl;
      return 0;
    }
    pos++;
  }
  cout << "YES" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// const int mod = int(1e9) + 7;
// const int SZ = 1010;
const int MX = 300500;

#define pii pair<int, int>

int a[MX];
int b[MX];

int main(int argc, char **argv) {
#ifdef LOCAL
  freopen("/Users/melnykroman/CLionProjects/cf/in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(b, b + m);
  int pos = 0;
  for (int i = 0; i < m; i++) {
    while (pos < n && a[pos] < b[i])
      pos++;
    if (pos == n || a[pos] != b[i]) {
      cout << "NO" << endl;
      return 0;
    }
    pos++;
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03578 (C++)

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

const int ms = 200200;

int freq[ms];

int main() {
  int n, m;
  std::cin >> n;
  while (n--) {
    int temp;
    std::cin >> temp;
    freq[temp]++;
  }
  std::cin >> m;
  while (m--) {
    int temp;
    std::cin >> temp;
    if (!freq[temp]) {
      std::cout << "NO\n";
      return 0;
    }
    freq[temp]--;
  }
  std::cout << "YES\n";
}

Fixed submission:
#include <iostream>
#include <map>
std::map<int, int> freq;

int main() {
  int n, m;
  std::cin >> n;
  while (n--) {
    int temp;
    std::cin >> temp;
    freq[temp]++;
  }
  std::cin >> m;
  while (m--) {
    int temp;
    std::cin >> temp;
    if (!freq[temp]) {
      std::cout << "NO\n";
      return 0;
    }
    freq[temp]--;
  }
  std::cout << "YES\n";
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
typedef pair<int, int> P;
vector<int> G[N];
set<int> g[N];
int n;
int check(int a, int b) {
  if (g[a].size() < 2)
    return 0;
  if (g[b].size() < 2)
    return 0;
  return 1;
}

int used[N];
int check2(int pos = 0, int f = 0) {
  if (used[pos] != -1)
    return f != used[pos];
  used[pos] = f;
  for (int to : G[pos])
    if (check2(to, !f))
      return 1;
}

signed main() {

  int m;
  cin >> n >> m;

  vector<int> A(n), B(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    A[i] = a, B[i] = b;
    G[a].push_back(b);
    G[b].push_back(a);
    g[a].insert(b);
    g[b].insert(a);
  }
  int flg = 0;
  for (int i = 0; i < m; i++)
    flg |= check(A[i], B[i]);

  memset(used, -1, sizeof(used));
  if (flg == 0)
    cout << 0 << endl;
  else if (check2())
    cout << n * (n - 1) / 2 - m << endl;
  else {
    int cnt[2] = {};
    for (int i = 0; i < n; i++)
      cnt[used[i]]++;
    cout << (cnt[0] * cnt[1]) - m << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
typedef pair<int, int> P;
vector<int> G[N];
set<int> g[N];
int n;
int check(int a, int b) {
  if (g[a].size() < 2)
    return 0;
  if (g[b].size() < 2)
    return 0;
  return 1;
}

int used[N];
int check2(int pos = 0, int f = 0) {
  if (used[pos] != -1)
    return f != used[pos];
  used[pos] = f;
  for (int to : G[pos])
    if (check2(to, !f))
      return 1;
}

signed main() {

  int m;
  cin >> n >> m;

  vector<int> A(m), B(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    A[i] = a, B[i] = b;
    G[a].push_back(b);
    G[b].push_back(a);
    g[a].insert(b);
    g[b].insert(a);
  }
  int flg = 0;
  for (int i = 0; i < m; i++)
    flg |= check(A[i], B[i]);

  memset(used, -1, sizeof(used));
  if (flg == 0)
    cout << 0 << endl;
  else if (check2())
    cout << n * (n - 1) / 2 - m << endl;
  else {
    int cnt[2] = {};
    for (int i = 0; i < n; i++)
      cnt[used[i]]++;
    cout << (cnt[0] * cnt[1]) - m << endl;
  }

  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n, m;
vector<int> G[maxn];
int b, w;
int color[maxn];
bool bipartite(int u, int c) {
  if (color[u] != 0 && color[u] != c)
    return false;
  color[u] = c;
  if (c == 1)
    b++;
  else
    w++;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (!bipartite(v, 3 - c))
      return false;
  }
  return true;
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  if (!bipartite(u, 1)) {
    cout << (ll)n * (n - 1) / 2 - m << "\n";
  } else
    cout << (ll)b * w - m << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n, m;
vector<int> G[maxn];
int b, w;
int color[maxn];
bool bipartite(int u, int c) {
  if (color[u] != 0 && color[u] != c)
    return false;
  if (color[u] != 0 && color[u] == c)
    return true;
  color[u] = c;
  if (c == 1)
    b++;
  else
    w++;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (!bipartite(v, 3 - c))
      return false;
  }
  return true;
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  if (!bipartite(u, 1)) {
    cout << (ll)n * (n - 1) / 2 - m << "\n";
  } else
    cout << (ll)b * w - m << "\n";
  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, int> P;

ll N, M;
typedef vector<vector<ll>> G;
G graph;
ll color[10050];

bool dfs(ll v, ll c) {
  color[v] = c; // 頂点v を cで塗る
  for (ll i = 0; i < graph[v].size(); i++) {
    // 隣接している頂点が同じ色なら false
    if (color[graph[v][i]] == c)
      return false;

    if (color[graph[v][i]] == 0 && !dfs(graph[v][i], -c))
      return false;
  }

  // 全ての頂点をぬれた
  return true;
}

int main() {
  cin >> N >> M;
  graph = G(N);
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  ll b = 0, w = 0;
  if (dfs(0, 1)) {
    rep(i, N) {
      if (color[i] == 1) {
        b++;
      } else if (color[i] == -1) {
        w++;
      }
    }
    cout << b * w - M << endl;
  } else {
    // 二部グラフではない場合
    cout << N * (N - 1) / 2 - M << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, int> P;

ll N, M;
typedef vector<vector<ll>> G;
G graph;
ll color[100050];

bool dfs(ll v, ll c) {
  color[v] = c; // 頂点v を cで塗る
  for (ll i = 0; i < graph[v].size(); i++) {
    // 隣接している頂点が同じ色なら false
    if (color[graph[v][i]] == c)
      return false;

    if (color[graph[v][i]] == 0 && !dfs(graph[v][i], -c))
      return false;
  }

  // 全ての頂点をぬれた
  return true;
}

int main() {
  cin >> N >> M;
  graph = G(N);
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  ll b = 0, w = 0;
  if (dfs(0, 1)) {
    rep(i, N) {
      if (color[i] == 1) {
        b++;
      } else if (color[i] == -1) {
        w++;
      }
    }
    cout << b * w - M << endl;
  } else {
    // 二部グラフではない場合
    cout << N * (N - 1) / 2 - M << endl;
  }
}
Problem p03579 (C++)

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)

using namespace std;

typedef long long int lli;

class UnionFind {
public:
  vector<int> parent;
  UnionFind(int n) {
    parent.resize(n);
    REP(i, 0, n) parent[i] = i;
  }
  int Find(int a) { return (a == parent[a] ? a : a = Find(parent[a])); }
  bool isSame(int a, int b) { return Find(a) == Find(b); }
  bool Union(int a, int b) {
    if (isSame(a, b))
      return false;
    int pa = Find(a);
    int pb = Find(b);
    parent[pa] = pb;
    return true;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(2 * N);
  REP(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.Union(a + N, b);
    uf.Union(a, b + N);
  }
  bool OK = true;
  REP(i, 0, N) if (uf.isSame(i, i + N)) OK = false;
  if (OK) {
    lli x = 0, y = 0;
    REP(i, 0, N) {
      if (uf.isSame(0, i))
        x++;
      else
        y++;
    }
    cout << x * y - M << endl;
  } else {
    cout << (lli)N * (N - 1) / 2 - M << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)

using namespace std;

typedef long long int lli;

class UnionFind {
public:
  vector<int> parent;
  UnionFind(int n) {
    parent.resize(n);
    REP(i, 0, n) parent[i] = i;
  }
  int Find(int a) { return (a == parent[a] ? a : parent[a] = Find(parent[a])); }
  bool isSame(int a, int b) { return Find(a) == Find(b); }
  bool Union(int a, int b) {
    if (isSame(a, b))
      return false;
    int pa = Find(a);
    int pb = Find(b);
    parent[pa] = pb;
    return true;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(2 * N);
  REP(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.Union(a + N, b);
    uf.Union(a, b + N);
  }
  bool OK = true;
  REP(i, 0, N) if (uf.isSame(i, i + N)) OK = false;
  if (OK) {
    lli x = 0, y = 0;
    REP(i, 0, N) {
      if (uf.isSame(0, i))
        x++;
      else
        y++;
    }
    cout << x * y - M << endl;
  } else {
    cout << (lli)N * (N - 1) / 2 - M << endl;
  }
  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

struct UnionFind {
  vector<int> par;  // 親
  vector<int> size; // 集合の大きさ

  UnionFind(int n) {
    par.resize(n);
    size.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      size[i] = 1;
    }
  }

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

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

    if (size[x] < size[y])
      swap(x, y);
    par[y] = x;
    size[x] += size[y];
  }
};

signed main() {

  int n, m;
  cin >> n >> m;
  UnionFind uf(n * n + n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b + n);
    uf.unite(b, a + n);
  }

  if (uf.root(0) == uf.root(n)) {
    cout << n * (n - 1) / 2 - m << endl;
  } else {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (uf.root(0) == uf.root(i))
        cnt++;
    }
    cout << cnt * (n - cnt) - m << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

struct UnionFind {
  vector<int> par;  // 親
  vector<int> size; // 集合の大きさ

  UnionFind(int n) {
    par.resize(n);
    size.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      size[i] = 1;
    }
  }

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

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

    if (size[x] < size[y])
      swap(x, y);
    par[y] = x;
    size[x] += size[y];
  }
};

signed main() {

  int n, m;
  cin >> n >> m;
  UnionFind uf(n + n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b + n);
    uf.unite(b, a + n);
  }

  if (uf.root(0) == uf.root(n)) {
    cout << n * (n - 1) / 2 - m << endl;
  } else {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (uf.root(0) == uf.root(i))
        cnt++;
    }
    cout << cnt * (n - cnt) - m << endl;
  }

  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 77;

#define gc getchar
#define inc(i, x, y) for (int i = x; i <= y; ++i)
#define dec(i, x, y) for (int i = x; i >= y; --i)

bool chkmin(int &x, int y) { return (x > y) ? x = y, 1 : 0; }
bool chkmax(int &x, int y) { return (x < y) ? x = y, 1 : 0; }

int rd() {
  int ret = 0;
  bool fl = 0;
  char ch = 0;
  while (!isdigit(ch))
    fl |= (ch == '-'), ch = gc();
  while (isdigit(ch))
    ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = gc();
  return fl ? -ret : ret;
}

struct Edge {
  int nxt, to;
} e[N << 1];

int tot, head[N];

void add(int u, int v) { e[++tot] = (Edge){head[u], v}, head[u] = tot++; }

int n, m, c[N];

void dfs(int u, int col) {
  c[u] = col;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (c[v] == -1)
      dfs(v, !col);
    if (c[v] != !col) {
      printf("%lld\n", 1LL * n * (n - 1) / 2 - m);
      exit(0);
    }
  }
}

int main() {
  n = rd(), m = rd();
  memset(c, -1, sizeof(c));
  for (int i = 0; i < m; ++i) {
    int u = rd(), v = rd();
    add(u, v);
    add(v, u);
  }
  dfs(1, 0);
  int b = 0, w = 0;
  for (int i = 1; i <= n; ++i)
    if (c[i])
      ++b;
    else
      ++w;
  printf("%lld\n", 1LL * b * w - m);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + 77;

#define gc getchar
#define inc(i, x, y) for (int i = x; i <= y; ++i)
#define dec(i, x, y) for (int i = x; i >= y; --i)

bool chkmin(int &x, int y) { return (x > y) ? x = y, 1 : 0; }
bool chkmax(int &x, int y) { return (x < y) ? x = y, 1 : 0; }

int rd() {
  int ret = 0;
  bool fl = 0;
  char ch = 0;
  while (!isdigit(ch))
    fl |= (ch == '-'), ch = gc();
  while (isdigit(ch))
    ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = gc();
  return fl ? -ret : ret;
}

struct Edge {
  int nxt, to;
} e[N << 1];

int tot, head[N];

void add(int u, int v) { e[++tot] = (Edge){head[u], v}, head[u] = tot++; }

int n, m, c[N];

void dfs(int u, int col) {
  c[u] = col;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (c[v] == -1)
      dfs(v, !col);
    if (c[v] != !col) {
      printf("%lld\n", 1LL * n * (n - 1) / 2 - m);
      exit(0);
    }
  }
}

int main() {
  n = rd(), m = rd();
  memset(c, -1, sizeof(c));
  for (int i = 0; i < m; ++i) {
    int u = rd(), v = rd();
    add(u, v);
    add(v, u);
  }
  dfs(1, 0);
  int b = 0, w = 0;
  for (int i = 1; i <= n; ++i)
    if (c[i])
      ++b;
    else
      ++w;
  printf("%lld\n", 1LL * b * w - m);
  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
const int maxx = 1e5 + 5;
int read() {
  char x = getchar();
  int u = 0;
  while (!isdigit(x))
    x = getchar();
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return u;
}
int be[maxx], ne[maxx << 1], to[maxx << 1], e = 0;
int color[maxx];
LL ans = 0, num[5], n, m;
void add(int x, int y) {
  to[++e] = y;
  ne[e] = be[x];
  be[x] = e;
}
bool pd(int x, int y) {
  if (color[x] == 1 && color[y] == 2)
    return 1;
  else if (color[x] == 2 && color[y] == 1)
    return 1;
  return 0;
}
void dfs(int id, int fa) {
  for (int i = be[id]; i; i = ne[i]) {
    int go = to[i];
    if (go == fa || pd(go, id) || color[go] == 3)
      continue;
    if (!color[go]) {
      if (color[id] == 3)
        color[go] = 3;
      else
        color[go] = (color[id] & 1) + 1;
    } else
      color[go] = 3;
    //			printf("%d %d %d %d\n",id,go,color[id],color[go]);
    dfs(go, id);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  int u, v;
  n = read();
  m = read();
  For(i, 1, m) {
    u = read();
    v = read();
    add(u, v);
    add(v, u);
  }
  color[1] = 1;
  dfs(1, 0);
  For(i, 1, n) num[color[i]]++;
  ans = num[1] * num[2] + (2ll * n - 1ll - num[3]) * num[3] / 2ll - 1LL * m;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
const int maxx = 1e5 + 5;
int read() {
  char x = getchar();
  int u = 0;
  while (!isdigit(x))
    x = getchar();
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return u;
}
int be[maxx], ne[maxx << 1], to[maxx << 1], e = 0;
int color[maxx];
LL ans = 0, num[5], n, m;
void add(int x, int y) {
  to[++e] = y;
  ne[e] = be[x];
  be[x] = e;
}
bool pd(int x, int y) {
  if (color[x] == 1 && color[y] == 2)
    return 1;
  else if (color[x] == 2 && color[y] == 1)
    return 1;
  return 0;
}
void dfs(int id, int fa) {
  for (int i = be[id]; i; i = ne[i]) {
    int go = to[i];
    if (go == fa || pd(go, id) || color[go] == 3)
      continue;
    if (!color[go]) {
      if (color[id] == 3)
        color[go] = 3;
      else
        color[go] = (color[id] & 1) + 1;
    } else
      color[go] = 3;
    //			printf("%d %d %d %d\n",id,go,color[id],color[go]);
    dfs(go, id);
  }
}
int main() {
  int u, v;
  n = read();
  m = read();
  For(i, 1, m) {
    u = read();
    v = read();
    add(u, v);
    add(v, u);
  }
  color[1] = 1;
  dfs(1, 0);
  For(i, 1, n) num[color[i]]++;
  ans = num[1] * num[2] + (2ll * n - 1ll - num[3]) * num[3] / 2ll - 1LL * m;
  printf("%lld\n", ans);
  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vector<ll>>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

// 二部グラフ判定
vector<ll> color;
bool dfs(mat G, ll v, ll cur = 0) {
  color[v] = cur;
  for (auto next_v : G[v]) {
    // 隣接頂点がすでに色確定していた場合
    if (color[next_v] != -1) {
      if (color[next_v] == cur)
        return false; // 同じ色が隣接したらダメ
      continue;
    }

    // 隣接頂点の色を変えて、再帰的に探索 (一回でも false が返ってきたら false)
    if (!dfs(G, next_v, 1 - cur))
      return false;
  }
  return true;
}

int main() {

  ll n, m;
  cin >> n >> m;

  mat G(n);
  vec a(n), b(n);
  for (ll i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }

  vec d(n, -1);
  d[0] = 0;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (ll i = 0; i < G[v].size(); i++) {
      if (d[G[v][i]] != -1)
        continue;
      d[G[v][i]] = d[v] + 1;
      q.push(G[v][i]);
    }
  }
  bool ok = true;
  for (ll i = 0; i < m; i++) {
    if ((d[a[i] - 1] - d[b[i] - 1]) % 2 == 0)
      ok = false;
  }

  color = vec(n, -1);
  if (!ok) {
    cout << n * (n - 1) / 2 - m << endl;
  } else {
    ll b = 0;
    for (ll i = 0; i < n; i++)
      b += (d[i] & 1);
    cout << b * (n - b) - m << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vector<ll>>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

// 二部グラフ判定
vector<ll> color;
bool dfs(mat G, ll v, ll cur = 0) {
  color[v] = cur;
  for (auto next_v : G[v]) {
    // 隣接頂点がすでに色確定していた場合
    if (color[next_v] != -1) {
      if (color[next_v] == cur)
        return false; // 同じ色が隣接したらダメ
      continue;
    }

    // 隣接頂点の色を変えて、再帰的に探索 (一回でも false が返ってきたら false)
    if (!dfs(G, next_v, 1 - cur))
      return false;
  }
  return true;
}

int main() {

  ll n, m;
  cin >> n >> m;

  mat G(n);
  vec a(m), b(m);
  for (ll i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }

  vec d(n, -1);
  d[0] = 0;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (ll i = 0; i < G[v].size(); i++) {
      if (d[G[v][i]] != -1)
        continue;
      d[G[v][i]] = d[v] + 1;
      q.push(G[v][i]);
    }
  }
  bool ok = true;
  for (ll i = 0; i < m; i++) {
    if ((d[a[i] - 1] - d[b[i] - 1]) % 2 == 0)
      ok = false;
  }

  color = vec(n, -1);
  if (!ok) {
    cout << n * (n - 1) / 2 - m << endl;
  } else {
    ll b = 0;
    for (ll i = 0; i < n; i++)
      b += (d[i] & 1);
    cout << b * (n - b) - m << endl;
  }
}
Problem p03579 (C++)

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

using namespace std;

long long ans, n, m, x, y, i, banyak;
vector<int> edge[100007];
long long flag[100007];

bool DFS(int pos, int nyan) {
  bool ret = 1;

  int i;
  for (i = 0; i < edge[pos].size(); i++) {
    if (flag[edge[pos][i]] == 0) {
      flag[edge[pos][i]] = 3 - nyan;
      // printf("--- %lld %lld\n",edge[pos][i],3-nyan);
      banyak++;
      ret &= DFS(edge[pos][i], 3 - nyan);
    } else if (flag[edge[pos][i]] == nyan) {
      // printf("%lld %lld %d\n",pos,edge[pos][i],nyan);
      ret = false;
    }
  }

  return ret;
}

int main() {
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld", &x, &y);
    edge[x].push_back(y);
    edge[y].push_back(x);
  }

  for (i = 1; i <= n; i++)
    if (flag[i] == 0) {
      flag[i] = 1;
      banyak = 1;
      bool bin = DFS(i, 1);
      assert(banyak == n);
      // printf("%lld %lld\n",bin,banyak);
      if (bin) {
        ans += (banyak / 2) * ((banyak + 1) / 2);
        assert(false);
      } else {
        ans += (banyak) * (banyak - 1) / 2;
      }
    }

  printf("%lld\n", ans - m);
}


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

using namespace std;

long long ans, n, m, x, y, i, banyak;
vector<int> edge[100007];
long long flag[100007];

bool DFS(int pos, int nyan) {
  bool ret = 1;

  int i;
  for (i = 0; i < edge[pos].size(); i++) {
    if (flag[edge[pos][i]] == 0) {
      flag[edge[pos][i]] = 3 - nyan;
      // printf("--- %lld %lld\n",edge[pos][i],3-nyan);
      banyak++;
      ret &= DFS(edge[pos][i], 3 - nyan);
    } else if (flag[edge[pos][i]] == nyan) {
      // printf("%lld %lld %d\n",pos,edge[pos][i],nyan);
      ret = false;
    }
  }

  return ret;
}

int main() {
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld", &x, &y);
    edge[x].push_back(y);
    edge[y].push_back(x);
  }

  for (i = 1; i <= n; i++)
    if (flag[i] == 0) {
      flag[i] = 1;
      banyak = 1;
      bool bin = DFS(i, 1);
      assert(banyak == n);
      // printf("%lld %lld\n",bin,banyak);
      if (bin) {
        long long x = 0;
        for (i = 1; i <= n; i++)
          if (flag[i] == 1)
            x++;
        ans += x * (n - x);
      } else {
        ans += (banyak) * (banyak - 1) / 2;
      }
    }

  printf("%lld\n", ans - m);
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define endl "\n"
using Edge = pair<int, int>;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

Graph G;
int color[1050];

bool dfs(int v, int c) {
  color[v] = c;
  for (auto e : G[v]) {
    if (color[e] == c)
      return false;
    if (color[e] == 0 && !dfs(e, -c))
      return false;
  }
  return true;
}

int main(void) {
  ll n, m;
  cin >> n >> m;
  G.assign(n, vector<int>());
  FOR(i, 0, m - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].PB(b);
    G[b].PB(a);
  }
  FOR(i, 0, n - 1) {
    if (color[i] == 0) {
      if (!dfs(i, 1)) {
        cout << n * (n - 1) / 2 - m << "\n";
        return 0;
      }
    }
  }
  ll color1 = 0;
  FOR(i, 0, n - 1) if (color[i] == 1)++ color1;
  cout << color1 * (n - color1) - m << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define endl "\n"
using Edge = pair<int, int>;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

Graph G;
int color[100050];

bool dfs(int v, int c) {
  color[v] = c;
  for (auto e : G[v]) {
    if (color[e] == c)
      return false;
    if (color[e] == 0 && !dfs(e, -c))
      return false;
  }
  return true;
}

int main(void) {
  ll n, m;
  cin >> n >> m;
  G.assign(n, vector<int>());
  FOR(i, 0, m - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].PB(b);
    G[b].PB(a);
  }
  FOR(i, 0, n - 1) {
    if (color[i] == 0) {
      if (!dfs(i, 1)) {
        cout << n * (n - 1) / 2 - m << "\n";
        return 0;
      }
    }
  }
  ll color1 = 0;
  FOR(i, 0, n - 1) if (color[i] == 1)++ color1;
  cout << color1 * (n - color1) - m << "\n";
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define ll long long
#define For(i, j, k) for (int i = j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = j; i >= (int)k; --i)
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 2e5 + 5;

int n, m;
int dep[maxn], tot[2];
int cnt, Begin[maxn], Next[maxn << 1], To[maxn << 1];
bool vis[maxn], flag = true;

inline void file() {
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
}

inline int Read() {
  int x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  return x;
}

inline void Add(int x, int y) {
  To[++cnt] = y;
  Next[cnt] = Begin[x];
  Begin[x] = cnt;
}

void dfs(int x, int f) {
  vis[x] = 1;
  ++tot[dep[x]];
  for (int i = Begin[x], v; i; i = Next[i]) {
    v = To[i];
    if (v == f)
      continue;
    if (vis[v] && dep[v] == dep[x]) {
      flag = false;
      continue;
    } else {
      dep[v] = dep[x] ^ 1;
      dfs(v, x);
    }
  }
}

int main() {
  int x, y;
  n = Read();
  m = Read();
  For(i, 1, m) {
    x = Read();
    y = Read();
    Add(x, y);
    Add(y, x);
  }

  dfs(1, 0);
  if (flag)
    printf("%lld", 1ll * tot[0] * tot[1] - m);
  else
    printf("%lld", 1ll * n * (n - 1) / 2 - m);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define ll long long
#define For(i, j, k) for (int i = j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = j; i >= (int)k; --i)
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 2e5 + 5;

int n, m;
int dep[maxn], tot[2];
int cnt, Begin[maxn], Next[maxn << 1], To[maxn << 1];
bool vis[maxn], flag = true;

inline void file() {
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
}

inline int Read() {
  int x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  return x;
}

inline void Add(int x, int y) {
  To[++cnt] = y;
  Next[cnt] = Begin[x];
  Begin[x] = cnt;
}

void dfs(int x, int f) {
  vis[x] = 1;
  ++tot[dep[x]];
  for (int i = Begin[x], v; i; i = Next[i]) {
    v = To[i];
    if (v == f)
      continue;
    if (vis[v]) {
      if (dep[v] == dep[x]) {
        flag = false;
        continue;
      }
    } else {
      dep[v] = dep[x] ^ 1;
      dfs(v, x);
    }
  }
}

int main() {
  int x, y;
  n = Read();
  m = Read();
  For(i, 1, m) {
    x = Read();
    y = Read();
    Add(x, y);
    Add(y, x);
  }

  dfs(1, 0);
  if (flag)
    printf("%lld", 1ll * tot[0] * tot[1] - m);
  else
    printf("%lld", 1ll * n * (n - 1) / 2 - m);
  return 0;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G;
vector<int> num;
void dfs(int s) {
  for (int e : G[s]) {
    if (num[e] == 0) {
      num[e] = -num[s];
      dfs(e);
    }
  }
}
int main() {
  long long N, M;
  cin >> N >> M;
  G.resize(N);
  vector<int> a(N), b(N);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  num.resize(N, 0);
  num[0] = 1;
  dfs(0);
  long long cnt = 0;
  for (int i = 0; i < N; i++) {
    if (num[i] == 1)
      cnt++;
  }
  long long cnteq = 0;
  int flag = 0;
  for (int i = 0; i < M; i++) {
    if (num[a[i]] != num[b[i]])
      cnteq++;
    else
      flag = 1;
  }
  if (flag == 0)
    cout << cnt * (N - cnt) - cnteq << endl;
  else
    cout << N * (N - 1) / 2 - M << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G;
vector<int> num;
void dfs(int s) {
  for (int e : G[s]) {
    if (num[e] == 0) {
      num[e] = -num[s];
      dfs(e);
    }
  }
}
int main() {
  long long N, M;
  cin >> N >> M;
  G.resize(N);
  vector<int> a(M), b(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  num.resize(N, 0);
  num[0] = 1;
  dfs(0);
  long long cnt = 0;
  for (int i = 0; i < N; i++) {
    if (num[i] == 1)
      cnt++;
  }
  long long cnteq = 0;
  int flag = 0;
  for (int i = 0; i < M; i++) {
    if (num[a[i]] != num[b[i]])
      cnteq++;
    else
      flag = 1;
  }
  if (flag == 0)
    cout << cnt * (N - cnt) - cnteq << endl;
  else
    cout << N * (N - 1) / 2 - M << endl;
}
Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
ll n, m, num;

const int maxn = 10005;
vector<int> G[maxn];

bool vis[maxn];
int f[maxn];

int bfs(int i) {
  queue<int> que;
  que.push(i);
  vis[i] = true;
  f[i] = 0;
  while (!que.empty()) {
    int e = que.front();
    que.pop();
    int d = G[e].size();
    for (int i = 0; i < d; i++) {
      int t = G[e][i];
      if (vis[t]) {
        if (f[t] == f[e])
          return 1;
      } else {
        vis[t] = true;
        f[t] = f[e] == 0 ? 1 : 0;
        que.push(t);
      }
    }
  }
  return 0;
}

int fun() {
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      if (bfs(i) == 1)
        return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%lld %lld", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  memset(vis, false, sizeof(vis));
  if (fun() == 1) {
    printf("%lld", n * (n - 1) / 2 - m);
  } else {
    for (int i = 1; i <= n; i++)
      if (f[i])
        num++;
    printf("%lld", num * (n - num) - m);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
ll n, m, num;

const int maxn = 1e5 + 5;
vector<int> G[maxn];

bool vis[maxn];
int f[maxn];

int bfs(int i) {
  queue<int> que;
  que.push(i);
  vis[i] = true;
  f[i] = 0;
  while (!que.empty()) {
    int e = que.front();
    que.pop();
    int d = G[e].size();
    for (int i = 0; i < d; i++) {
      int t = G[e][i];
      if (vis[t]) {
        if (f[t] == f[e])
          return 1;
      } else {
        vis[t] = true;
        f[t] = f[e] == 0 ? 1 : 0;
        que.push(t);
      }
    }
  }
  return 0;
}

int fun() {
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      if (bfs(i) == 1)
        return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%lld %lld", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  memset(vis, false, sizeof(vis));
  if (fun() == 1) {
    printf("%lld", n * (n - 1) / 2 - m);
  } else {
    for (int i = 1; i <= n; i++)
      if (f[i])
        num++;
    printf("%lld", num * (n - num) - m);
  }
  return 0;
}