Problem p03787 (C++)

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

using std::swap;
typedef long long LL;

const int N = 1e5 + 10;

struct Edge {
  int to, next;
} way[N << 1];
int n, m, num[N], tot, son[N], f[N][20], dep[N];
LL p, q, cnt;
bool vis[N];

void Build(int a, int b) {
  way[++tot] = (Edge){b, num[a]};
  num[a] = tot;
}

void Init() {
  int u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    Build(u, v);
    Build(v, u);
  }
}

int LCA(int x, int y) {
  if (dep[x] > dep[y])
    swap(x, y);
  for (int i = 19; i >= 0; --i)
    if (dep[x] <= dep[f[y][i]])
      y = f[y][i];
  for (int i = 19; i >= 0; --i)
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  if (x != y)
    x = f[x][0], y = f[y][0];
  return x;
}

bool Dfs(int x, int fa) {
  vis[x] = 1;
  f[x][0] = fa;
  son[x] = 1;
  dep[x] = dep[fa] + 1;
  for (int i = 1; i < 20; ++i)
    f[x][i] = f[f[x][i - 1]][i - 1];
  bool ret = 0;
  for (int i = num[x]; i; i = way[i].next) {
    int v = way[i].to;
    if (v == fa)
      continue;
    if (!vis[v])
      ret |= Dfs(v, x), son[x] += son[v];
    else {
      int lca = LCA(v, x), len = dep[x] + dep[v] - (dep[lca] << 1) + 1;
      if (len & 1)
        ret |= 1;
    }
  }
  return ret;
}

void Solve() {
  p = q = cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      bool fl = Dfs(i, 0);
      if (son[i] == 1)
        cnt++;
      else if (!fl)
        q++;
      else
        p++;
    }
  printf("%lld\n", 1ll * n * n - 1ll * (n - cnt) * (n - cnt) + q * q * 2 +
                       p * p + p * q * 2);
}

int main() {
  Init();
  Solve();
  return 0;
}

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

using std::swap;
typedef long long LL;

const int N = 1e5 + 10;

struct Edge {
  int to, next;
} way[N << 2];
int n, m, num[N], tot, son[N], f[N][20], dep[N];
LL p, q, cnt;
bool vis[N];

void Build(int a, int b) {
  way[++tot] = (Edge){b, num[a]};
  num[a] = tot;
}

void Init() {
  int u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    Build(u, v);
    Build(v, u);
  }
}

int LCA(int x, int y) {
  if (dep[x] > dep[y])
    swap(x, y);
  for (int i = 19; i >= 0; --i)
    if (dep[x] <= dep[f[y][i]])
      y = f[y][i];
  for (int i = 19; i >= 0; --i)
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  if (x != y)
    x = f[x][0], y = f[y][0];
  return x;
}

bool Dfs(int x, int fa) {
  vis[x] = 1;
  f[x][0] = fa;
  son[x] = 1;
  dep[x] = dep[fa] + 1;
  for (int i = 1; i < 20; ++i)
    f[x][i] = f[f[x][i - 1]][i - 1];
  bool ret = 0;
  for (int i = num[x]; i; i = way[i].next) {
    int v = way[i].to;
    if (v == fa)
      continue;
    if (!vis[v])
      ret |= Dfs(v, x), son[x] += son[v];
    else {
      int lca = LCA(v, x), len = dep[x] + dep[v] - (dep[lca] << 1) + 1;
      if (len & 1)
        ret |= 1;
    }
  }
  return ret;
}

void Solve() {
  p = q = cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      bool fl = Dfs(i, 0);
      if (son[i] == 1)
        cnt++;
      else if (!fl)
        q++;
      else
        p++;
    }
  printf("%lld\n", 1ll * n * n - 1ll * (n - cnt) * (n - cnt) + q * q * 2 +
                       p * p + p * q * 2);
}

int main() {
  Init();
  Solve();
  return 0;
}