Pet

                                                         Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                         Total Submission(s): 1909    Accepted Submission(s): 924


                                                                                                                             链接:

pid=4707">Click Me !

Problem Description
One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three days. Nothing but cockroaches
was caught. He got the map of the school and foundthat there is no cyclic path and every location in the school can be reached from his room. The trap’s manual mention that the pet will always come back if it still in somewhere nearer than distance D. Your
task is to help Lin Ji to find out how many possible locations the hamster may found given the map of the school. Assume that the hamster is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.
Input
The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations
in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.
Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
Sample Input
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
Sample Output
2
Source

题意:

给定N个点,标号为0~N-1,还有N-1条边,数据保证N-1条边不成环,也就是说,输入的节点为N的一棵树。根节点为0,要你求深度大于d的节点的数目。

分析:

从根节点0開始,BFS其全部的子节点。统计深度小于等于d的节点的数目cnt。那么答案就是N-cnt。水题~

#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
typedef long long LL;
const int MAXN = 1e6 + 50;
struct Node
{
vector<int> son;
} nodes[MAXN];
bool vis[MAXN];
void add_edge(int a, int b)
{
nodes[a].son.push_back(b);
} struct Fuck
{
int pos, step;
Fuck() {}
Fuck(int _p, int _s) : pos(_p), step(_s) {}
};
queue<Fuck> Que;
int Dis, N;
int BFS()
{
memset(vis,false,sizeof(vis));
int cnt = 0;
Fuck Now(0, 0);
Que.push(Now);
vis[0] = true;
while(!Que.empty())
{
Now = Que.front();
Que.pop();
int nowp = Now.pos, nows = Now.step;
if(nows == Dis) continue;
for(int i = 0; i < nodes[nowp].son.size(); i++)
{
int sonp = nodes[nowp].son[i];
if(vis[sonp]) continue;
Que.push(Fuck(sonp, nows + 1));
vis[sonp] = true;
cnt ++;
}
}
return N - cnt - 1;
}
int main()
{
// FIN;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &Dis);
for(int i = 0; i < N; i++)
nodes[i].son.clear();
for(int i = 1; i <= N - 1; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b);
add_edge(b, a);
}
int ans = BFS();
printf("%d\n", ans);
}
return 0;
}

hdu 4707 Pet【BFS求树的深度】的更多相关文章

  1. 小小c#算法题 - 10 - 求树的深度

    树型结构是一类重要的非线性数据结构,树是以分支关系定义的层次结构,是n(n>=0)个结点的有限集.关于树的基本概念不再作过多陈述,相信大家都有了解,如有遗忘,可翻书或去其他网页浏览以温习. 树中 ...

  2. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  3. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  4. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  5. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  6. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  7. 牛客小白月赛6C-桃花(DFS/BFS求树的直径)

    链接:https://www.nowcoder.com/acm/contest/136/C 来源:牛客网 桃花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言 ...

  8. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  9. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

随机推荐

  1. RocketMQ学习笔记(9)----RocketMQ的Producer 顺序消息

    1. 顺序消息原理图 2. 什么是顺序消息? 消费消息的顺序要求同发送消息的顺序一致,在RocketMQ中,主要指的是局部顺序,即一类消息为满足顺序性,必须Producer单线程顺序发送,并且发送给到 ...

  2. 关于目标检测 Object detection

    NO1.目标检测 (分类+定位) 目标检测(Object Detection)是图像分类的延伸,除了分类任务,还要给定多个检测目标的坐标位置.      NO2.目标检测的发展 R-CNN是最早基于C ...

  3. Matlab--从入门到精通(chapter2 matlab 基础知识)

    Chapter2 Matlab 基础知识 1.基本数学运算符号 注:矩阵的右除是一般意义的除法,但是左除具有对称意义,即A./B=B.\A 2. 命令行中的常用标点 3.常见的操作命令 4.输出数据显 ...

  4. 异步线程编程,线程池,线程组,后面涉及ThreadLocal在理解

    join模拟订单 package com.future.demo.future; /** * * * @author Administrator * */ public class NormalThr ...

  5. PostGIS解析Geometry几何对象

    一.Geometry转WKT select st_astext(geom) where tableName; 二.PostGIS常用函数 wkt转geometry st_geomfromtext(wk ...

  6. GIT配置多用户

    在公司工作的时候有时候想提交一点代码到github上,然后一台电脑上就需要配置两个账号分别访问github和公司的gitlab 1. 分别生成两个key 为什么要生成两个key的原因我也不清楚,望路过 ...

  7. linux指令--用户和工作组管理

    >>前言    Linux是一个多用户.多任务的操作系统,Linux系统的初衷之一就是满足多用户同时工作的需求,因此,linux需要具备很好的安全性,需要对用户进行管理,用户又分几种,管理 ...

  8. python 中i++、逻辑表达式

    参考链接:https://www.cnblogs.com/yupeng/p/3345946.html i++运算符 python中没有类似i++之类实现+1的运算符,但是有++i,+-i.之类的,他们 ...

  9. quick-cocos2d-x游戏开发【2】——项目结构分析、创建新场景

    创建完一个新项目之后,我们能够简单的看一看这个项目的文件组成,有这么一个文件层次结构 几个proj.*目录就不用说了,是相应的平台的解决方式,res专门存放我们的游戏资源.scripts存放我们的lu ...

  10. [MST] Create an Entry Form to Add Models to the State Tree

    It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...