-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlca_logn_query_n-preprocessing.cpp
More file actions
63 lines (63 loc) · 1.52 KB
/
lca_logn_query_n-preprocessing.cpp
File metadata and controls
63 lines (63 loc) · 1.52 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
//N preprocessing for building tree rather accessing levels
//logN time to reach same level and love up until the values become common
//The target was to find maximum valued state at the end when all nodes from path u to v
//are incremented by 1.
#include<bits/stdc++.h>
using namespace std;
void build_tree(vector<long> a[],long root,long level[],long store_parent[])
{
for(long i=0;i<a[root].size();++i)
{
if(a[root][i]!=store_parent[root])
{
level[a[root][i]]=level[root]+1;
store_parent[a[root][i]]=root;
build_tree(a,a[root][i],level,store_parent);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long n,m;
cin>>n>>m;
vector<long> a[n+1];
long level[100005];
long store_parent[100005];
long state[100005]={0};
for(long i=0;i<n-1;++i)
{
long u,v;
cin>>u>>v;
a[u].push_back(v);
a[v].push_back(u);
}
level[1]=1;
store_parent[1]=-1;
build_tree(a,1,level,store_parent);
for(long i=0;i<m;++i)
{
long u,v;
cin>>u>>v;
while(level[u]>level[v])
{
state[u]++;
u=store_parent[u];
}
while(level[u]<level[v])
{
state[v]++;
v=store_parent[v];
}
while(u!=v)
{
state[u]++;
state[v]++;
u=store_parent[u];
v=store_parent[v];
}
state[u]++;
}
cout<<*max_element(state,state+n+2);
}