Input
Your program is to read the input from standard input. The input consists of T test cases. The number
of test cases (T) is given in the first line of the input. The first line of each test case contains an integer
n (3 ≤ n ≤ 1, 000) which is the number of nodes of the tree network. The next line contains two
integers s (1 ≤ s ≤ n) and k (k ≥ 1) where s is the VOD server and k is the distance value for ensuring
the quality of service. In the following n − 1 lines, each line contains a pair of nodes which represent
an edge of the tree network.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain an integer that is the minimum number of the needed replicas.
Sample Input
2

14

12 2
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11
14
3 4
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11
Sample Output
1
0

vector建立边关系,如何判断是叶子节点,从任一点dfs范围搜索

#include <cstdio>
#include <vector>using namespace std;
const int MAX = 1000 + 5;
int cover[MAX], fa[MAX], n, s, k;
vector<int> gr[MAX], nodes[MAX];//gr是边,nodes是该深度的叶子节点void dfs(int u, int f, int d);void dfs2(int u, int f, int d);int main() {int T;scanf("%d", &T);for (int base = 0; base < T; ++base) {for (int i = 0; i < MAX; ++i) {cover[i] = 0, fa[i] = 0, gr[i].clear(), nodes[i].clear();}scanf("%d%d%d", &n, &s, &k);  //有回车可以无视for (int i = 0; i < n - 1; ++i) {int u, v;scanf("%d%d", &u, &v);  //建立双边关系
            gr[u].push_back(v);gr[v].push_back(u);}dfs(s, -1, 0);int ans = 0;for (int h = n - 1; h > k; --h) { //从最深开始for (int j = 0; j < nodes[h].size(); ++j) {int v = nodes[h][j];if (cover[v])continue;for (int i = 0; i < k; ++i) {v = fa[v];}dfs2(v, -1, 0); //从该节点dfsans++;}}printf("%d\n", ans);}
}void dfs2(int u, int f, int d) {cover[u] = 1;for (int i = 0; i < gr[u].size(); ++i) {int v = gr[u][i];if (v != f and d < k)dfs2(v, u, d + 1);}
}void dfs(int u, int f, int d) {fa[u] = f;  //建立父节点int nc = gr[u].size();if (nc == 1 and d > k)nodes[d].push_back(u); //是叶子节点存进去for (int i = 0; i < nc; ++i) {int v = gr[u][i];if (v != f)dfs(v, u, d + 1); //不是叶子节点继续地柜
    }
}

转载于:https://www.cnblogs.com/wangsong/p/7587603.html

UVA - 1267 Network相关推荐

  1. UVA - 315 Network(tarjan求割点)

    题目链接:点击查看 题目大意:给出一个由n台电脑互相连接而成的网络系统,其中有一些电脑如果一旦损坏,则会造成整个网络出现缺口,导致某些地方无法互相连通,我们称这种电脑为关键点,题目需要求出有多少个关键 ...

  2. OI 刷题记录——每周更新

    每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...

  3. 解题报告:【kuangbin带你飞】专题九 连通图

    目录 A.POJ 1236 Network of Schools(有向图缩点) B.UVA 315 Network(找割点) C.UVA 796 Critical Links(桥) D.POJ 369 ...

  4. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  5. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  6. [kuangbin带你飞]专题九 连通图

    A. POJ 1236  Network of Schoolst 题意:有n个学校,每个学校都可以给它名单上的学校发送软件.然后现在问你至少需要给多少个学校发送软件. 思路:求出强连通分量的个数,每一 ...

  7. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  8. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  9. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

最新文章

  1. CSS 实例之打开大门
  2. linux sftp没有读写权限,sftp服务器上的Linux权限问题
  3. 异构计算架构师眼中的AI算法(object detection)
  4. Linux命令中的rpm安装命令
  5. php短信接口源码,比较简单,但也实用
  6. 微信防撤回python代码_Python实现微信防撤回
  7. QT的QSplashScreen类的使用
  8. Windows 下的批处理脚本基础——网络相关命令(用户操作命令、用户组操作命令)
  9. c++ stl string char* 向 string 转换的问题
  10. C++:在堆上创建对象,还是在栈上?
  11. kuka机器人齿轮箱油_库卡KUKA机器人保养润滑油00-144-898
  12. Minitab数据分析时,无法进行统计变量的选择
  13. EXCEL VBA批量下载URL链接地址图片、URL链接地址图片转为图片
  14. 做一个靠谱的软件测试工程师:进行有效地沟通
  15. css中滑轮中是如何做的,横向的网页如何实现鼠标滑轮横向移动?_html/css_WEB-ITnose...
  16. ui设计发展到底好不好?为什么越来越多的人开始学习UI设计?
  17. 计算机一级表格分类汇总怎么弄,多张word表格分类汇总 word表格分类汇总
  18. 基于java婚纱影楼服务管理计算机毕业设计源码+系统+lw文档+部署
  19. 一个 SAP 开发工程师的成长史 - 关于信息素养和数字化资产的思考
  20. 罗姆BD9P308MUF-C----- Nano Pulse Control™ 车载用 3.5V~40V输入 3.0A 单通道降压DC-DC转换器

热门文章

  1. 如何备份服务器日志到其他服务器_云计算迁移别忘了备份物理服务器和数据
  2. SQLi LABS Less 16 布尔盲注
  3. SQL server 增删改查语句
  4. Tensorflow的快速安装(张量图例)
  5. opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移...
  6. Collection源码分析(二):LinkedList源码分析
  7. 2PC AND 3PC
  8. 各操作系统各文件系统支持的最大文件的大小
  9. 一个汉字在数据库占几个字节
  10. 2748: [HAOI2012]音量调节