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 ...
随机推荐
- javascript实现自动添加文本框功能
转自:http://www.cnblogs.com/damonlan/archive/2011/08/03/2126046.html 昨天,我们公司的网络小组决定为公司做一个内部的网站,主要是为员工比 ...
- POJ-3159 Candies 最短路应用(差分约束)
题目链接:https://cn.vjudge.net/problem/POJ-3159 题意 给出一组不等式 求第一个变量和最后一个变量可能的最大差值 数据保证有解 思路 一个不等式a-b<=c ...
- Springboot错误问题总结
进行springboot+swagger2测试的时候,启动项目发现出现这个问题 把所有的类,配置类都注释掉,不管用,百度搜索之后发现一个解决办法, 半信半疑的加到启动类SpringBootApplic ...
- 使用InstelliJ IDEA创建Web应用程序
环境版本 Windows 8.1IDE:InstelliJ IDEA 13 Spring:Spring 4.1.1 & Spring MVC 4.1.1 WebLogic 10.3 ...
- const int *a与int *const a,const int *const a的区别
来源:https://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰 ...
- 【codeforces 429D】Tricky Function
[题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...
- HDU 3046 Pleasant sheep and big big wolf
Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ...
- Vim 删除不包含指定字符串的行及统计匹配个数
Vim 删除不包含指定字符串的行及统计匹配个数 转载▼ Help :g/pattern/d 是找到pattern, 删之 :v/pattern/d 是找到非pattern, 删之 :%s/xx ...
- Pycharm在创建py文件时,如何自动添加默认文件头注释?
PyCharm是一款很好用的编写Python工程的IDE,用PyCharm创建一个Python文件或者向工程添加一个.py文件时,为了更好的使所编写的代码在各操作环境更好的运行,我们往往需要在.py文 ...
- POJ 3122 Pie 二分答案
题意:给你n个派,每个派都是高为一的圆柱体,把它等分成f份,每份的最大体积是多少. 思路: 明显的二分答案题-- 注意π的取值- 3.14159265359 这样才能AC,,, //By Sirius ...