hdu 4707 Pet【BFS求树的深度】

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

field=problem&key=2013+ACM%2FICPC+Asia+Regional+Online+%A1%AA%A1%AA+Warmup&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">2013 ACM/ICPC Asia Regional Online
—— Warmup

题意:

给定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;
}
时间: 2024-08-04 19:42:01

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

soj 1034 Forest_求树的深度和宽度

题目链接 题意:给你n个节点,m条边,每条边是有向的,这颗树不能有自环,问这颗树的深度和宽度 思路: 不合法情况 1,入度大于1,即存在两条指向同一顶点的边 2,一条入点和出点都相同的边 3,一条变得入点和出点深度已知,但不符合出点的深度是入点的深度加1 4,点入深度未知但出点深度已知 5,遍历完以后,有顶点未遍历,说明有多个根 树的宽度是指,同一层最多有多少个节点 #include <iostream> #include<cstdio> #include<cstring&g

广度优先搜索求树的深度

#include<iostream> #include<vector> #include<stack> #include<string> #include<queue> #include<algorithm> #include<numeric> using namespace std; class node{ public: int val; node* left; node* right; node():val(0),l

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

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E

Codeforces1294F. Three Paths on a Tree(两次BFS求树的直径)

题意: 给一棵树,找到三个顶点,使三个顶点两两之间路径的并集最大 思路: 必定会有一组最优解,使得 a,b是树直径上的端点. 证明: 假设某个答案取连接点x.x最远的树到达的点是s,根据树的直径算法,s是树的某个直径a的端点.假设x的最远和第二远的点组成的链是b,b就会和a有一段公共部分.我们取a和b相交部分距离s最远的那个点y.那么取这个链上点y的答案一定比x更优 用两次BFS可以求出直径的两个端点,在这个过程中还能顺便求出一个端点到树上每一点的距离.之后再用一次BFS求得另一个端点到树上每一

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is

求树的深度(通解)

树的深度求解 递归方法求深度 寻找递归的条件: 解题思路: (1)如果树只有一个结点,则树的深度为1.       (2)如果根结点只有左子树或者只有右子树,那么这棵树的深度是其根结点的左子树的深度再加1.   (3)如果根结点既有左子树又有右子树,那么这棵树的深度是左子树深度与右子树深度的最大值再加1. int GetTreeDepth(BiTree &T) { if (!T) { return 0; } int left = GetTreeDepth(BiTree T->lchild);

HDU 4607 Park visit (求树的直径)

解题思路: 通过两次DFS求树的直径,第一次以任意点作为起点,找到距离该点距离最远的点,则可以证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,如果K <= D + 1则可以沿着直径走,距离为K  -  1, 如果K >= D + 1,则需要走直径旁边的分支,每访问一个点距离为2(从直径到这个点,再返回到直径上). #include <iostream> #include <cstring> #include <cstd

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

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 *

hdu 4707 Pet(dfs,bfs)

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1548    Accepted Submission(s): 733 Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He sear