Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>
using namespace std;

#define NMAX 100005

vector<int> graph1[NMAX], graph2[NMAX];
int n, k, l, num_groups;
int f[NMAX], viz[NMAX], viz2[NMAX], answer[NMAX];
int pula[NMAX], nrp;

inline void dfs1(int node) {
  if (viz[node])
    return;
  viz[node] = num_groups;
  int lim = graph1[node].size();
  for (int i = 0; i < lim; i++)
    dfs1(graph1[node][i]);
}

inline void dfs2(int node) {
  if (viz2[node])
    return;
  viz2[node] = 1;
  f[viz[node]]++;
  pula[++nrp] = node;
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs2(graph2[node][i]);
}

inline void dfs3(int node) {
  if (viz2[node] == 2)
    return;
  viz2[node] = 2;
  answer[node] = f[viz[node]];
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs3(graph2[node][i]);
}

inline void dfs_clear(int node) {
  if (!viz2[node])
    return;
  f[viz[node]]--;
  viz2[node] = 0;
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs_clear(graph2[node][i]);
}

int main() {
  int a, b;

  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &a, &b);
    graph1[a].push_back(b);
    graph1[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!viz[i]) {
      ++num_groups;
      dfs1(i);
    }
  // for(int i = 1; i <= n; i++)
  //    printf("%d ", viz[i]);
  // printf("\n");

  for (int i = 1; i <= l; i++) {
    scanf("%d%d", &a, &b);
    graph2[a].push_back(b);
    graph2[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!viz2[i]) {
      nrp = 0;
      dfs2(i);
      for (int j = 1; j <= nrp; j++) {
        answer[pula[j]] = f[viz[pula[j]]];
      }
      for (int j = 1; j <= nrp; j++)
        f[viz[pula[j]]] = 0;
    }

  for (int i = 1; i <= n; i++)
    printf("%d ", answer[i]);
  printf("\n");

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <vector>
using namespace std;

#define NMAX 200005

vector<int> graph1[NMAX], graph2[NMAX];
int n, k, l, num_groups;
int f[NMAX], viz[NMAX], viz2[NMAX], answer[NMAX];
int pula[NMAX], nrp;

inline void dfs1(int node) {
  if (viz[node])
    return;
  viz[node] = num_groups;
  int lim = graph1[node].size();
  for (int i = 0; i < lim; i++)
    dfs1(graph1[node][i]);
}

inline void dfs2(int node) {
  if (viz2[node])
    return;
  viz2[node] = 1;
  f[viz[node]]++;
  pula[++nrp] = node;
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs2(graph2[node][i]);
}

inline void dfs3(int node) {
  if (viz2[node] == 2)
    return;
  viz2[node] = 2;
  answer[node] = f[viz[node]];
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs3(graph2[node][i]);
}

inline void dfs_clear(int node) {
  if (!viz2[node])
    return;
  f[viz[node]]--;
  viz2[node] = 0;
  int lim = graph2[node].size();
  for (int i = 0; i < lim; i++)
    dfs_clear(graph2[node][i]);
}

int main() {
  int a, b;

  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &a, &b);
    graph1[a].push_back(b);
    graph1[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!viz[i]) {
      ++num_groups;
      dfs1(i);
    }
  // for(int i = 1; i <= n; i++)
  //    printf("%d ", viz[i]);
  // printf("\n");

  for (int i = 1; i <= l; i++) {
    scanf("%d%d", &a, &b);
    graph2[a].push_back(b);
    graph2[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!viz2[i]) {
      nrp = 0;
      dfs2(i);
      for (int j = 1; j <= nrp; j++) {
        answer[pula[j]] = f[viz[pula[j]]];
      }
      for (int j = 1; j <= nrp; j++)
        f[viz[pula[j]]] = 0;
    }

  for (int i = 1; i <= n; i++)
    printf("%d ", answer[i]);
  printf("\n");

  return 0;
}