const int MAX = 10000;
char visited[MAX];
vector<int> adj[MAX];
void dfs(int i) {
visited[i] = 1;
for (size_t j = 0 ; j < adj[i].size(); j++)
if (! visited[ adj[i][j] ] )
dfs( adj[i][j] );
}
int numConnectedComponents(int N) { // graph with N vertices
int i, comps = 0;
for (i = 0 ; i < N ; i++)
visited[i] = 0;
for (i = 0 ; i < N; i++)
if (!visited[i]) {
comps++;
dfs(i);
}
return comps;
}