Problem p03579 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const int INF = 1 << 29;

int N, M;

vector<int> graph[101010];

set<pair<int, int>> edges;

int main() {
  cin >> N >> M;
  for (int j = 0; j < M; ++j) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
    graph[b].push_back(a);

    if (a > b)
      swap(a, b);
    edges.emplace(a, b);
  }

  int dist[101010];
  fill(dist, dist + N, INF);
  dist[0] = 0;

  queue<pair<int, int>> q;
  q.emplace(0, 0);
  int v, d;
  while (!q.empty()) {
    tie(v, d) = q.front();
    q.pop();

    for (const int w : graph[v]) {
      if (dist[w] == INF) {
        dist[w] = d + 1;
        q.emplace(w, d + 1);
      }
    }
  }

  bool f = false;
  for (pair<int, int> e : edges) {
    if (dist[e.first] == dist[e.second]) {
      f = true;
      break;
    }
  }

  if (f) {
    assert(false);
    cout << (N * (N - 1) / 2 - M) << endl;
    return 0;
  }

  int re[101010], ss[101010];
  fill(re, re + 101010, 0);
  for (int j = 0; j < N; ++j) {
    ++re[dist[j]];
  }

  for (int j = N; j >= 0; --j) {
    ss[j] = re[j] + ss[j + 2];
  }

  i64 ans = -M;
  for (int j = 0; j < N; ++j) {
    ans += re[j] * (re[j + 1] + ss[j + 3]);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const int INF = 1 << 29;

int N, M;

vector<int> graph[101010];

set<pair<int, int>> edges;

int main() {
  cin >> N >> M;
  for (int j = 0; j < M; ++j) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
    graph[b].push_back(a);

    if (a > b)
      swap(a, b);
    edges.emplace(a, b);
  }

  int dist[101010];
  fill(dist, dist + N, INF);
  dist[0] = 0;

  queue<pair<int, int>> q;
  q.emplace(0, 0);
  int v, d;
  while (!q.empty()) {
    tie(v, d) = q.front();
    q.pop();

    for (const int w : graph[v]) {
      if (dist[w] == INF) {
        dist[w] = d + 1;
        q.emplace(w, d + 1);
      }
    }
  }

  bool f = false;
  for (pair<int, int> e : edges) {
    if (dist[e.first] == dist[e.second]) {
      f = true;
      break;
    }
  }

  if (f) {
    cout << ((i64)N * (N - 1) / 2 - M) << endl;
    return 0;
  }

  int re[101010], ss[101010];
  fill(re, re + 101010, 0);
  for (int j = 0; j < N; ++j) {
    ++re[dist[j]];
  }

  for (int j = N; j >= 0; --j) {
    ss[j] = re[j] + ss[j + 2];
  }

  i64 ans = -M;
  for (int j = 0; j < N; ++j) {
    ans += re[j] * (re[j + 1] + ss[j + 3]);
  }
  cout << ans << endl;

  return 0;
}