## I need help on this problem.https://www.spoj.com/problems/RTREE/ Implementation of my approach is given below.I'm not able to figure out why i am getting the wrong answer.
Your title here...
include "iostream"
include "algorithm"
include "queue"
include "string.h"
using namespace std; typedef long long int ll; vector vg[2000005]; bool used[2000005]; ll dp[2000005];
ll myFunction(int root , int parent){ used[root] = true; ll max1 = 0, max2 = 0; for( int i= 0; i < vg[root].size(); i++) { if(parent == vg[root][i]) continue; ll x = myFunction(vg[root][i],root) + 1; if(x >= max1 ) { max2 = max1; max1 = x; } else max2 = max(x,max2); } dp[root] = max1 + max2; return max1; }
int main(){ ios::sync_with_stdio(0); int n ,m,u,v,query,root; cin >> n ; for(int i=1 ; i <= n-1 ; i++) { cin >> u >> v ; vg[v].push_back(u); vg[u].push_back(v); } memset(used, false,sizeof(used)); memset(dp,0,sizeof(dp)); cin >> root >> query; myFunction(root,-1); while(query --) { cin >> u; cout << dp[u] <<"\n"; } return 0; }