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
| #include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
const int MAXN = 110000;
namespace fast_io {
//...
}using namespace fast_io;
struct Edge{
int from,to;
int len,nex;
}edge[MAXN];
int ecnt = 1;int fir[MAXN];
void addedge(int a,int b,int l){
edge[ecnt] = (Edge){a,b,l,fir[a]};
fir[a] = ecnt++;
edge[ecnt] = (Edge){b,a,l,fir[b]};
fir[b] = ecnt++;
}
//-----
int n,m,q[MAXN],ans[MAXN];
//-----
int f[MAXN],vis[MAXN],siz[MAXN];
int rt,sz;
struct node{
int d,f;
bool operator < (node a)const{
return d < a.d;
}
}dep[MAXN];
int num;
int search(int d){
int b = 1,e = num;
while(e!=b){
int mid = (b+e)>>1;
if(dep[mid].d >= d) e = mid;
else b = mid + 1;
}
return b;
}
//-----
void getroot(int nown,int fa){
siz[nown] = 1,f[nown] = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(vis[v] || v == fa) continue;
getroot(v,nown);
siz[nown] += siz[v];
f[nown] = max(f[nown],siz[v]);
}
f[nown] = max(f[nown],sz - siz[nown]);
if(f[nown] < f[rt]) rt = nown;
}
void dfs(int nown,int fa,int wh,int d){
dep[++num] = (node){d,wh};
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to,l = edge[nowe].len;
if(vis[v] || v == fa) continue;
dfs(v,nown,wh,d+l);
}
}
void work(int nown){
num = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to,l = edge[nowe].len;
if(vis[v]) continue;
dfs(v,nown,v,l);
}
dep[++num] = (node){0,0};
sort(dep+1,dep+num+1);
//二分查找 并判断对于每个ans是否有符合的答案
for(int i = 1;i<=m;i++){
if(ans[i]) continue;
int l = 1;
while(l < num && dep[l].d + dep[num].d < q[i])
l++;
while(l < num && 2*dep[l].d < q[i]){
if(ans[i]) break;
int t = q[i]-dep[l].d,r = search(t);
while(r <= num && dep[r].d == t && dep[l].f == dep[r].f)
r++;
ans[i] |= (dep[r].d == t);
l++;
}
}
}
void solve(int nown){
vis[nown] = 1;
work(nown);
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(vis[v]) continue;
f[rt = 0] = sz = siz[v];
getroot(v,rt);
solve(rt);
}
}
void init(){
read(n),read(m);
int a,b,c;
for(int i = 1;i<=n-1;i++){
read(a),read(b),read(c);
addedge(a,b,c);
}
for(int i = 1;i<=m;i++)
read(q[i]);
}
void solve(){
f[rt = 0] = sz = n;
getroot(1,rt);
solve(rt);
for(int i = 1;i<=m;i++){
if(ans[i]) print('A'),print('Y'),print('E');
else print('N'),print('A'),print('Y');
print('\n');
}
}
int main(){
init();
solve();
flush();
return 0;
}
|