刷题总结——book of evil(codefoeces 337D)
题目:
description
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 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.
题解:
很妙的一道树形DP
我们用disd[u][1]表示在u所在子树中且距离节点u受影响的点的距离的最大值···disd[u][2]表示次大值··注意disd[u][1]与disd[u][2]保证两个值来自于两个不同的子树中(后面会知道为什么),disu[u]表示不在u所在子树中的影响点距u的距离的最大值····
disd[u][1]和disd[u][2]我们可以用一遍DFS求得···而disu我们需要在第二遍dfs时根据父节点来计算儿子节点的值···
首先设u的其中一个儿子为v,如果disd[v][1]+1==disd[u][1],那么disu[v]=max(disu[u]+1,disd[u][2]+1),否则disu[v]=max(disu[u]+1,disd[u][1]+1)
由此可以发现disd[u][2]有着给disu[u]的计算做铺垫的作用··三个值计算完后就可以判断了···
代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<ctime> #include<cctype> #include<string> #include<cstring> #include<algorithm> using namespace std; const int N=1e5+5; const int inf=0x3f3f3f3f; int disd[N][3],disu[N]; int first[N],next[N*2],go[N*2],tot,n,m,d; bool jud[N]; inline int R() { char c;int f=0; for(c=getchar();c<'0'||c>'9';c=getchar()); for(;c<='9'&&c>='0';c=getchar()) f=(f<<3)+(f<<1)+c-'0'; return f; } inline void comb(int a,int b) { next[++tot]=first[a],first[a]=tot,go[tot]=b; next[++tot]=first[b],first[b]=tot,go[tot]=a; } inline void dfs1(int u,int fa) { if(jud[u]) disd[u][1]=disu[u]=0; for(int e=first[u];e;e=next[e]) { int v=go[e];if(v==fa) continue; dfs1(v,u); if(disd[v][1]+1>disd[u][1]) { disd[u][2]=disd[u][1];disd[u][1]=disd[v][1]+1; } else disd[u][2]=max(disd[u][2],disd[v][1]+1); } } inline void dfs2(int u,int fa) { for(int e=first[u];e;e=next[e]) { int v=go[e];if(v==fa) continue; if(disd[u][1]==disd[v][1]+1) disu[v]=max(disu[u]+1,disd[u][2]+1); else disu[v]=max(disu[u]+1,disd[u][1]+1); dfs2(v,u); } } int main() { //freopen("a.in","r",stdin); n=R(),m=R(),d=R();int a,b; for(int i=1;i<=n;i++) disd[i][1]=disd[i][2]=disu[i]=-inf; for(int i=1;i<=m;i++) a=R(),jud[a]=true; for(int i=1;i<n;i++) a=R(),b=R(),comb(a,b); dfs1(1,0);dfs2(1,0); int ans=0; for(int i=1;i<=n;i++) { if(i==1) { if(disd[i][1]<=d&&disd[i][2]<=d) ans++; } else if(disd[i][1]<=d&&disu[i]<=d) ans++; } cout<<ans<<"\n"; return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步