Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

// C++ program to print connected components in
// an undirected graph
#include <iostream>
#include <list>
using namespace std;

// Graph class represents a undirected graph
// using adjacency list representation
int ans;
class Graph {
  int V; // No. of vertices

  // Pointer to an array containing adjacency lists
  list<int> *adj;

  // A function used by DFS
  void DFSUtil(int v, bool visited[]);

public:
  Graph(int V); // Constructor
  ~Graph();
  void addEdge(int v, int w);
  void connectedComponents();
};

// Method to print connected components in an
// undirected graph
void Graph::connectedComponents() {
  // Mark all the vertices as not visited
  bool *visited = new bool[V];
  for (int v = 0; v < V; v++)
    visited[v] = false;

  for (int v = 0; v < V; v++) {
    if (visited[v] == false) {
      // print all reachable vertices
      // from v
      DFSUtil(v, visited);

      // cout << "\\n";
      ans++;
    }
  }
  delete[] visited;
}

void Graph::DFSUtil(int v, bool visited[]) {
  // Mark the current node as visited and print it
  visited[v] = true;
  // cout << v << " ";

  // Recur for all the vertices
  // adjacent to this vertex
  list<int>::iterator i;
  for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
      DFSUtil(*i, visited);
}

Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}

Graph::~Graph() { delete[] adj; }

// method to add an undirected edge
void Graph::addEdge(int v, int w) {
  adj[v].push_back(w);
  adj[w].push_back(v);
}

// Driver program to test above
int main() {
  // Create a graph given in the above diagram
  // Graph g(5); // 5 vertices numbered from 0 to 4
  // g.addEdge(1, 0);
  // g.addEdge(2, 3);
  // g.addEdge(3, 4);

  // cout << "Following are connected components \\n";
  // g.connectedComponents();

  int n, m;
  cin >> n >> m;
  Graph g(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g.addEdge(u - 1, v - 1);
  }
  ans = 0;
  g.connectedComponents();
  cout << ans - 1;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mod 1e9
#define vll vector<ll>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define repe(i, a) for (auto &i : a)
#define pb push_back
using namespace std;
void initialize(vector<int> &a, int n) {
  for (int i = 0; i < n; i++)
    a[i] = i;
}
int root(vector<int> Arr, int i) {
  while (Arr[i] != i) {
    i = Arr[i];
  }
  return i;
}
void g(vector<int> &Arr, int A, int B) {
  int root_A = root(Arr, A);
  int root_B = root(Arr, B);
  Arr[root_A] = root_B;
}
bool f(vector<int> a, int A, int B) {
  if (root(a, A) == root(a, B))
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll n, m;
  cin >> n >> m;
  vector<int> a(n);
  initialize(a, n);
  // for (int i = 0; i < n; i++)
  //   cout << a[i] << endl;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g(a, u - 1, v - 1);
  }
  set<int> st;
  for (int i = 0; i < n; i++) {
    st.insert(root(a, i));
  }
  // for(int i=0;i<n;i++)cout<<a[i]<<endl;
  cout << st.size() - 1;

  return 0;
}