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 154 155 156 157 158 159 160 161
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register #define db double typedef long long ll; using namespace std; const int MAXN=1e7+1; const int MAXM=1e5+1; template<class T> inline void read(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline bool checkMax(T &x,T &y) { return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T &y) { return x>y?x=y,1:0; } int N,M; struct Road { int next,to,val; Road(int next=0,int to=0,int val=0): next(next),to(to),val(val){} }Edge[MAXN<<1]; int Head[MAXN],Total; int Pos,ans_Pos,Size[MAXN]; bool vis_Pos[MAXN],vis[MAXN]; inline void addEdge(int u,int v,int w) { Edge[++Total]=Road(Head[u],v,w);Head[u]=Total; Edge[++Total]=Road(Head[v],u,w);Head[v]=Total; } void dfs_Pos(int x,int fa,int size) { Size[x]=1; int Max_Part=0; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==fa||vis[v]) continue; dfs_Pos(v,x,size); Size[x]+=Size[v]; Max_Part=max(Max_Part,Size[v]); } Max_Part=max(Max_Part,size-Max_Part); if(!Pos||Max_Part<ans_Pos) { ans_Pos=Max_Part; Pos=x; } } int blg[MAXN],dis[MAXN],query[MAXM]; int num[MAXN],Tot; inline bool cmp(int x,int y) { return dis[x]<dis[y]; } void dfs_Dist(int x,int fa,int val,int u) { num[++Tot]=x; dis[x]=val,blg[x]=u; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==fa||vis[v]) continue; dfs_Dist(v,x,val+Edge[e].val,u); } } bool ans[MAXM]; void dfs_Calc(int x) { Tot=0; num[++Tot]=x; dis[x]=0,blg[x]=x; for(int e=Head[x],v;e;e=Edge[e].next) { if(vis[(v=Edge[e].to)]) continue; dfs_Dist(v,x,Edge[e].val,v); } sort(num+1,num+1+Tot,cmp); for(int i=1;i<=M;++i) { int l=1,r=Tot; while(l<r) { if(dis[num[l]]+dis[num[r]]>query[i]) --r; else if(dis[num[l]]+dis[num[r]]<query[i]) ++l; else if(blg[num[l]]==blg[num[r]]) { if(dis[num[r]]==dis[num[r-1]]) --r; else ++l; } else { ans[i]=1; break; } } } } void solve(int x) { vis[x]=1; dfs_Calc(x); for(int e=Head[x],v;e;e=Edge[e].next) { if(vis[(v=Edge[e].to)]) continue; Pos=0; dfs_Pos(v,0,Size[v]); solve(Pos); } } int main() { read(N,M); for(int i=1,u,v,w;i<N;++i) { read(u,v,w); addEdge(u,v,w); } dfs_Pos(1,-1,N); for(int i=1;i<=M;++i) read(query[i]); solve(Pos); for(int i=1;i<=M;++i) { if(ans[i]||!query[i]) printf("AYE\n"); else printf("NAY\n"); } return 0; }
|