luoguP3806 【模板】点分治1

屑题。。。。
调的比较久、、、、、
直接每次取重心,暴力检测答案就行了。。。

#include<bits/stdc++.h>
#define MAXN 80005
typedef long long ll;
using namespace std;

int n,m,k,h[MAXN],tot,vis[MAXN],sz[MAXN],zhong[MAXN],maxl,ask[MAXN],ans[MAXN];
int dis[MAXN],js,dui[MAXN],in[40000005],jjs;

struct node{
	int from,to,nex,cost;
}e[MAXN << 1];

void add(int x , int y , int z){
	tot++;
	e[tot].from = x;
	e[tot].to = y;
	e[tot].cost = z;
	e[tot].nex = h[x];
	h[x] = tot;
	return;
}

void getsz(int now , int fa){
	sz[now] = 1;
	for(int i = h[now] ; i != (-1) ; i = e[i].nex){
		if(e[i].to == fa || vis[e[i].to])continue;
		getsz(e[i].to , now);
		sz[now] += sz[e[i].to];
	}
	return;
}

void getzx(int now , int fa , int yl){
	zhong[now] = 0;
	for(int i = h[now] ; i != (-1) ; i = e[i].nex){
		if(e[i].to == fa || vis[e[i].to])continue;
		getzx(e[i].to , now , yl);
		if(sz[zhong[now]] < sz[e[i].to])zhong[now] = e[i].to;
	}
	zhong[now] = max(sz[zhong[now]] , sz[yl] - sz[zhong[now]]);
	if(zhong[now] < zhong[maxl])maxl = now;
	return;
}

void getdis(int now , int fa){
	for(int i = h[now] ; i != (-1) ; i = e[i].nex){
		if(e[i].to == fa || vis[e[i].to])continue;
		dis[e[i].to] = dis[now] + e[i].cost;
		getdis(e[i].to , now);
	}
	dui[++js] = dis[now];
	return;
}

void calc(int now){
	js = 0;
	for(int i = h[now] ; i != (-1) ; i = e[i].nex){
		if(vis[e[i].to])continue;
		dis[e[i].to] = e[i].cost , jjs = js;
		dui[++js] = dis[e[i].to];
		getdis(e[i].to , now);
		
		//ÏȶÔÓÚÿ¸öѯÎÊ
		for(int i = 1 ; i <= m ; i++){
			for(int j = jjs + 1 ; j <= js ; j++){
				if(ask[i] < dui[j])continue;
				if(in[ask[i] - dui[j]])ans[i] = 1;
			}
		}
		for(int i = jjs ; i <= js ; i++)if(dui[i] <= 10000005)in[dui[i]] = 1;
	}
	for(int i = 1 ; i <= js ; i++)in[dui[i]] = 0;
	return;
}

void solve(int now){
	vis[now] = 1;
	calc(now);getsz(now , now);
	for(int i = h[maxl] ; i != (-1) ; i = e[i].nex){
		if(vis[e[i].to])continue;
		maxl = 0;getzx(e[i].to , now , e[i].to);
		solve(maxl);
	}
	return;
}

void init(){
	memset(ans , -1 , sizeof(ans));
	memset(in, 0 , sizeof(in));
	memset(h , -1 , sizeof(h));
	cin>>n>>m;
	int x,y,z;tot = 0 , sz[n + 1] = n;
	zhong[0] = 9999999;in[0] = 1;
	for(int i = 1 ; i < n ; i++){
		cin>>x>>y>>z;
		add(x , y , z);
		add(y , x , z);
	}
	for(int i = 1 ; i <= m ; i++)cin>>ask[i];
	maxl = 0;getzx(1 , 1 , n + 1);
	solve(maxl);
	for(int i = 1 ; i <= m ; i++){
		if(ans[i] == 1)cout<<"AYE"<<endl;
		else cout<<"NAY"<<endl;
	}
}



int main(){
	init();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值