CF 196 Div 2 D (树形dp)

在一个由n个定居点构成的沼泽区域中,帕拉丁·马诺卡追踪到了一本古老的邪恶之书。通过已知受其影响的m个定居点及损害范围d,利用图论与算法知识确定可能藏匿该书的位置。

D. Book of Evil
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.

The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.

Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.

Input

The first line contains three space-separated integers nm and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains mdistinct space-separated integers p1, p2, ..., pm (1 ≤ pi ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.

Output

Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.

Sample test(s)
input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
output
3
Note

Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.






/*
思路:求出离i这个点最远的受影响的点作为i这点的属性,然后当这个属性小于等于d,那么这个点可能有鬼

*/

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef __int64 ll;

using namespace std;
#define INF 0x3f3f3f3f
#define N 100005

int vis[N];
int n,m,d;
int head[N];
int num;

int dp[N][2];
int dir[N][2];

struct stud{
  int to,ne;
}e[N*2];

inline void add(int u,int v)
{
    e[num].to=v;
    e[num].ne=head[u];
    head[u]=num++;
}

void dfs(int u,int pre)
{
     if(vis[u]) dp[u][0]=dp[u][1]=0;
     else dp[u][0]=dp[u][1]=-INF;
     dir[u][0]=dir[u][1]=0;

    for(int i=head[u];i!=-1;i=e[i].ne)
    {
        int to=e[i].to;
        if(to==pre) continue;
        dfs(to,u);
        if(dp[to][1]+1>dp[u][0])
        {
            dp[u][0]=dp[to][1]+1;
            dir[u][0]=to;
            if(dp[u][0]>dp[u][1])
            {
                swap(dp[u][0],dp[u][1]);
                swap(dir[u][0],dir[u][1]);
            }
        }
    }
}

void dfss(int u,int pre)
{
     int i,j;
     for(int i=head[u];i!=-1;i=e[i].ne)
     {
         int to=e[i].to;
         if(to==pre) continue;
         if(dir[u][1]==to)
         {
             if(dp[u][0]+1>dp[to][0])
             {
                 dp[to][0]=dp[u][0]+1;
                 dir[to][0]=u;
                 if(dp[to][0]>dp[to][1])
                 {
                     swap(dp[to][0],dp[to][1]);
                     swap(dir[to][1],dir[to][0]);
                 }
             }
         }
         else
             if(dp[u][1]+1>dp[to][0])
             {
                 dp[to][0]=dp[u][1]+1;
                 dir[to][0]=u;
                 if(dp[to][0]>dp[to][1])
                 {
                     swap(dp[to][0],dp[to][1]);
                     swap(dir[to][1],dir[to][0]);
                 }
             }
        dfss(to,u);
     }
}

int main()
{
    int i,j;
    while(~scanf("%d%d%d",&n,&m,&d))
    {
        memset(head,-1,sizeof(head));
        num=0;
        int x;
        memset(vis,0,sizeof(vis));
        for(i=0;i<m;i++)
        {
            scanf("%d",&x);
            vis[x]=1;
        }
        int u,v;
        i=n-1;
        while(i--)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
       dfs(1,-1);

       int ans=0;
//       for(i=1;i<=n;i++)
//         printf("%d ",dp[i][1]);
//       printf("\n");
        dfss(1,-1);
       for(i=1;i<=n;i++)
        if(dp[i][1]<=d) ans++;

       printf("%d\n",ans);
    }
    return 0;
}



### 关于 Codeforces CF994 Div. 2 的题目与解答 #### 题目概述 Codeforces Round #412 (Div. 2),即 CF994,采用动态评分机制。这种机制意味着一个问题的最大分值取决于解决问题的人数与总参赛人数的比例[^1]。 #### 动态评分机制解释 对于该轮比赛而言,如果某道题目的解决者数量占总参与者的比例较低,则这道题目的分数会相对较高;反之则低。所有至少提交了一次代码的人都被视为参加了这场比赛。 #### 示例解法展示 考虑到不同的算法挑战,在这里提供一道关于字符串处理的问题及其解决方案作为例子: ##### 不同字符计数问题 给定一个长度不超过 \(10^5\) 的字符串,目标是计算其中不同字符的数量并输出重复字符的次数。以下是实现这一功能的一个 C++ 程序片段: ```cpp #include<bits/stdc++.h> using namespace std; const int N=100000+10; char a[N]; int main(){ int n; while(~scanf("%d",&n)){ scanf("%s",a); sort(a,a+n); int x=unique(a,a+n)-a; // 计算不重复字符数目 if(n>26) printf("-1\n"); else printf("%d\n",n-x); // 输出重复字符个数 } return 0; } ``` 此程序通过 `sort` 函数对输入字符串进行了排序,并利用 STL 中的 `unique()` 来去除相邻相同的元素,从而统计出独一无二的字符数量[^2]。 #### 构建三维结构体模型 另一个有趣的案例涉及构建由立方体组成的二维网格表示的物体。每个位置上的整数值代表堆叠在此处的小方块的高度。为了重建这个对象的外观视角下的形态,可以按照如下方法操作: ```cpp #include<bits/stdc++.h> using namespace std; const int N=107; int n,m,h,mp[N][N],a[N],b[N],i,j,k; int main(){ for(scanf("%d%d%d",&n,&m,&h),i=1;i<=m;++i){ scanf("%d",a+i); } for(i=1;i<=n;++i){ scanf("%d",b+i); } for(i=1;i<=n;++i){ for(j=1;j<=m;++j){ scanf("%d",&mp[i][j]); if(mp[i][j]){ mp[i][j]=min(a[j],b[i]); // 取主视图和侧视图高度较小者 } } } for(i=1;i<=n;++i,puts("")){ for(j=1;j<=m;++j){ printf("%d ",mp[i][j]); } } } ``` 这段代码接收了两组数据——分别对应每一列以及每一行的最大可能高度限制,并据此调整实际放置的立方体高度以满足视觉效果的要求[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值