• hdu 4707 Pet


    Pet

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1054    Accepted Submission(s): 528


    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
     
    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818 
     
     1 //937MS    4188K    915 B    G++    
     2 /*
     3 
     4     有点暴力...水题。好像没用到什么算法 
     5 
     6 */
     7 #include<iostream>
     8 #include<vector>
     9 #include<queue>
    10 using namespace std;
    11 struct node{
    12     int id,s;
    13     node(int x,int y){
    14         id=x;s=y;
    15     }
    16 };
    17 vector<node>V[100005];
    18 int deal(int n,int d)
    19 {
    20     queue<node>Q;
    21     int s=0;
    22     Q.push(node(0,0));
    23     while(!Q.empty()){
    24         node t=Q.front();
    25         Q.pop();
    26         //printf("*%d %d
    ",t.id,t.s);
    27         if(t.s>d) s++;
    28         for(int i=0;i<V[t.id].size();i++){
    29             V[t.id][i].s=t.s+1;
    30             Q.push(V[t.id][i]); 
    31         }
    32     }
    33     return s;
    34 }
    35 int main(void)
    36 {
    37     int t,n,d;
    38     int x,y;
    39     scanf("%d",&t);
    40     while(t--)
    41     {
    42         scanf("%d%d",&n,&d);
    43         for(int i=0;i<n;i++){
    44             V[i].clear();
    45         }
    46         for(int i=1;i<n;i++){
    47             scanf("%d%d",&x,&y);
    48             V[x].push_back(node(y,0));
    49         }
    50         printf("%d
    ",deal(n,d));
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
    PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
    PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
    字典树trie的学习与练习题
    POJ 1273 Drainage Ditches(网络流dinic算法模板)
    单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
    Hungary(匈牙利算法)——二分图最大匹配
    LCIS 最长公共上升子序列
    POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)
    Codeforces 494D Upgrading Array
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3643844.html
Copyright © 2020-2023  润新知