Demoe @ 2021-08-01 19:12:19
T 飞了
所有点都跑了 300+ ms/kk
求帮调/kel
// wish to get better qwq
#include<bits/stdc++.h>
#define re register int
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define dec(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
template<class T> inline void rd(T &x){
int fl=1;x=0;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') fl=-fl;
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
x*=fl;
}
template<class T> inline void wr(T x){
if(x<0) x=-x,putchar('-');
if(x<10){putchar(x+'0');return ;}
int tmp[30]={0},cnt=0;
while(x) tmp[cnt++]=x%10,x/=10;
for(re i=cnt-1;i>=0;--i) putchar(tmp[i]+'0');
}
// ---------- IO ---------- //
const int N=1e4+5,K=105;
int n,m,cnte,hd[N],maxn[N],siz[N],root,cnt,st[N],id[N],dis[N],w[K];
bool vis[N],ok[K];
struct edge{int t,nxt,v;}es[N<<1];
inline void add(int x,int y,int z){es[++cnte]=(edge){y,hd[x],z};hd[x]=cnte;}
inline int get_root(int x,int fa,int tot){
siz[x]=1;maxn[x]=0;
for(re i=hd[x];i;i=es[i].nxt)
if(es[i].t!=fa&&!vis[es[i].t]){
get_root(es[i].t,x,tot);
siz[x]+=siz[es[i].t];maxn[x]=max(maxn[x],siz[es[i].t]);
}
maxn[x]=max(maxn[x],tot-siz[x]);
if(!root||maxn[x]<maxn[root]) root=x;
}
inline void get_dis(int x,int fa,int d,int tag){
st[++cnt]=x;dis[x]=d;id[x]=tag;
for(re i=hd[x];i;i=es[i].nxt)
if(es[i].t!=fa&&!vis[es[i].t]) get_dis(es[i].t,x,d+es[i].v,tag);
}
inline bool cmp(int x,int y){return dis[x]<dis[y];}
inline int calc(int x){
cnt=0;st[++cnt]=x;dis[x]=0;id[x]=x;
for(re i=hd[x];i;i=es[i].nxt)
if(!vis[es[i].t]) get_dis(es[i].t,x,es[i].v,es[i].t);
sort(st+1,st+cnt+1,cmp);
for(re i=1;i<=m;++i){
int l=1,r=cnt;
if(ok[i]) continue;
while(l<r){
if(dis[st[l]]+dis[st[r]]>w[i]) --r;
else if(dis[st[l]]+dis[st[r]]<w[i]) ++l;
else{
if(id[st[l]]==id[st[r]]){
if(id[st[r]]==id[st[r-1]]) --r;
else ++l;
}
else{ok[i]=1;break;}
}
}
}
}
inline void solve(int x){
vis[x]=1;calc(x);
for(re i=hd[x];i;i=es[i].nxt)
if(!vis[es[i].t]) root=0,get_root(es[i].t,0,siz[es[i].t]),solve(root);
}
// ---------- ---------- //
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
rd(n);rd(m);
for(int i=1,x,y,z;i<n;++i){
rd(x);rd(y);rd(z);add(x,y,z);add(y,x,z);
}
for(re i=1;i<=m;++i) rd(w[i]),ok[i]=(w[i]==0);
maxn[0]=n;get_root(1,0,n);solve(root);
for(re i=1;i<=m;++i) puts(ok[i]?"AYE":"NAY");
return 0;
}
// ---------- Main ---------- //
by UltiMadow @ 2021-08-01 19:31:10
@Demoe 为啥您的 int 函数没返回值啊/youl
by Demoe @ 2021-08-01 19:49:19
@UltiMadow
全改成 void 就过了/bx/bx/bx
by do_while_true @ 2021-08-01 19:50:12