题目链接:http://codeforces.com/problemset/problem/337/D

题意:

  给你一棵树,n个节点。

  如果一个节点处放着“罪恶之书”,那么它会影响周围距离不超过d的所有节点。

  然后告诉你一部分被影响的节点aff[i],共m个。

  已知有且仅有一个节点放着“罪恶之书”。

  现在问你有多少个节点可能放着“罪恶之书”。

题解:

  如果一个节点放着“罪恶之书”,那么它到所有aff[i]的距离都不超过d。

  也就是:max(它到aff[i]的距离) <= d

  有一个关于树的直径的结论:

    从一个点出发,不重复经过节点,若要使走的路程最远,则最终到达的点一定是树的直径的某个端点。

  在这道题中就是:

    从一个点出发,若到达aff[i]的距离在所有受影响的节点中最大。

    则节点i一定是受影响的点中,两两距离最远的一对点(op,ed)中的一个。

  所以要找出在aff[i]中,两两距离最远的一对点(op,ed)。

  也就是求所有aff[i]构成的一棵树的直径:

    先随便找一个aff[i],从它开始dfs1一遍,找出最远的点即为op。

    再从op开始,dfs1一遍,找出ed。

  然后从op和ed分别做一次dfs2,给每个距离不超过d的点的cnt加1。

  最后统计一下cnt为2的点的个数,即为答案。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX_N 100005 using namespace std; int n,m,d;
int maxd;
int op,ed;
int aff[MAX_N];
int cnt[MAX_N];
bool flag[MAX_N];
vector<int> edge[MAX_N]; void read()
{
cin>>n>>m>>d;
memset(flag,false,sizeof(flag));
for(int i=;i<=m;i++)
{
cin>>aff[i];
flag[aff[i]]=true;
}
int x,y;
for(int i=;i<n;i++)
{
cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
} void dfs1(int now,int p,int nd,int &v)
{
if(nd>maxd && flag[now])
{
maxd=nd;
v=now;
}
for(int i=;i<edge[now].size();i++)
{
int temp=edge[now][i];
if(temp!=p)
{
dfs1(temp,now,nd+,v);
}
}
} void dfs2(int now,int p,int stp)
{
if(stp>d) return;
cnt[now]++;
for(int i=;i<edge[now].size();i++)
{
int temp=edge[now][i];
if(temp!=p) dfs2(temp,now,stp+);
}
} void work()
{
maxd=-;
dfs1(aff[],-,,op);
maxd=-;
dfs1(op,-,,ed);
memset(cnt,,sizeof(cnt));
dfs2(op,-,);
dfs2(ed,-,);
int ans=;
for(int i=;i<=n;i++)
{
if(cnt[i]==) ans++;
}
cout<<ans<<endl;
} int main()
{
read();
work();
}

Codeforces 337D Book of Evil:树的直径【结论】的更多相关文章

  1. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  2. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

  3. codeforces 337D Book of Evil(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Book of Evil Paladin Manao caught the tra ...

  4. Codeforces 379F New Year Tree 树的直径的性质推理

    New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...

  5. Codeforces 337D Book of evil

    一道树形dp,写出来是因为最近也做了道类似的.这题是看了分析的思路才做出来的,但感觉很多这样的dp都是利用类似的性质.像这题的话distDown很好想,但distUp的时候就很难想了,其实只要抓住di ...

  6. codeforces 337D 树形DP Book of Evil

    原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...

  7. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  8. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  9. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

随机推荐

  1. iOS中文输入法多次触发的问题及解决方案

    最近要在移动端实现一个文本框实时搜索的功能,即在文本框里每输入一个字,就向服务器请求一次搜索结果.暂且不考虑性能优化问题,第一时间想到的是用keyup实现: $('input').on('keyup' ...

  2. CGI是什么 搜索了这么多,大致看明白了保留下来。

    转自:http://blog.chinaunix.net/uid-13408389-id-2894933.html 分类: CGI是什么 CGI是CommonGatewayInterface 的简称. ...

  3. oracle查看用户有哪些权限和角色

    select * from dba_sys_privs t where t.grantee='HR';select * from dba_role_privs t where t.grantee='H ...

  4. MapReudce源码分析之Mapper

    Mapper是MapReduce编程模型中一个将输入的key/value对映射成一组中间key/value对的组件.Map是将输入记录转换成中间记录的单个任务.被转换的中间记录不需要与输入记录一样的类 ...

  5. Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha

    Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...

  6. Android双缓冲技术

    参考文章: 1.http://djt.qq.com/article/view/987 2.http://blog.csdn.net/i_lovefish/article/details/7913623 ...

  7. Linux4_文件操作

    以下操作都是在终端命令行: 1 apt-get install   应用名称,(---:apt-get是从Ubuntu的软件应用里自动下载) 如果你不知道下载,随便输入:java,javac,tree ...

  8. 转载:Python 包管理工具解惑

    Python 包管理工具解惑 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本站部分原创和翻译文章提供m ...

  9. ASIHTTPRequest数据压缩

    本文转载至  http://blog.csdn.net/zhuoyuetec/article/details/18216439 IOSASIHttprequestsetShouldCompressRe ...

  10. ios推送服务,php服务端

    本文转载至http://my.oschina.net/AStar/blog/176531   生成证书 证书生成参考:https://parse.com/tutorials/ios-push-noti ...