hdu 4707 Pet【BFS求树的深度】
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) pid=4707">Click Me !
Total Submission(s): 1909 Accepted Submission(s): 924
链接:
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.
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.
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
2
题意:
给定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求树的深度】的更多相关文章
- 小小c#算法题 - 10 - 求树的深度
树型结构是一类重要的非线性数据结构,树是以分支关系定义的层次结构,是n(n>=0)个结点的有限集.关于树的基本概念不再作过多陈述,相信大家都有了解,如有遗忘,可翻书或去其他网页浏览以温习. 树中 ...
- 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 ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- HDU4612+Tarjan缩点+BFS求树的直径
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...
- 牛客小白月赛6C-桃花(DFS/BFS求树的直径)
链接:https://www.nowcoder.com/acm/contest/136/C 来源:牛客网 桃花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言 ...
- 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 ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
随机推荐
- 我的Java历程_写出这个数
lzJava基础进行中,今天偶然间看到的一个题目: 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字.如下代码: import java.util.*;public class Ma ...
- 优动漫PAINT-紫藤花画法
本教程分享一篇使用优动漫PAINT绘制一树梦幻的紫藤萝花教程,原文转载自优动漫官网. 小清新紫藤萝教程,就到这里啦!有兴趣的可以尝试画一画哦,软件下载:www.dongmansoft.com/xiaz ...
- NetworkX-根据权重画图
load_data = sio.loadmat(load_path) #阈值处理 mat=np.array(load_data['R']) mat[mat<0]=0 mat[mat<0.4 ...
- 之前的大数相加存在bug,这个ac通过了
#include <iostream> #include <string> /*project:两个大整数相加 **@author:浅滩 **data:2019.05.15 * ...
- socket网络编程登录实现及多客户端和服务端的数据交互
一.TCP/IP 客户端 package com.demo.entity; import java.io.Serializable; public class UserInfo implements ...
- Servicification
Servicification Summary The Chromium codebase now supports many platforms and use cases. In response ...
- 简洁的MVC思想框架——Nancy(环境配置与Get操作)
Nancy官网——https://github.com/NancyFx/Nancy 概述:Nancy是一个开源的Web轻型框架内核符合MVC思想,有开发方便,路由简单的特点,而且功能齐全 起步:Hel ...
- NodeJS学习笔记 (1)资源压缩-zlib(ok)
原文: https://github.com/chyingp/nodejs-learning-guide/blob/master/README.md 自己的跟进学习: 概览 做过web性能优化的同学, ...
- input标签type为number时,输入小数,在Firefox浏览器上输入框标红的问题
问题一:firefox 下 默认情况 <input type="number"> 只允许输入整数,输入小数时会报错,输入框被标红 这时候可以添加参数 step=&q ...
- java 获取config 配置文件
static ResourceBundle PropertiesUtil = ResourceBundle.getBundle("config"); public static S ...