双向DFS模板题

在一片沼泽地里,圣骑士马诺发现了古老邪恶之书的踪迹。这片区域由n个从1到n编号的定居点组成,共存在n-1条双向路径连接这些点,使得任意两点间可以通过一条或多条路径到达。邪恶之书的影响范围为d单位距离。已知有m个定居点受到了影响,编号分别为p1, p2,..., pm。马诺需要帮助确定可能藏有邪恶之书的定居点数量。

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

B. 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 mdistinct 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 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.


#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <bitset>
#include <fstream>

typedef unsigned long long ull;
#define mp make_pair
#define pb push_back

const long double eps = 1e-9;
const double pi = acos(-1.0);
const long long inf = 1e18;

using namespace std;

int n, m, d;
int f[ 100100 ], g[ 100100 ];
vector< int > graf[ 100100 ];
bool ok[ 100100 ];

void dfs1( int v, int p )
{
    //cout << v << " " << p << endl;
    f[v] = -1;
    for ( int i = 0; i < graf[v].size(); i++ )
    {
        int next = graf[v][i]; if ( next == p ) continue;
        dfs1( next, v );
        f[v] = max( f[v], ( f[next] == -1 ? -1 : f[next] + 1 ) );
    }
    if ( ok[v] ) f[v] = max( 0, f[v] );
}

void dfs2( int v, int p, int root )
{
    g[v] = root;
    vector< int > sons;
    sons.pb( ( root == -1 ? -1 : root + 	1 ) );
    if ( ok[v] ) sons.pb( 1 );
    for ( int i = 0; i < graf[v].size(); i++ )
    {
        int next = graf[v][i]; if ( next == p ) continue;
        sons.pb( ( f[next] == -1 ? -1 : f[next] + 2 ) );
    }
    sort( sons.begin(), sons.end(), greater<int>() );
    for ( int i = 0; i < graf[v].size(); i++ )
    {
        int next = graf[v][i]; if ( next == p ) continue;
        int nroot = sons[1];
        if ( ( f[next] == -1 ? -1 : f[next] + 2 ) != sons[0] ) nroot = sons[0];
        dfs2( next, v, nroot );
    }
}

int main (int argc, const char * argv[])
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    scanf("%d%d%d", &n, &m, &d);
    for ( int i = 1; i <= m; i++ )
    {
        int a; scanf("%d", &a);
        ok[a] = true;
    }
    for ( int i = 1; i < n; i++ )
    {
        int a, b; scanf("%d%d", &a, &b);
        graf[a].pb(b);
        graf[b].pb(a);
    }
    dfs1( 1, -1 );
    dfs2( 1, -1, -1 );
    int ans = 0;
    for ( int i = 1; i <= n; i++ ) if ( max( f[i], g[i] ) <= d ) ans++;
    //for ( int i = 1; i <= n; i++ ) cout << i << " " << f[i] << " " << g[i] << endl;
    cout << ans << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值