Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  int 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 ^ 48), ch = getchar();
  x *= f;
  return;
}
template <typename T> void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
const int MAXN = 100010;
int n, m, k;
struct node {
  int tot = 1;
  int edge[MAXN * 2], nxt[MAXN * 2], hd[MAXN];
  inline void add_edge(int u, int v) {
    edge[tot] = v, nxt[tot] = hd[u], hd[u] = tot++;
  }
} g1, g2;
int f[MAXN];
int find(int x) {
  if (f[x] == x)
    return f[x];
  return find(f[x]);
}
inline void Union(int u, int v) {
  u = find(u), v = find(v);
  if (u != v)
    f[u] = v;
}
bool book[MAXN];
vector<int> d[MAXN];
int sum[MAXN];
int main() {
  read(n), read(m), read(k);
  for (register int i = 1; i <= n; i++)
    f[i] = i;
  for (register int i = 1; i <= m; i++) {
    int u, v;
    read(u), read(v);
    g1.add_edge(u, v), g1.add_edge(v, u);
    Union(u, v);
  }
  for (register int i = 1; i <= k; i++) {
    int u, v;
    read(u), read(v);
    g2.add_edge(u, v), g2.add_edge(v, u);
  }
  for (register int i = 1; i <= n; i++)
    d[find(i)].push_back(i);
  for (register int i = 1; i <= n; i++) {
    int tot = 0;
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
      book[*it] = 1, tot++;
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++) {
      int x = *it;
      int cnt = 0;
      for (int j = g1.hd[x]; j; j = g1.nxt[j])
        if (book[g1.edge[j]])
          cnt++;
      for (int j = g2.hd[x]; j; j = g2.nxt[j])
        if (book[g2.edge[j]])
          cnt++;
      sum[x] = tot - cnt - 1;
    }
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
      book[*it] = 0;
  }
  for (register int i = 1; i <= n; i++)
    write(sum[i]), putchar(' ');
  putchar('\n');
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  int 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 ^ 48), ch = getchar();
  x *= f;
  return;
}
template <typename T> void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
const int MAXN = 100010;
int n, m, k;
struct node {
  int tot = 1;
  int edge[MAXN * 2], nxt[MAXN * 2], hd[MAXN];
  inline void add_edge(int u, int v) {
    edge[tot] = v, nxt[tot] = hd[u], hd[u] = tot++;
  }
} g1, g2;
int f[MAXN];
int find(int x) {
  if (f[x] == x)
    return f[x];
  return f[x] = find(f[x]);
}
inline void Union(int u, int v) {
  u = find(u), v = find(v);
  if (u != v)
    f[u] = v;
}
bool book[MAXN];
vector<int> d[MAXN];
int sum[MAXN];
int main() {
  read(n), read(m), read(k);
  for (register int i = 1; i <= n; i++)
    f[i] = i;
  for (register int i = 1; i <= m; i++) {
    int u, v;
    read(u), read(v);
    g1.add_edge(u, v), g1.add_edge(v, u);
    Union(u, v);
  }
  for (register int i = 1; i <= k; i++) {
    int u, v;
    read(u), read(v);
    g2.add_edge(u, v), g2.add_edge(v, u);
  }
  for (register int i = 1; i <= n; i++)
    d[find(i)].push_back(i);
  for (register int i = 1; i <= n; i++) {
    int tot = 0;
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
      book[*it] = 1, tot++;
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++) {
      int x = *it;
      int cnt = 0;
      for (int j = g1.hd[x]; j; j = g1.nxt[j])
        if (book[g1.edge[j]])
          cnt++;
      for (int j = g2.hd[x]; j; j = g2.nxt[j])
        if (book[g2.edge[j]])
          cnt++;
      sum[x] = tot - cnt - 1;
    }
    for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
      book[*it] = 0;
  }
  for (register int i = 1; i <= n; i++)
    write(sum[i]), putchar(' ');
  putchar('\n');
  return 0;
}