zoukankan      html  css  js  c++  java
  • HDU 4707 Pet (水题)

    Pet

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


    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
     

    很水。

    只要bfs一次,求出从0出发的深度。

    判断深度>D的个数

     1 /* *******************************************
     2 Author       : kuangbin
     3 Created Time : 2013年09月08日 星期日 12时00分01秒
     4 File Name    : 1009.cpp
     5 ******************************************* */
     6 
     7 #include <stdio.h>
     8 #include <algorithm>
     9 #include <iostream>
    10 #include <string.h>
    11 #include <vector>
    12 #include <queue>
    13 #include <set>
    14 #include <map>
    15 #include <string>
    16 #include <math.h>
    17 #include <stdlib.h>
    18 #include <time.h>
    19 using namespace std;
    20 
    21 const int MAXN = 1000010;
    22 vector<int>vec[MAXN];
    23 int dep[MAXN];
    24 int pre[MAXN];
    25 void bfs(int s)
    26 {
    27     memset(dep,-1,sizeof(dep));
    28     dep[s] = 0;
    29     queue<int>q;
    30     q.push(s);
    31     while(!q.empty())
    32     {
    33         int u = q.front();
    34         q.pop();
    35         int sz = vec[u].size();
    36         for(int i = 0;i < sz;i++)
    37         {
    38             int v = vec[u][i];
    39             if(dep[v] != -1)continue;
    40             dep[v] = dep[u] + 1;
    41             pre[v] = u;
    42             q.push(v);
    43         }
    44     }
    45 }
    46 int main()
    47 {
    48     
    49     int T;
    50     int n;
    51     int D;
    52     scanf("%d",&T);
    53     while(T--)
    54     {
    55         scanf("%d%d",&n,&D);
    56         int u,v;
    57         for(int i = 0;i < n;i++)
    58             vec[i].clear();
    59         for(int i = 1;i < n;i++)
    60         {
    61             scanf("%d%d",&u,&v);
    62             vec[u].push_back(v);
    63             vec[v].push_back(u);
    64         }
    65         bfs(0);
    66         int ans = 0;
    67         for(int i = 0;i < n;i++)
    68             if(dep[i] > D)
    69                 ans++;
    70         cout<<ans<<endl;
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    Ubuntu下 实现Linux与Windows的互相复制与粘贴
    bzoj2426
    bzoj1835
    bzoj1197
    bzoj1049
    bzoj2893
    bzoj1820
    bzoj1819
    bzoj1455
    bzoj3689
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3308685.html
Copyright © 2011-2022 走看看