许豪 @ 2020-04-02 23:05:54
#include<map>
#include<iostream>
using namespace std;
int n,m;
int u,v,w;
int head[30001];
int gg=0;
map<int,int> fina;
struct d
{
int l;
int qian;
int hao;
}shu[60001];
int wen[101];
int fang[30001];
int zi[30001];
int los;
int zan[30001];
int zhi(int,int);
bool tong[10000001];
int guo[101];
int getzi(int,int);
int adde(int,int,int);
int zhong,biao;
int main()
{
cin>>n>>m;
for(int i=2;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);
adde(v,u,w);
}
for(int i=1;i<=m;i++)
scanf("%d",&wen[i]);
zi[1]=n;
zhi(1,0);
for(int e=1;e<=m;e++)
if(guo[e]==1)
printf("AYE\n");
else printf("NAY\n");
return 0;
}
int adde(int uu,int vv,int ww)
{
shu[++gg].qian=head[uu];
shu[gg].hao=vv;
shu[gg].l=ww;
head[uu]=gg;
}
int xin(int tr,int ba,int size)
{
int giga=0;
zi[tr]=1;
for(int i=head[tr];i;i=shu[i].qian)
if(fang[shu[i].hao]==0&&ba!=shu[i].hao)
{
xin(shu[i].hao,tr,size);
zi[tr]+=zi[shu[i].hao];
giga=max(giga,zi[shu[i].hao]);
}
giga=max(giga,size-zi[tr]);
if(giga<biao)
{
biao=giga;
zhong=tr;
}
return 0;
}
int dis(int rt,int fu,int shen)
{
if(shen>10000000) return 0;
zan[++zan[0]]=shen;
for(int i=1;i<=m;i++)
if(wen[i]>=shen)
if(tong[wen[i]-shen]==1) guo[i]=1;
for(int e=head[rt];e;e=shu[e].qian)
if(fang[shu[e].hao]==0&&shu[e].hao!=fu)
dis(shu[e].hao,rt,shen+shu[e].l);
return 0;
}
int zhi(int rt,int fu)
{
int qq=1;
fang[rt]=1;
zan[0]=0;
tong[0]=1;
for(int e=head[rt];e;e=shu[e].qian)
if(fang[shu[e].hao]==0&&shu[e].hao!=fu)
{
dis(shu[e].hao,rt,shu[e].l);
for(int w=qq;w<=zan[0];w++)
tong[zan[w]]=1;
qq=zan[0]+1;
}
for(int i=1;i<=zan[0];i++) tong[zan[i]]=0;
for(int s=head[rt];s;s=shu[s].qian)
if(fang[shu[s].hao]==0&&shu[s].hao!=fu)
{
biao=999999999;
los=getzi(shu[s].hao,0);
xin(shu[s].hao,rt,los);
zhi(zhong,rt);
}
return 0;
}
int getzi(int gs,int fu)
{
int ans=1;
for(int i=head[gs];i;i=shu[i].qian)
if(shu[i].hao!=fu&&fang[shu[i].hao]==0)
ans+=getzi(shu[i].hao,gs);
return ans;
}
by huanyue @ 2020-04-02 23:11:03
像是数组越界吧
by 许豪 @ 2020-04-03 13:59:56
@huanyue 并不是数组越界,,,