-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3806 点分治.cpp
110 lines (110 loc) · 2.29 KB
/
P3806 点分治.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
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e4 + 5;
int n , m , q[105]; bool vis[N] , ans[105];
int tot , head[N] , nxt[N * 2] , to[N * 2] , c[N * 2];
int sz[N] , dis[N] , minsz , center , cnt;
pair<int , int>a[N];
void Add(int u , int v , int w)
{
to[++tot] = v , c[tot] = w;
nxt[tot] = head[u] , head[u] = tot;
}
void GetCenter(int p , int fa , int total)
{
int maxsz = 0;
sz[p] = 1;
for(int i = head[p] ; i ; i = nxt[i])
{
int v = to[i];
if(vis[v] || v == fa)continue;
GetCenter(v , p , total);
sz[p] += sz[v];
maxsz = max(maxsz , sz[v]);
}
maxsz = max(maxsz , total - sz[p]);
if(maxsz < minsz)
{
minsz = maxsz;
center = p;
}
}
void GetDis(int p , int fa , int rt)
{
a[++cnt] = make_pair(dis[p] , rt);
for(int i = head[p] ; i ; i = nxt[i])
{
int v = to[i] , w = c[i];
if(vis[v] || v == fa)continue;
dis[v] = dis[p] + w;
if(dis[v] <= 1e7)GetDis(v , p , rt);
}
}
void Calc(int rt)
{
dis[rt] = 0;
a[cnt = 1] = make_pair(0 , rt);
for(int i = head[rt] ; i ; i = nxt[i])
{
int v = to[i];
if(vis[v])continue;
dis[v] = c[i];
GetDis(v , rt , v);
}
sort(a + 1 , a + cnt + 1);
;//cout << '\t' << rt << ':';
for(int i = 1 ; i <= cnt ; i++)
;//cout << a[i].first << ',' << a[i].second << " ";
;//cout << endl;
for(int i = 1 ; i <= m ; i++)
{
int k = q[i];
;//cout << "\tK" << k << ':';
for(int l = 1 , r = cnt ; l < r ; l++)
{
while(l < r && a[l].first + a[r].first > k)r--;
if(a[l].first + a[r].first == k && a[l].second != a[r].second)
{
ans[i] = 1;
break;
}
;//cout << l << ' ' << r << " ";
}
;//cout << endl;
}
}
void Dfs(int p)
{
GetCenter(p , 0 , 0);
center = 0 , minsz = N;
GetCenter(p , 0 , sz[p]);
int rt = center;
;//cout << rt << ' ' << sz[rt] << endl;
vis[rt] = 1;
Calc(rt);
for(int i = head[rt] ; i ; i = nxt[i])
{
int v = to[i];
if(vis[v])continue;
Dfs(v);
}
}
int main()
{
scanf("%d%d" , &n , &m);
for(int i = 1 ; i < n ; i++)
{
int u , v , w;
scanf("%d%d%d" , &u , &v , &w);
Add(u , v , w);
Add(v , u , w);
}
for(int i = 1 ; i <= m ; i++)
scanf("%d" , &q[i]);
Dfs(1);
for(int i = 1 ; i <= m ; i++)
puts((ans[i] ? "AYE" : "NAY"));
return 0;
}