#include <bits/stdc++.h>
using namespace std;
vector <int> adj(10001);
bool vis[10001];
void initialize ()
{
for (int i = 0; i<10001; i++)
vis[i] = false;
}
pair <int,int> dfs ( int x )
{
stack < pair<int,int> > 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>maxlevel)
{
maxnode = t;
maxlevel = level;
}
s.pop();
vis[t] = true;
for ( int i = 0; i<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>>n;
if (n==1 || n==2)
{
cout<<n-1;
exit(0);
}
initialize();
for (int i = 0; i<n-1; i++)
{
int a, b;
cin>>a>>b;
if (i==0)
x = a;
adj[a].push_back(b);
adj[b].push_back(a);
}
pair <int,int> far1 = dfs (x);
initialize();
pair <int,int> far2 = dfs (far1.first);
cout<<far2.second;
return 0;
}
Error related to vector, stack, pair:
prog.cpp: In function 'std::pair dfs(int)': prog.cpp:32:29: error: request for member 'size' in 'adj.std::vector<_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)
^ prog.cpp: In function 'int main()': prog.cpp:66:10: error: request for member 'push_back' in 'adj.std::vector<_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);
^ prog.cpp:67:10: error: request for member 'push_back' in 'adj.std::vector<_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);
^
Please help me rectify these.