// to find the distance of each node from root(single source sorthest path (on tress)) vector g[100001]; vector visi(100001, 0); vector distance(100001); void dfs(ll v,ll dist){ visi[v] = 1; distance[v] = dist; for(ll child: g[v]) { if(visi[child]==0) dfs(child,dist+1); } } //the final dist array will give the shortest distance from 'v' node.