-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHC1.cpp
More file actions
65 lines (64 loc) · 1.22 KB
/
Copy pathHC1.cpp
File metadata and controls
65 lines (64 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
vector<int>arr[100000];
int visited[100000];
int depth[100000];
int precompute(int i)
{
//cout<<endl<<"Hello->>"<<i;
int links = arr[i].size();
vector<int>tempArr;
//memset(tempArr,0,sizeof(tempArr));
for(int j=0;j<arr[i].size();j++)
{
if(!visited[arr[i][j]])
{
visited[arr[i][j]]=1;
tempArr.push_back(1+precompute(arr[i][j]));
}
}
sort(tempArr.begin(),tempArr.end());
if(tempArr.size()==0)
{depth[i]= 0;
}
if(tempArr.size()==1)
depth[i]=tempArr[0]-1;
if(tempArr.size()>=2)
depth[i]=tempArr[tempArr.size()-1]+tempArr[tempArr.size()-2];
//cout<<"returnde"<<depth[i];
return depth[i];
//cout<<"Hello";
}
int main()
{
int N;
cin>>N;
int i = 0;
//vector<int>arr[N];
for(i=0;i<N-1;i++)
{
int p,q;
cin>>p>>q;
arr[p-1].push_back(q-1);
arr[q-1].push_back(p-1);
//cout<<"Success";
}
memset(visited,0,sizeof(visited));
int root;
cin>>root;
visited[root-1]=1;
precompute(root-1);
int q;
cin>>q;
for(int i=0;i<N;i++)
{
cout<<endl<<"depth"<<depth[i];
}
while(q--)
{
int r;
cin>>r;
cout<<depth[r-1]<<endl;
}
return 0;
}