转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

Book of Evil

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 n, m 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.

Sample test(s)
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个结点的树,在树上的某个点上有一本"book of evil",在其周围的与其距离小于等于d的点都会受到其影响,已知m个受到影响的点,求有几个可能会有"book of evil"

通过维护一个结点到其子树中的最长距离,以及除去存在最长距离之外,到另外子树的最长距离,然后再dfs一遍即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <vector>
 6 using namespace std;
 7 #define MAXN 100010
 8 vector<int>G[MAXN];
 9 int dp[MAXN][2];
10 int pre[MAXN][2];
11 int ans=0;
12 int d;
13 void dfs(int u,int fa){
14     for(int i=0;i<G[u].size();i++){
15         int v=G[u][i];
16         if(v==fa)continue;
17         dfs(v,u);
18         int d1=dp[v][0]+1;
19         if(d1>dp[u][1]){
20             dp[u][1]=d1;
21             pre[u][1]=v;
22             if(dp[u][1]>dp[u][0]){
23                 swap(dp[u][0],dp[u][1]);
24                 swap(pre[u][0],pre[u][1]);
25             }
26         }
27     }
28 }
29 void dfs2(int u,int fa,int dis){
30     if(dis>d)return;
31     if(dp[u][0]<=d)ans++;//cout<<u<<endl;}
32     int d1=max(dp[u][0],dis)+1;
33     int d2=max(dp[u][1],dis)+1;
34     for(int i=0;i<G[u].size();i++)
35     {
36         int v=G[u][i];
37         if(v==fa)continue;
38         if(pre[u][0]==v)dfs2(v,u,d2);
39         else dfs2(v,u,d1);
40     }
41 }
42 int main()
43 {
44     ios::sync_with_stdio(false);
45     //freopen("in.in","r",stdin);
46     int m,n;
47     int u,v;
48     cin>>n>>m>>d;
49     for(int i=0;i<n;i++)dp[i][0]=dp[i][1]=-MAXN;
50     for(int i=0;i<n;i++)pre[i][0]=pre[i][1]=-1;
51     for(int i=0;i<m;i++){
52         cin>>u;
53         u--;
54         dp[u][0]=dp[u][1]=0;
55     }
56     for(int i=0;i<n;i++)G[i].clear();
57     for(int i=0;i<n-1;i++){
58         cin>>u>>v;
59         u--;v--;
60         G[u].push_back(v);
61         G[v].push_back(u);
62     }
63     ans=0;
64     dfs(0,-1);
65     dfs2(0,-1,-MAXN);
66     cout<<ans<<endl;
67     return 0;
68 }

代码君

转载于:https://www.cnblogs.com/fraud/p/4338858.html

codeforces 337D Book of Evil(dp)相关推荐

  1. Codeforces 337D Book of Evil:树的直径【结论】

    题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着"罪恶之书",那么它会影响 ...

  2. CodeForces 337D Book of Evil(双向dfs)

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  3. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  4. 【CodeForces 1253C --- Sweets Eating】DP

    [CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...

  5. codeforces 337D:树形dp[能到最远的一定可以到其他点]

    题目链接 题意:有一颗有n个结点的树,树上存在一个污染源(位置不确定),它可以污染与它距离不超过d的节点,现给出m个被污染的节点(污染源本身也可能是被污染的节点),求污染源可能的位置数. 解题思路:很 ...

  6. codeforces Palindromic characteristics(hash或者dp)

    1.动态规划 用dp(l,r)表示子串s[l..r]的回文串阶数.对于长度len为1的有dp(l,r)=1.对于长度len等于2的,看字符串左右是否相等即可.当r-l>1时,如果s[l]不等于s ...

  7. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  8. Codeforces 429B Working out:dp【枚举交点】

    题目链接:http://codeforces.com/problemset/problem/429/B 题意: 给你一个n*m的网格,每个格子上有一个数字a[i][j]. 一个人从左上角走到右下角,一 ...

  9. CodeForces - 1551F Equidistant Vertices(暴力+dp)

    题目链接:点击查看 题目大意:给出一棵 nnn 个节点组成的树,问选出 kkk 个节点满足相互之间距离相等的方案数有多少 题目分析:n=100n=100n=100,感觉数据范围越小的题目越难发现 ta ...

最新文章

  1. imopen和bwmorph_MATLAB图像处理中的应用
  2. 关于MM32-Link Programmer软件修改建议
  3. python当型循环_对python while循环和双重循环的实例详解
  4. PR值:PagePank算法
  5. 机器学习——相似度算法汇总
  6. 英雄联盟微信登录服务器怎么回事,英雄联盟微信怎么登陆 lol微信登录功能开放大区一览...
  7. 三菱fx3u中文手册_3个月高效掌握三菱PLC!四个阶段经验大总结~
  8. ZooKeeper原生java客户端使用
  9. 面试官:都说阻塞 I/O 模型将会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?
  10. Java:for循环出现for(int i : arr)
  11. 软件工程案例学习-网上购书系统
  12. 原生js获取屏幕高度
  13. 友华PT921G光猫破解获取超级密码和更改桥接模式
  14. Keras的Adam优化器decay理解及自适应学习率
  15. 用ajax进行分页查询
  16. 概率分布之二项分布、泊松分布
  17. 解决谷歌浏览器不能打开Axure原型的问题
  18. 测试管理工具大全2009版
  19. Linux编辑文件时,提示.swp文件已存在怎么办?
  20. linux一切皆是文件_Linux中“一切皆文件”是什么意思?

热门文章

  1. 超时空机战服务器配置信息错误,超时空机战熔炉篇FAQ教你如何合理的使用熔炉...
  2. 【Makefile】
  3. 原生态基于OpenCV图像处理软件开发
  4. C语言实现魔方阵代码及解析
  5. html文字添加波浪线,利用css渐变给文字下方加波浪线
  6. ajax实现表单验证 html,Ajax+ajax做的表单验证
  7. python删除字符串中指定_python删除字符串中指定字符
  8. mysql cluster 设置单向复制_mysql单向主从配置
  9. 【渝粤教育】国家开放大学2018年秋季 0107-21T现代货币金融学 参考试题
  10. 【渝粤题库】陕西师范大学180113 学前儿童艺术教育作业