this a dfs funciton code snippet:-
void dfs(int u, int p)
{
if(p!=-1)d[u] = d[p]+1;
for(int i: v[u])
{
if(i==p)continue;
dfs(i, u);
}
}
i am not understanding this dfs implementation which came in the editorial of of a contest. the complete code is as follows.it would be really nice if someone could help me understand this piece of code
#include <bits/stdc++.h>
using namespace std;
#define int long long int
vector<int> d;
vector< vector<int> > v;
void dfs(int u, int p)
{
if(p!=-1)d[u] = d[p]+1;
for(int i: v[u])
{
if(i==p)continue;
dfs(i, u);
}
}
#undef int
int main()
{
#define int long long int
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
v.resize(n);
d.resize(n);
for(int i = 0;i<n-1;i++)
{
int x, y;
cin>>x>>y;
v[x-1].push_back(y-1);
v[y-1].push_back(x-1);
}
d[0] = 0;
dfs(0, -1);
int q;
cin>>q;
while(q--)
{
int x, y;
cin>>x>>y;
if((d[x-1]+d[y-1])&1)cout<<"Odd"<<endl;
else cout<<"Even"<<endl;
}
return 0;
}