forked from bajajvinamr/CodeForces-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq.cpp
More file actions
153 lines (138 loc) · 3.6 KB
/
Copy pathq.cpp
File metadata and controls
153 lines (138 loc) · 3.6 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define rep(i,a,b) for(int i = a; i < b; i++)
#define repp(i,a,b) for(int i = a; i <= b; i++)
#define endl '\n'
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mod 998244353
#define mod_in(a) bin(a, mod - 2)
#define fact(n) rep(i, 1, n+1) ft.pb((ft[i-1] * i) % mod)
#define ncr(n, r) (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n)-(r)]) % mod)) % mod) : 0LL)
#define pi 3.14159265358979323
#define INF 1e15
#define lb(v, a) lower_bound(v.begin(), v.end(), a)
#define ub(v, a) upper_bound(v.begin(), v.end(), a)
#define deb(x) cerr<<#x<<" "<<x<<endl;
// Ordered Set
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// template<typename T>
// using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
// #define os_find(k) find_by_order(k)
// #define os_order(k) order_of_key(k)
// //(**FOR USING ORDERED SET CHANGE INT**)
int power(int x, int n){
int res=1;
while(n>0){
if(n&1)
res=res*x;
x*=x;
n>>=1;
}
return res;
}
int bin(int x, int n){
int res=1; x%=mod;
while(n){
if(n&1) res=(res*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return res;
}
bool prime(int n){
if(n==3)return 1;
repp(i,2,sqrt(n)){
if(n%i==0)return 0;
}
return 1;
}
//DSU
void make_set(int u){
par[u] = u;
sz[u] = 1;
}
int find_set(int u){
if(u == par[u]) return u;
return par[u] = find_set(par[u]);
}
void union_set(int u, int v){
int a = find_set(u);
int b = find_set(v);
if(a == b) return;
if(sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
}
// Sieve
vector<int> pr(1000005, 1);
void sieve(int n){
pr[1] = 0; pr[0] = 0;
for(int p = 2; p <= n; p++){
if(pr[p] == 1){
for(int i = p * p; i <= n; i += p){
pr[i] = 0;
}
}
}
}
int arr[200001], p[200001], h[200001], ans[200001], val[200001];
int dp[200001][100];
vector<int> adj[200001];
void dfs(int n, int l, int p){
h[n] = l;
dp[n][0] = p;
for(auto i : adj[n]){
if(i != p){
dfs(i, l + 1, n);
}
}
}
void lca1(int n, int k){
repp(i, 1, k){
rep(j, 2, n + 1){
if(dp[j][i-1] != -1) dp[j][i] = dp[dp[j][i-1]][i-1];
}
}
}
int lca(int x, int y, int k){
if(h[x] > h[y]) swap(x, y);
int d = h[y] - h[x];
while(d){
int f = log2(d);
y = dp[y][f];
d -= (1 << f);
}
if(y == x) return y;
for(int i = k; i >= 0; i--){
if(dp[x][i] != -1 && (dp[x][i] != dp[y][i])) x = dp[x][i], y = dp[y][i];
}
return dp[x][0];
}
void solve(){
int n, q, c = 0;; cin >> n >> q;
memset(dp, -1, sizeof(dp));
rep(i, 0, n - 1){
int x, y; cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1, 0, -1);
int k = log2(n);
lca1(n, k);
while(q--){
int x, y; cin >> x >> y;
int node = lca(x, y, k);
if(node == x or node == y) cout << abs(h[x] - h[y]) << endl;
else cout << h[x] + h[y] - 2 * h[node] << endl;
}
}