Codeforces 337D Book of Evil【树形Dp】好题~好题~

来源:互联网 发布:淘宝怎么设置起拍数量 编辑:程序博客网 时间:2024/05/20 23:06

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 m distinct 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.

Examples
input
6 2 31 21 52 33 44 55 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.



题目大意:


给你一颗N个节点的树,已知我们在一个节点u放置一本魔法书的话,距离节点u小于等于d的节点就会出现魔鬼。

现在已知有m个点肯定有魔鬼,问哪些点可以放置魔法书。


思路:


我们设定Dp【u】表示以u为根的子树中有魔鬼的点距离节点u最远的长度。

那么不难推出其状态转移方程:Dp【u】=max(Dp【v】+1,Dp【u】)【保证要以v为子树中带有魔鬼点】;


然而我们只有一个点的子树中的信息,如果我们能够得到数组:Dis【i】表示所有带有魔鬼的点到点i的距离最远长度的话,问题也就解决了。

那么设定F【i】表示节点i除了其子树以外所有带魔鬼的点距离点i的最远距离。


那么也不难推出其状态转移方程:

1.从其兄弟节点而来:F【u】=max(dp【brother】+2,F【u】)【保证要以brother为子树中带有魔鬼点】;

2.从其父亲节点(从根到节点u这条路上的转移)而来:F【u】=max(F【from】+1,F【u】)【保证要从根到当前父亲节点这条路径上带有魔鬼点】;


其中1如果一个父节点带有过多的子节点的话,暴力枚举会超时,所以维护一个数组Id【u】表示节点u中,子节点dp【v】最大的点的编号。

那么在第二次Dp的时候,就可以避免暴力枚举超时的情况了。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int >mp[150000];int vis[150000];int dp[150000];int fa[150000];int Id[150000];int F[150000];int n,m,d;void Dfs(int u,int from){    if(vis[u]==1)dp[u]=0;    int maxn=-0x3f3f3f3f;    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(v==from)continue;        Dfs(v,u);        if(dp[v]!=-1)dp[u]=max(dp[v]+1,dp[u]);        if(maxn<dp[v])        {            maxn=dp[v];            Id[u]=v;        }    }}void dfs(int u,int from){    fa[u]=from;    if(vis[u]==1)F[u]=0;    if(from!=-1&&F[from]!=-1)F[u]=max(F[u],F[from]+1);    if(from!=-1&&Id[from]==u)    {        for(int i=0;i<mp[from].size();i++)        {            int v=mp[from][i];            if(v==from)continue;            if(v==u)continue;            if(from!=-1&&v==fa[from])continue;            if(dp[v]!=-1)F[u]=max(F[u],dp[v]+2);        }    }    else    {        if(from!=-1&&dp[Id[from]]!=-1)F[u]=max(F[u],dp[Id[from]]+2);    }    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(v==from)continue;        else dfs(v,u);    }}int main(){    while(~scanf("%d%d%d",&n,&m,&d))    {        memset(F,-1,sizeof(F));        memset(dp,-1,sizeof(dp));        for(int i=1;i<=n;i++)mp[i].clear();        memset(vis,0,sizeof(vis));        for(int i=1;i<=m;i++)        {            int x;            scanf("%d",&x);            vis[x]=1;        }        for(int i=2;i<=n;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);            mp[y].push_back(x);        }        Dfs(1,-1);        dfs(1,-1);                int output=0;        for(int i=1;i<=n;i++)        {            int dis=max(F[i],dp[i]);            if(dis<=d)output++;        }        printf("%d\n",output);    }}






原创粉丝点击