CodeForces 337D Book of Evil(双向dfs)

在一个由n个定居点构成的沼泽区域中,通过已知受邪恶之书影响的m个定居点及其最大影响距离d,寻找可能藏匿邪恶之书的具体位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 birepresenting 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.

Example
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.



题解:

题意:

这题主要是理解题意比较难,给你n个节点,m个已经被感染了的节点,一个范围d,和n-1个边形成一颗树,让你求存在多少个点使得所有被感染的点与它的距离不大于d

思路:

将感染点中相距最远的两个点作为边界,当前点与那两个边界点的距离都小于d那么其他被感染的点也符合条件了,先从任意一个点dfs一遍树,求出距离最远的那个被感染了的点作为tar1,然后再从tar1 dfs一遍,求出最远的被感染的点作为tar2并将所有点与tar1的距离存在d1数组中,然后从tar2出发dfs一遍树求出所有点与tar2的距离存在d2数组中,然后遍历所有节点并计数符合d1[i]和d2[i]都小于d的点

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
int d1[100005];
int d2[100005];
int a[100005];
vector<int>p[100005];
int vis[100005];
void dfs(int index,int deep,int b[])
{
    b[index]=deep;
    for(int i=0;i<p[index].size();i++)
    {
        if(vis[p[index][i]])
            continue;
        vis[p[index][i]]=1;
        dfs(p[index][i],deep+1,b);
    }
}
int main()
{
    int n,m,d,x,y,i,j,num,tar1,tar2;
    scanf("%d%d%d",&n,&m,&d);
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        p[x].push_back(y);
        p[y].push_back(x);
    }
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    dfs(1,0,d1);
    tar1=a[0];
    for(i=1;i<m;i++)
    {
        if(d1[tar1]<d1[a[i]])
            tar1=a[i];
    }
    memset(vis,0,sizeof(vis));
    vis[tar1]=1;
    dfs(tar1,0,d1);
    tar2=a[0];
    for(i=1;i<m;i++)
    {
        if(d1[tar2]<d1[a[i]])
            tar2=a[i];
    }
    memset(vis,0,sizeof(vis));
    vis[tar2]=1;
    dfs(tar2,0,d2);
    num=0;
    for(i=1;i<=n;i++)
    {
        if(d1[i]<=d&&d2[i]<=d)
            num++;
    }
    printf("%d\n",num);
    return 0;
}



### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值