# Hanuman and Tree (Programming Contest #1 (Chapter 'Ramayan') )

https://www.codechef.com/PCR12020/problems/HTREE
It is clarified in announcement that node 1 is always the root of tree.

``````#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include<numeric>
using namespace std;

#define ll long long
#define endl "\n"
#define pb push_back
#define mp make_pair

map<ll,ll> tree;

void dfs(vector<ll> t[],ll node,vector<bool> visited)
{
visited[node]=true;

for(ll x:t[node])
{
if(!visited[x])
{
tree[x+1]=node+1;
dfs(t,x,visited);
}
}
}

int solve()
{
ll n,q,a,b;
cin>>n>>q;

vector<ll> t[n];

for(ll i=0;i<n-1;i++)
{
cin>>a>>b;
t[a-1].pb(b-1);
t[b-1].pb(a-1);
}

vector<bool> visited(n,false);

dfs(t,0,visited);

for(ll i=0;i<q;i++)
{
cin>>a>>b;

ll ans=0;

while(b--)
{
ans=tree[a];

if(ans==0)
break;

a=tree[a];
}

if(ans==0)
cout<<-1<<endl;
else
cout<<ans<<endl;
}

return 0;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

/*ll t;
cin>>t;
while(t--)*/
solve();

return 0;
}
``````

I am not understanding where I went wrong.

Your code takes O(b) time to process the queries, which is way too slow.

@everule1 Yes I got it. Thankyou.