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
| #include<iostream> #include<cstdio> using namespace std; const int maxK=10000001; int head[10005],vet[20005],nxt[20005],w[20005],num; int sz[10005],mxs[10005],root; int d[10005],top,found[10000005]; bool vis[10005]; int n,m; void addedge(int x,int y,int c) { num++;vet[num]=y;nxt[num]=head[x];w[num]=c;head[x]=num; num++;vet[num]=x;nxt[num]=head[y];w[num]=c;head[y]=num; } void getroot(int x,int ff,int tt) { sz[x]=1;mxs[x]=0; for (int e=head[x];e;e=nxt[e]) { int v=vet[e]; if (v==ff || vis[v]) continue; getroot(v,x,tt); sz[x]+=sz[v]; mxs[x]=max(mxs[x],sz[v]); } mxs[x]=max(mxs[x],tt-sz[x]); if (mxs[x]<mxs[root]) root=x; } void getdepth(int x,int ff,int depth) { d[++top]=depth; for (int e=head[x];e;e=nxt[e]) { if (vet[e]==ff || vis[vet[e]]) continue; getdepth(vet[e],x,depth+w[e]); } } void cal(int x,int typ,int val) { top=0; getdepth(x,0,0); for (int i=1;i<=top;i++) for (int j=1;j<=top;j++) { if (typ && d[i]+d[j]<=maxK) found[d[i]+d[j]]++; else if (d[i]+d[j]+val<=maxK) found[d[i]+d[j]+val]--; } } void solve(int x,int tot) { cal(x,1,0); vis[x]=true; for (int e=head[x];e;e=nxt[e]) { if (!vis[vet[e]]) { cal(vet[e],0,w[e]*2); root=0; int tt=sz[vet[e]]; if (tt>sz[x]) tt=tot-sz[x]; getroot(vet[e],x,tt); solve(root,tt); } } } int main() { scanf("%d%d",&n,&m); for (int i=1;i<n;i++) { int x,y,c; scanf("%d%d%d",&x,&y,&c); addedge(x,y,c); } mxs[0]=(n<<1);root=0; getroot(1,0,n); solve(root,n); while (m--) { int x; scanf("%d",&x); if (found[x]) printf("AYE\n"); else printf("NAY\n"); } return 0; }
|