<spoiler summary="Using vector of vector, compiled successfully and giving correct output">↵
int n;↵
cin>>n;↵
↵
vector<vector<int>> g(n);↵
vector<int> deg(n);↵
for(int i=0, u, v; i<n-1; i++){↵
cin>>u>>v;↵
--u, --v;↵
g[u].pb(v);↵
g[v].pb(u);↵
++deg[u], ++deg[v];↵
}↵
↵
int ans = 1;↵
auto dfs = [&](auto self, int v, int p = -1) -> int {↵
int mx1 = 0, mx2 = 0;↵
for(auto &c : g[v]){↵
if(c == p) continue;↵
int res = self(self,c,v);↵
if(res > mx1) mx2 = mx1, mx1 = res;↵
else mx2 = max(mx2, res);↵
}↵
ans = max(ans, mx1+mx2-(mx1>0)-(mx2>0)+deg[v]);↵
return (mx1-(mx1>0)+deg[v]-1);↵
};↵
↵
dfs(dfs,0);↵
cout<<ans<<nl;↵
</spoiler>↵
↵
↵
<spoiler summary="But when trying the same with array of vectors, it doesn't compile">↵
int n;↵
cin>>n;↵
↵
vector<int> g[n];↵
vector<int> deg(n);↵
for(int i=0, u, v; i<n-1; i++){↵
cin>>u>>v;↵
--u, --v;↵
g[u].pb(v);↵
g[v].pb(u);↵
++deg[u], ++deg[v];↵
}↵
↵
int ans = 1;↵
auto dfs = [&](auto self, int v, int p = -1) -> int {↵
int mx1 = 0, mx2 = 0;↵
for(auto &c : g[v]){↵
if(c == p) continue;↵
int res = self(self,c,v);↵
if(res > mx1) mx2 = mx1, mx1 = res;↵
else mx2 = max(mx2, res);↵
}↵
ans = max(ans, mx1+mx2-(mx1>0)-(mx2>0)+deg[v]);↵
return (mx1-(mx1>0)+deg[v]-1);↵
};↵
↵
dfs(dfs,0);↵
cout<<ans<<nl;↵
</spoiler>↵
↵
`I want to know why this is happening. Can someone please tell me the reason behind this unknown behavior of lambda function ?`↵