题目链接


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


解题思路:很明显如果一个点能到最远的污染源那么其他都能到达。现在题目就转变成对每一个点求距离自己最远的污染点这就类似HDU2916[求所有点能到的最远距离]:不过这里要特判一下下面是否有污染点才能更新结果


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <limits.h>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
#define y1 Y
using namespace std;
const int N = 1e7 + 10, MOD = 1e9 + 7;
const int maxn = 4e5 + 10;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{read(first);read(args...);
}
vector<int>G[maxn << 1];
int down[maxn << 1][2], up[maxn << 1][2];
//0最长路,1是次长路
int n, m, k;
bool goal[maxn];
int depth[maxn], poi;
vector<int> ans[maxn];
inline void dfs1(int u, int fa)//第一次是所有节点向下搜索的遇到的最大的距离
{if(goal[u]) down[u][0] = down[u][1] = 0;for(auto it : G[u]){if(it == fa) continue;dfs1(it,u);if(down[it][0] == -1) continue;if(down[it][0] + 1 >= down[u][0]){down[u][1] = down[u][0];down[u][0] = down[it][0] + 1;}else if(down[it][0] + 1 > down[u][1])down[u][1] = down[it][0] + 1;}
}inline void dfs2(int u, int fa)
{if(down[fa][0] != -1){if(down[u][0] + 1 != down[fa][0]){if(down[u][0] < down[fa][0] + 1){down[u][1] = down[u][0];down[u][0] = down[fa][0] + 1;}else if(down[fa][0] + 1 > down[u][1])down[u][1] = down[fa][0] + 1;}else {if(down[fa][1] != -1){if(down[fa][1] + 1 > down[u][0]){down[u][1] = down[u][0];down[u][0] = down[fa][1] + 1;}else if(down[fa][1] + 1 > down[u][1])down[u][1] = down[fa][1] + 1;}}}for(auto it : G[u]){if(it == fa) continue;dfs2(it,u);}
}
int main()
{ms(down,-1);read(n,m,k);for(int i = 0; i < m; ++ i) {int x;read(x);goal[x] = true;}for(int i = 1; i < n; ++ i){int l, r;read(l,r);G[l].pb(r);G[r].pb(l);}dfs1(1,0);// for(int i = 1; i <= n; ++ i)// {//     cout << down[i][0] << "  = max " << down[i][1] << endl;// }// cout << endl;for(auto it : G[1])dfs2(it,1);int ans = 0;for(int i = 1; i <= n; ++i)if(down[i][0] <= k)ans ++;cout << ans << endl;// for(int i = 1; i <= n; ++ i)// {//     cout << down[i][0] << "  = max\n";// }return 0;
}

codeforces 337D:树形dp[能到最远的一定可以到其他点]相关推荐

  1. CodeForces 543D 树形DP Road Improvement

    题意: 有一颗树,每条边是好边或者是坏边,对于一个节点为x,如果任意一个点到x的路径上的坏边不超过1条,那么这样的方案是合法的,求所有合法的方案数. 对于n个所有可能的x,输出n个答案. 分析: 题解 ...

  2. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

  3. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

  4. 【Codeforces Round #614(div2)】E-Xenon's Attack on the Gangs(树形dp)

    一.题目链接 https://codeforces.com/contest/1293/problem/E 二.题意 给n个结点,n-1条无向边.即一棵树.我们需要给这n-1条边赋上0~n-2不重复的值 ...

  5. 【Christmas Game】【CodeCraft-21 and Codeforces Round #711 (Div. 2)】【Nim-博弈】【树形DP】【拆分树】

    CodeCraft-21 and Codeforces Round #711 (Div. 2) Christmas Game Nim-博弈 树形DP 拆分树 牛客链接 https://ac.nowco ...

  6. Codeforces 1088E Ehab and a component choosing problem(树形DP)

    Codeforces 1088E Ehab and a component choosing problem(树形DP) 题意 给一棵树,要求从中选一些联通分量,使得平均联通分量重量总和最大.如果有多 ...

  7. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

  8. Codeforces 766E Mahmoud and a xor trip(树形DP)

    题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...

  9. CodeForces - 1453E Dog Snacks(树形dp+贪心)

    题目链接:点击查看 题目大意:给出一棵有根树,现在需要选择一个最小的 k 值,可以满足下列的 n 次操作: 起始时位于点 1(根节点) 每一步选择一个未被遍历的节点中,距离最近的,且必须满足此距离小于 ...

最新文章

  1. 一次受限制、无连接的故障处理
  2. private 的访问权限
  3. gulp 常用插件汇总
  4. 节点的度与网络稀疏性
  5. 【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)
  6. 在加拿大读大学被开除了,以后该怎么办?
  7. 数据增强_imgaug图像数据增强必备工具
  8. cpp 编译dll_GCC编译基础
  9. MySQL数据库进阶(API的理解、Python对MySQL的接口)
  10. java关键字与解释_Java关键字及作用解释
  11. 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
  12. 苏宁有货:为“轻创业”而来
  13. IPv6技术精要--第1章 IPv6简介
  14. 【Pre-Finals 2016, Kent Nikaido Contest A】Tetris Puzzle 题解
  15. 技术解密之百度搜索中台低代码的探索与实践
  16. Task 4 用户输入->知识库的查询语句
  17. (20201015 Solved)docker-compose创建网络ERROR: Pool overlaps with other one on this address space
  18. 处理器与大力神杯,夺冠或许并不需要11人
  19. 真王服务器文件,虚惊后的火速报道《真王》迎来历史性更新
  20. 开源飞控初探(六):地面站和SDK

热门文章

  1. GitHub使用指南——建立仓库、建立文件夹、上传图片详细教程
  2. 前沿 | 一文详解自动驾驶激光雷达和摄像头的数据融合方法
  3. 2012年至今,细数深度学习领域这些年取得的经典成果
  4. 【OpenCV 4开发详解】边缘检测原理
  5. Mac下pycharm如何安装tensorflow
  6. Plan9操作系统基本概念
  7. 利用IPSec实现网络安全之三(身份验证和加密数据)
  8. zookeeper安装和使用 windows环境(转)
  9. Firefox 50优化Electrolysis
  10. BlueTooth: 蓝牙基础知识进阶——链路控制操作