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 ...
随机推荐
- [洛谷P1352][codevs1380]没有上司的舞会
题目大意:某大学有N个职员,编号为1~N.他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但如果某个职员的上司来参加舞 ...
- 05003_Linux的基本命令
1.目录结构 Linux的目录结构:Linux各目录及每个目录的详细介绍 链接:Linux各目录及每个目录的详细介绍 密码:84ab 2.LInux的基本命令 (1)目录切换命令 ①root是超级管理 ...
- POJ 1975 Median Weight Bead
Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...
- [Angular + TsLint] Disable directive selector tslint error
@Directive({ // tslint:disable-next-line:directive-selector selector: '[scrollable]' })
- libevent的使用(socket)
这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序堵塞在socket I/O操作上造成程序性能的下降,须要使用异步编程,即程序准备好读写的 ...
- zzulioj--1824--BOOM(模拟水)
1824: BOOM Time Limit: 1 Sec Memory Limit: 128 MB Submit: 25 Solved: 5 SubmitStatusWeb Board Descr ...
- nyoj--1009--So Easy[Ⅰ](数学)
So Easy[Ⅰ] 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给出任意一个三角形的三个边a,b,c. 要求:求出这个三角形的外接圆半径. 输入 输入数据有多组. ...
- CAP定理在分布式系统设计中的最新应用
本文翻译自国外InfoQ和计算机杂志上一篇2012年旧文,本文就有关数据同步进行了讨论,特别关注业务事务的不变性与一致性如何在分布式系统中巧妙保证,探讨了长时间运行的事务的补偿机制.这些对分布式系统设 ...
- 11.IntelliJ IDEA详细配置和使用教程(适用于Java开发人员)
转自:https://blog.csdn.net/chssheng2007/article/details/79638076 前言 正所谓工欲善其事必先利其器,对开发人员而言若想提高编码效率,一款高效 ...