Codeforces 337d

树形dp,还是要想好再写啊,T^T;

#include <cstdio>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int inf = 0x3f3f3f3f;
struct node{
	int v;
	int next;
}e[maxn*2];
int head[maxn];
int tot;
bool marked[maxn];
int max1[maxn];
int max2[maxn];
template<class T> void checkmax(T &a,T b){
	if(b > a)a = b;
}
void add(int u,int v){
	e[tot].v = v;
	e[tot].next = head[u];
	head[u] = tot ++;
}

void init(){
	memset(head,-1,sizeof(head));
	tot = 0;

}

int n,m,d;

void dfs1(int u,int pre){
	if(marked[u] == true){
		max1[u] = 0;
	}
	for(int i=head[u];i!=-1;i=e[i].next){
		int v = e[i].v;
		if(v == pre)continue;
		dfs1(v,u);
		/*
		if(max2[v] != -1){
			checkmax(max2[u],max2[v]+1);
			if(max2[u] > max1[u])swap(max2[u],max1[u]);//important
		}
		*/
		if(max1[v] != -1){
			checkmax(max2[u],max1[v]+1);
			if(max2[u] > max1[u])swap(max2[u],max1[u]);//important
		}
	}
}

void dfs2(int u,int pre){
	for(int i=head[u];i!=-1;i=e[i].next){
		int v = e[i].v;
		if(v == pre)continue;
		if(max1[u] == -1);
		else {
			if(max1[v] != -1 && max1[u] == max1[v] + 1){
				if(max2[u]!=-1)checkmax(max2[v],max2[u]+1);
			}
			else checkmax(max2[v],max1[u]+1);
			if(max2[v] > max1[v])swap(max2[v],max1[v]);//important
		}
		dfs2(v,u);
	}
}

int main(){
	scanf("%d%d%d",&n,&m,&d);
	memset(marked,false,sizeof(marked));
	for(int i=0;i<m;i++){
		int t;
		scanf("%d",&t);
		marked[t] = true;
	}
	init();
	for(int i=0;i<n-1;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v);
		add(v,u);
	}
	memset(max1,-1,sizeof(max1));
	memset(max2,-1,sizeof(max2));
	dfs1(1,0);
	for(int i=1;i<=n;i++){
		//printf("%d %d %d\n",i,max1[i],max2[i]);
	}
	dfs2(1,0);
	int ans = 0;
	for(int i=1;i<=n;i++){
		//printf("%d %d %d\n",i,max1[i],max2[i]);
		if(max1[i] <= d)ans ++;
	}
	printf("%d\n",ans);
	return 0;	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值