-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
147 lines (143 loc) · 2.45 KB
/
F.cpp
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
#include "bits/stdc++.h"
using namespace std;
const int maxn = 400010;
const int logn = 19;
int n, k;
vector <int> g[maxn];
int dp[20][maxn], dep[maxn];
bool mark[maxn];
int d[maxn];
int close[maxn];
int par[maxn];
int root(int x) {
if(par[x] == x) return x;
return par[x] = root(par[x]);
}
void join(int x, int y) {
x = root(x);
y = root(y);
if(x != y) {
par[x] = y;
}
}
void bfs() {
for(int i = 1; i <= n; i++) {
par[i] = i;
d[i] = -1;
}
queue <int> Q;
for(int i = 1; i <= n; i++) {
if(mark[i]) {
Q.push(i);
d[i] = 0;
close[i] = i;
}
}
while(!Q.empty()) {
int x = Q.front();
Q.pop();
if(d[x] == k) continue;
for(int i : g[x]) {
if(d[i] == -1) {
Q.push(i);
d[i] = d[x] + 1;
close[i] = close[x];
}
join(close[x], close[i]);
}
}
for(int i = 1; i <= n; i++) {
if(d[i] != -1) {
join(i, close[i]);
}
}
}
void dfs(int x, int prev) {
dp[0][x] = prev;
for(auto i : g[x]) {
if(i != prev) {
dep[i] = 1 + dep[x];
dfs(i, x);
}
}
}
void addEdge(int x, int y) {
// cout << x << ' ' << y << endl;
g[x].push_back(y);
g[y].push_back(x);
}
int lift(int x, int k) {
for(int i = logn; i >= 0; i--) {
if(k >= (1 << i)) {
x = dp[i][x];
k -= 1 << i;
}
}
return x;
}
int lca(int x, int y) {
if(dep[x] > dep[y]) swap(x, y);
y = lift(y, dep[y] - dep[x]);
if(x == y) return x;
for(int i = logn; i >= 0; i--) {
if(dp[i][x] != dp[i][y]) {
x = dp[i][x];
y = dp[i][y];
}
}
return dp[0][x];
}
int distance(int p, int q) {
return dep[p] + dep[q] - 2 * dep[lca(p, q)];
}
int kthNode(int p, int q, int k) {
int anc = lca(p, q);
int dist = dep[p] + dep[q] - 2 * dep[anc];
if(dep[p] - dep[anc] >= k) {
return lift(p, k);
} else {
return lift(q, dist - k);
}
}
int main(int argc, char const *argv[])
{
int r;
scanf("%d %d %d", &n, &k, &r);
int tmp = n;
for(int i = 1; i < tmp; i++) {
int p, q;
scanf("%d %d", &p, &q);
addEdge(p, ++n);
addEdge(q, n);
}
for(int i = 1; i <= r; i++) {
int x;
scanf("%d", &x);
mark[x] = 1;
}
bfs();
dfs(1, 0);
for(int i = 1; i <= logn; i++) {
for(int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
int q;
scanf("%d", &q);
while(q--) {
int x, y;
scanf("%d %d", &x, &y);
if(distance(x, y) <= 2 * k) printf("YES\n");
else {
int a = kthNode(x, y, k);
int b = kthNode(y, x, k);
// cout << a << " qq " << b << endl;
if(root(a) == root(b)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
return 0;
}