【NOIP模拟赛】林下风气

在这里插入图片描述

题解 :

dp维护乱搞,强行枚举最小值,然后算出联通块满足的值f[i][1]和不满足的值f[i][0];

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3350,P=19260817;
int n,k,rt,num;
int a[N],last[N];
ll f[N][2],ans;
struct edge {
	int to,next;
} e[2*N];
void add(int x,int y) {
	e[++num].to=y;
	e[num].next=last[x];
	last[x]=num;
}
void dfs(int x,int fa) {
	if(a[x]==a[rt]+k) f[x][1]=1,f[x][0]=0;
	else f[x][1]=0,f[x][0]=1;
	for(int w=last[x]; w; w=e[w].next) {
		int y=e[w].to;
		if(y==fa||a[y]==a[rt]&&y>rt||a[y]<a[rt]||a[y]>a[rt]+k) continue;
		dfs(y,x);
		f[x][1]=(f[x][1]+f[x][1]*f[y][0]%P+f[x][0]*f[y][1]%P+f[x][1]*f[y][1]%P)%P;
		f[x][0]=(f[x][0]+f[x][0]*f[y][0]%P)%P;
	}
}
int read(){
	int num=0;
	char ch=getchar();
	while(ch>'9'||ch<'0'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		num=(num<<1)+(num<<3)+ch-'0';
		ch=getchar();
	}
	return num;
}
int main() {
	n=read(),k=read();
	for(int i=1;i<=n;i++) 
		a[i]=read();
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		add(x,y),add(y,x);
	}
	for(int i=1;i<=n;i++) 
		rt=i,dfs(rt,0),ans=(ans+f[i][1])%P;
	printf("%lld",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值