D. Book of Evil
1.P3478 [POI2008] STA-Station2.E1. Weights Division (easy version)3.P5007 DDOSvoid 的疑惑4.P2168 [NOI2015] 荷马史诗5.P8306 【模板】字典树6.P1481 魔族密码7.P3128 [USACO15DEC] Max Flow P8.P3258 [JLOI2014] 松鼠的新家9.P5536 【XR-3】核心城市10.P5836 [USACO19DEC] Milk Visits S11.P3384 【模板】重链剖分/树链剖分12.P3038 [USACO11DEC] Grass Planting G13.P4551 最长异或路径14.F. Alex's whims15.J. 上学16.Game on Tree17.E. We Need More Bosses18.A. Party19.D. Strong Vertices20.B. Omkar and Heavenly Tree21.B. Mahmoud and Ehab and the bipartiteness22.P8655 [蓝桥杯 2017 国 B] 发现环23.P10298 [CCC 2024 S4] Painting Roads24.F. Gardening Friends25.D. Playoff Tournament26.E. Lomsat gelral27.F. Minimum Maximum Distance
28.D. Book of Evil
29.P2195 HXY造公园30.P3304 [SDOI2013] 直径31.D. Sasha and a Walk in the City32.D. Vitaly and Cycle33.P10838 『FLA - I』庭中有奇树34.F - Perfect Matching on a Tree题解
题目要求有多少个点,其到标记点的最远距离不超过
看到这个我们不难想到树的直径:设直径端点
所以,我们把标记点看成叶子节点,并找出相距最远的一对标记点
(如果有非标记点,在叶子节点的“外侧”,该性质依然成立)
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> G[100004];
bool mark[100005]={0};
int maxh=0;
int node1;
int node2;
void dfs(int now,int fa,int height)
{
if(height>maxh&&mark[now])
{
maxh=height;
node1=now;
}
for(auto next:G[now])
{
if(next==fa) continue;
dfs(next,now,height+1);
}
}
int ans[100005]={0};
int n,m,d;
void dfs1(int now,int fa,int height)
{
if(height<=d) ans[now]++;
if(height>maxh&&mark[now])
{
maxh=height;
node2=now;
}
for(auto next:G[now])
{
if(next==fa) continue;
dfs1(next,now,height+1);
}
}
void dfs2(int now,int fa,int height)
{
if(height<=d) ans[now]++;
for(auto next:G[now])
{
if(next==fa) continue;
dfs2(next,now,height+1);
}
}
void solve()
{
cin>>n>>m>>d;
int start;
for(int i=1;i<=m;i++)
{
int x;
cin>>x;
mark[x]=1;
start=x;
}
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
node1=start;
dfs(start,start,0);
maxh=0;
node2=start;
dfs1(node1,node1,0);
dfs2(node2,node2,0);
int sum=0;
for(int i=1;i<=n;i++) sum+=(ans[i]==2);
cout<<sum<<'\n';
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~