Problem p03579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
typedef long long LL;
using namespace std;
const int N = 100010;
const int M = 200010;
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int n, m, col[N];
LL sum[4];
int bgn[N], nxt[M], to[M], E;
inline void add_edge(int u, int v) { nxt[++E] = bgn[u], bgn[u] = E, to[E] = v; }
inline bool bfs(int u) {
  queue<int> q;
  q.push(u);
  col[u] = 2;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v, i = bgn[u]; i; i = nxt[i]) {
      if (col[v = to[i]] && col[to[i]] == col[u])
        return 0;
      col[v] = col[u] ^ 1;
      q.push(v);
    }
  }
  For(i, 1, n) sum[col[i]]++;
  return 1;
}
int main() {
  int x, y;
  read(n), read(m);
  For(i, 1, m) {
    read(x), read(y);
    add_edge(x, y), add_edge(y, x);
  }
  if (bfs(1))
    printf("%lld\n", sum[2] * sum[3] - (LL)m);
  else
    printf("%lld\n", (LL)n * ((LL)n - 1ll) / 2ll - (LL)m);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
typedef long long LL;
using namespace std;
const int N = 100010;
const int M = 200010;
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int n, m, col[N];
LL sum[4];
int bgn[N], nxt[M], to[M], E;
inline void add_edge(int u, int v) { nxt[++E] = bgn[u], bgn[u] = E, to[E] = v; }
inline bool bfs(int u) {
  queue<int> q;
  q.push(u);
  col[u] = 2;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v, i = bgn[u]; i; i = nxt[i]) {
      if (col[v = to[i]] && col[to[i]] == col[u])
        return 0;
      if (!col[v]) {
        col[v] = col[u] ^ 1;
        q.push(v);
      }
    }
  }
  For(i, 1, n) sum[col[i]]++;
  return 1;
}
int main() {
  int x, y;
  read(n), read(m);
  For(i, 1, m) {
    read(x), read(y);
    add_edge(x, y), add_edge(y, x);
  }
  if (bfs(1))
    printf("%lld\n", sum[2] * sum[3] - (LL)m);
  else
    printf("%lld\n", (LL)n * ((LL)n - 1ll) / 2ll - (LL)m);
  return 0;
}