Problem p02573 (C++)

Buggy submission (status: Runtime Error):
// C++ implementation of the above approach
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxx = 100005;
vector<int> graph[maxx];

// Function to perform the DFS calculating the
// count of the elements in a connected component
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;

  // Number of nodes in this component
  ++cnt;

  // Stores the nodes which belong
  // to current component
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {

    // If the child is not visited
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

// Function to return the desired
// count for every node in the graph
void MaximumVisit(int n, int k) {
  // To keep track of nodes we visit
  int visited[maxx];

  // Mark every node unvisited
  memset(visited, 0, sizeof visited);
  int ans[maxx];

  // No of connected elements for each node
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;

    // If a node is not visited, perform a DFS as
    // this node belongs to a different component
    // which is not yet visited
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }

    // Now store the count of all the visited
    // nodes for any particular component.
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }

  // Print the result

  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (res < ans[i])
      res = ans[i];
  }
  cout << res;
  return;
}

// Function to build the graph
void MakeGraph(int n, int k) {
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
}

// Driver code
int32_t main() {
  int n, k;
  cin >> n >> k;

  // Build the graph
  MakeGraph(n, k);

  MaximumVisit(n, k);
  return 0;
}


Fixed submission:
// C++ implementation of the above approach
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxx = 200005;
vector<int> graph[maxx];

// Function to perform the DFS calculating the
// count of the elements in a connected component
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;

  // Number of nodes in this component
  ++cnt;

  // Stores the nodes which belong
  // to current component
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {

    // If the child is not visited
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

// Function to return the desired
// count for every node in the graph
void MaximumVisit(int n, int k) {
  // To keep track of nodes we visit
  int visited[maxx];

  // Mark every node unvisited
  memset(visited, 0, sizeof visited);
  int ans[maxx];

  // No of connected elements for each node
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;

    // If a node is not visited, perform a DFS as
    // this node belongs to a different component
    // which is not yet visited
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }

    // Now store the count of all the visited
    // nodes for any particular component.
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }

  // Print the result

  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (res < ans[i])
      res = ans[i];
  }
  cout << res;
  return;
}

// Function to build the graph
void MakeGraph(int n, int k) {
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
}

// Driver code
int32_t main() {
  int n, k;
  cin >> n >> k;

  // Build the graph
  MakeGraph(n, k);

  MaximumVisit(n, k);
  return 0;
}