hdu 4707 Pet

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4707  

Pet

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

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 100010;
const int INF = 0x3f3f3f3f;
int n, d, ans;
vector<int> G[N];
void init() {
    rep(i, n) {
        G[i].clear();
    }
}
void built() {
    int u, v;
    rep(i, n - 1) {
        scanf("%d %d", &u, &v);
        G[u].pb(v);
        G[v].pb(u);
    }
}
void dfs(int u, int fa, int dep) {
    if(dep > d) ans++;
    int m = sz(G[u]);
    for(int i = 0; i < m; i++) {
        int to = G[u][i];
        if(to != fa) {
            dfs(to, u, dep + 1);
        }
    }
}
void solve() {
    ans = 0;
    init(), built();
    dfs(0, -1, 0);
    printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &d);
        solve();
    }
    return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4792982.html

hdu1010搜索算法 杭州电子科技大学oj平台上的第1010题,是关于搜索的题目,很不错的题 立即下载

相关推荐

真双口RAM---Quartus与Vivado开发环境区别对比

2、写和写冲突:表示两个端口写使能同时有效且写地址相同,此时需要关断一个写,把两个写端口都需要更新的值处理到一个写端口上面,任何的DP RAM 都不支持写和写冲突。以上述直方图统计使用为例,博主在仿真过程中发现,vivado真双口RAM--B端口读写延迟2个clk;1、读和写冲突:如果读和写同时有效,且读和写是同一个地址时,发生RAM读写冲突,此时会把最新的写数据直接赋给读数据,称为写穿通到读。

weixin_54470733的博客 3582

hdu4707

#include<bits/stdc++.h> using namespace std; const int maxn=1e5+7; int T,n,deep,a,b,vis[maxn]; vector<int >g[maxn]; void dfs(int u,int step){ vis[u]=1; if(step>=deep) return; for(int j=0;j<g[u].size();j++){ int v=g[u][j]; if(!vis[.

weixin_50904510的博客 169

LMI线性矩阵不等式之非线性变量处理

​ 如果一个LMI线性矩阵不等式中的某项存在两个矩阵变量相乘,那么称该类情形为非线性矩阵不等式,而要直接求解时很困难的,而且LMI工具箱也无法求解非线性矩阵,所以需要通过变量替换法进行处理。 ​...

Mr.邹的博客 4549

hdu 4707(最短路) Pet

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 303    Accepted Submission(s): 117 Problem Description One day, Lin Ji wake up in t

twobqn123的专栏 706

GD32F303驱动WS2812B灯带:用TIMER+PWM+DMA解放CPU,实现流畅灯效(附完整代码)

本文详细介绍了基于GD32F303的TIMER+PWM+DMA全自动控制方案,用于高效驱动WS2812B灯带。通过硬件外设协同工作,该方案不仅解放了CPU资源,还能实现复杂的灯光效果,如渐变、流水等。文章提供了完整的代码实现和性能优化技巧,适合嵌入式开发者参考。

weixin_42561143的博客 378

光模块组成

时钟恢复电路(Clock Data Recovery,简称CDR)是光模块中的关键组成部分,CDR的作用主要是从接收到的信号中提取出数据序列,并且恢复出与数据序列相对应的时钟时序信号,从而还原接收到的具体信息。因为光信号传输至一定距离的时候,通常是长距离传输,其波形会出现一定程度的失真,接收端接收到的信号是一个个长短不一的脉冲信号,这个时候在接收端,我们就无法得到我们需要的数据。所以,这个时候就需要有信号的再生,信号的再生功能为再放大、再整形和再定时,其中的再定时指的就是CDR时钟数据恢复。

han_better的博客 5719

hdu - 4707 - Pet

题意:一棵N个结点(编号从0开始)的树,根结点为0,求到根结点的距离大于D的结点个数(0 < 测试组数T <= 10,0<N<=100000,0<D<N)。 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 ——>>统计吧。。。 #include <cstdio> #...

weixin_30652491的博客 97

图信号处理中的多尺度分析:图小波变换与图傅里叶变换的对比与应用

本文深入探讨了图信号处理中的多尺度分析方法,重点对比了图小波变换(GWT)与图傅里叶变换(GFT)的技术特点与应用场景。通过分析图的拓扑结构,GWT展现出强大的局部洞察力,适用于异常检测和社区发现;而GFT则擅长全局频率分析,适合趋势分析和信号去噪。文章结合实战案例,为读者提供了方法选型指南和前沿进展,帮助开发者在生物信息学、社交网络分析等领域高效应用这些技术。

weixin_30369041的博客 406

HDU-3912 Turn Right 简单搜索

D - Turn Right Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description This summer, ELT and his classmates went to Beijing for a training

菜鸟成长ing 632

锂电池充放电管理-单片机通用模版

- **①两节锂电池通过电阻分压检测ADC,再根据电压划分电量等级;** - **②充电使用的是锂电池充电IC方案,可通过硬件电阻参数调节充电电流,并可以通过STAT引脚检测充电和满电状态(充电注意会抬高电压);** - **③放电根据ADC_BAT检测电池电量进行等级划分(注意大负载电压骤降瞬间);** - **④充放电过程注意在电量划分等级临界值来回抖动现象;**

a6662580的博客 1488

家里摄像头录像存 NAS,空间爆满了怎么办?一招实现设定容量自动清理实现循环存储!

📷 家用摄像头录像久了会撑爆 NAS 吗?别担心,这篇文章教如何实现自动清理录像,释放 NAS 空间,避免爆盘🧹

XiaoqiangClub的博客 1879

hdu 4707 Pet(图 + 邻接表)

题目连接:4707 - Pet 题目大意:给出n和deep, 然后给出n - 1个关系(关系图为有向无环图,0~n - 1), 然后找出和0的关系大于deep的点有多少个。 解题思路:用邻接表(借助vector)保存关系, dfs搜索, 当d >= 2时返回, 然后遍历一遍所有点, 统计没有搜索过的点。 #include #include #include us

不慌不忙、不急不躁 1119

探索gtwr of mgtwr in python

代码】探索gtwr of GWmodel in python。

m0_46268055的博客 3388

HDU - 4707 Pet (BFS)

题目:HDU - 4707 PetOne 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 p

CrazyForsaken的博客 424

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

HDU 4707 Pet(DFS:树深度相关)

HDU4707 题目描述:给出一个具有N(0 输入:首先是一个T(0 输出:输出距离0号节点距离大于D的节点总数。 分析:从0号节点开始做一次DFS,记录距离大于D的节点数即可。 #include #include using namespace std; const int maxn=100000+100; vector G[maxn]; int sum,n,d;

code 1033

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

HDU 4707 Pet

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1909    Accepted Submission(s): 924 Problem Description One day, Lin Ji wake up

平爷ZXP 734

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Pet--hdu4707

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1809    Accepted Submission(s): 874 Problem Description One day, Lin Ji wake up in the mo

Eric_keke的专栏 366

hdu 4707(Pet)

Pet  (并查集) Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2508    Accepted Submission(s): 1233 Problem Description One day, Lin

渣渣眼里没有水题 306

hdu4707 Pet

思路:简单的DFS一波就好了 #include using namespace std; const int maxn = 1e5+100; vectore[maxn]; int n,d,cnt; int vis[maxn]; void dfs(int root,int v) { if(v>d) return; vis[root]=1; cnt++; for(int i =

围巾的ACM博客 605

【HDU4707】Pet(暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题意:一棵树有n个结点,编号从0到n-1,每条边的距离视为1,求与0号结点距离大于d的结点个数。 思路:题目所给n的范围是 这里需要用到vector的一些方法: vector::push_back()  将元素加入到向量vector::clear()  将向量清空vector::

企鹅学coding~ 901
上一篇: hdu 4593 Robot
下一篇: hdu 4715 Difference Between Primes
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值