zoukankan      html  css  js  c++  java
  • HDU 4707 Pet(邻接表dfs)

                      Pet




    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
     
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 const int maxn=100005*2+10;
     6 int head[maxn];
     7 int cnt,n,d,total;
     8 int vis[maxn];
     9 
    10 struct s
    11 {
    12     int u,v,w;
    13     int next;
    14 }edge[maxn];
    15 
    16 void add(int u,int v)
    17 {
    18     edge[total].u=u;
    19     edge[total].v=v;
    20     edge[total].next=head[u];
    21     head[u]=total++;
    22 }
    23 
    24 void dfs(int t,int disc)
    25 {
    26     if(disc>d)
    27     return;
    28     vis[t]=1;
    29     cnt++;
    30     if(head[t]==-1)return;
    31     for(int i=head[t];i!=-1;i=edge[i].next)
    32     {
    33         int v=edge[i].v; 
    34         if(!vis[v])
    35         dfs(v,disc+1);
    36     }
    37 }
    38 
    39 int main()
    40 {
    41     int i,t;
    42     scanf("%d",&t);
    43     while(t--)
    44     {
    45         memset(vis,0,sizeof(vis));
    46         memset(head,-1,sizeof(head));
    47         total=1,cnt=0;
    48         scanf("%d%d",&n,&d);
    49         for(i=0;i<n-1;i++)
    50         {
    51             int x,y;
    52             scanf("%d%d",&x,&y);
    53             add(x,y);
    54         }
    55         dfs(0,0);
    56         printf("%d
    ",n-cnt);
    57     }
    58 }
  • 相关阅读:
    Css的transform和transition
    移动端事件
    回流和重绘
    Swift更新至2.2版本 语言变化
    编程中遇到的 问题 总结
    NSNotificationCenter
    iOS中boolean、Boolean、BOOL、bool的区别
    推送的 代码实战写法
    MKNetworkKit的使用
    MKNetworkKit 的介绍
  • 原文地址:https://www.cnblogs.com/homura/p/4703506.html
Copyright © 2011-2022 走看看