title: Vector Stack Pair | Longest path in a tree using dfs
body: <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  
  vector &lt;int&gt; adj(10001);
  bool vis[10001];
  
  void initialize ()
  {
      for (int i = 0; i&lt;10001; i++)
          vis[i] = false;
  }
  
  pair &lt;int,int&gt; dfs ( int x )
  {
      stack &lt; pair&lt;int,int&gt; &gt; s;
      s.push( make_pair (x,0) );
      int maxnode = x, maxlevel = 0;
  
      while (!s.empty())
      {
          int t = s.top().first;
          int level = s.top().second;
          if (level&gt;maxlevel)
          {
              maxnode = t;
              maxlevel = level;
          }
  
          s.pop();
          vis[t] = true;
  
          for ( int i = 0; i&lt;adj[t].size(); i++)
          {
              if ( vis[adj[t][i]]==false)
              {
                  s.push( make_pair (adj[t][i], level+1 ));
              } 
          }
  
      }
  
      return make_pair (maxnode,maxlevel);
  }
  
  int main() {
      // your code goes here
      int n, x;
      cin&gt;&gt;n;
  
      if (n==1 || n==2)
      {
          cout&lt;&lt;n-1;
          exit(0);
      }
  
      initialize();
  
      for (int i = 0; i&lt;n-1; i++)
      {
          int a, b;
          cin&gt;&gt;a&gt;&gt;b;
  
          if (i==0)
              x = a;
  
          adj[a].push_back(b);
          adj[b].push_back(a);
      }
  
      pair &lt;int,int&gt; far1 = dfs (x);
      initialize();
      pair &lt;int,int&gt; far2 = dfs (far1.first);
  
      cout&lt;&lt;far2.second;
  
  
      return 0;
  }
  </code></pre>
  
  <p><a href="https://ide.geeksforgeeks.org/bsuLBaB2yQ" rel="nofollow noreferrer">Or the code is here.</a></p>
  
  <p>Error related to vector, stack, pair:</p>
  
  <blockquote>
    <p>prog.cpp: In function 'std::pair dfs(int)': prog.cpp:32:29:
    error: request for member 'size' in 'adj.std::vector&lt;_Tp,
    _Alloc>::operator[] >(((std::vector::size_type)t))', which is of non-class type '__gnu_cxx::__alloc_traits >::value_type {aka
    int}'    for ( int i = 0; i
                                 ^ prog.cpp:34:21: error: invalid types '__gnu_cxx::__alloc_traits >::value_type {aka
    int}[int]' for array subscript
        if ( vis[adj[t][i]]==false)<br>
                         ^ prog.cpp: In function 'int main()': prog.cpp:66:10: error: request for member 'push_back' in
    'adj.std::vector&lt;_Tp, _Alloc>::operator[]>(((std::vector::size_type)a))', which is of non-class type '__gnu_cxx::__alloc_traits >::value_type {aka
    int}'    adj[a].push_back(b);<br>
              ^ prog.cpp:67:10: error: request for member 'push_back' in 'adj.std::vector&lt;_Tp, _Alloc>::operator[]>(((std::vector::size_type)b))', which is of non-class type '__gnu_cxx::__alloc_traits >::value_type {aka
    int}'    adj[b].push_back(a);<br>
              ^</p>
  </blockquote>
  
  <p>Please help me rectify these.</p>