一道关于 DFS 的题目, 关键在于距离 K, 将服务器的节点作为根节点进行DFS
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1000 + 10;
vector<int> tree[maxn], nodes[maxn];
int fa[maxn];
int s, k, n;
bool covered[maxn];
void dfs(int child, int father, int depth){
fa[child] = father;
int newnode = tree[child].size();
if (newnode&&depth>k)
{
nodes[depth].push_back(child);
}
for (int i = 0; i < newnode;i++)
{
int newchild = tree[child][newnode];
if (newchild != father) dfs(newchild, child, depth + 1);
}
}
void dfs2(int serve, int father, int depth){
covered[serve] = true;
int nsurround = tree[serve].size();
for (int i = 0; i < nsurround; i++){
int v = tree[serve][i];
if (v != father&&depth < k) dfs2(v, serve, depth + 1);
}
}
int solve(){
int ans = 0;
memset(covered, 0, sizeof(covered));
for (int d = n - 1; d > k;d--)
{
for (int i = 0; i<nodes[d].size();i++)
{
int u = nodes[d][i];
if (covered[u]) continue;
int v = u;
for (int j = 0; j < k; j++) v = fa[v];
dfs2(v, -1, 0);
ans++;
}
}
return ans;
}
int main(){
int T;
cin >> T;
while (T--){
cin >> n >> s >> k;
for (int i = 0; i <= n;i++)
{
tree[i].clear();
nodes[i].clear();
}
for (int i = 0; i < n-1;i++)
{
int a, b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
dfs(s, -1, 0);
cout << solve();
}
return 0;
}
分享到:
相关推荐
Seoul Bike Demand.ipynb
seoul256.vim, 基于首尔颜色的低对比度Vim配色方案 " _____ _ ___ ___ ___"" | __|___ ___ _ _| |_ | _| _|"" |__ | -_|. | | | | _|_ |. |"" |_____|___|___|___|_|_
seoul.zip
介绍 unity3d shader 的ppt IntroShaders_Seoul.pptx
Smart Cities Seoul a case study.pdf
seoul256-emacs:vim的seoul256配色方案的Emacs主题端口
python库。 资源全名:seoul-0.2.0.tar.gz
IT IS ABOUT APPLICATION FOR KOREAN UNIVERSITY.
42_seoul:42_seoul_task
关于 MJT MJT是多P/N结技术,它的驱动电压要比常规的LED的驱动电压高。市场上的高压LED实际上是许多LED芯片串并联而成,电路设计复杂,MJT则采用多junction 技术集成在同一芯片中,从而得到不同的电压值。...
韩国一流大学的校园,让我们一起去欣赏校园美景。原始数码照片,可下载后制作ppt文件时参考,还可为取韩国留学的人提供资料
Seoul Commtech选用u-blox GPS技术开发功能丰富的先进多媒体PND.pdf
Seoul42会员信息$ git clone https://github.com/innovationacademy-kr/42seoul-info$ cd 42seoul-info$ npm install 在42 intra上注册,然后将重定向URI设置为http://localhost:4200/login/42/return 。 将.env....
42seoul
AWS CloudFormation 文件用于创建VPC+TGW的基础架构和模拟本地IDC环境的脚本
首尔地铁首尔大学大学新生研讨会项目。 (2021年Spring)。
崇高文本的灵魂配色方案轻松的眼睛崇高的主题。 混合 , 和我自己的意见。截屏变更日志0.1.0 - 第一个版本0.1.1 - 更好的 ERB 颜色
2016年,Blue Coat 发布了针对Sony Pictures Entertainment的网络攻击事件的APT报告,攻击者被命名为DarkSeoul或Silent Chollima