zoukankan      html  css  js  c++  java
  • hdu 4707 Pet(DFS水过)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707

    【题目大意】:

    Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示校园中点的个数,D表示宠物鼠不可能在距离D之内,接下来N-1行,输入x,y,表示x与y相邻,(相邻两点之间的距离为1,不相邻为inf),不存在环结构。

    【题解】:

    用邻接表存储树形结构,然后用DFS遍历图

    【code】:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <vector>
     5 
     6 using namespace std;
     7 #define NN 100010
     8 
     9 struct Nod
    10 {
    11     int b,dis;
    12 }tnd;
    13 
    14 vector<Nod> G[NN];
    15 int ans;
    16 
    17 void dfs(int s,int dis)
    18 {
    19     int i;
    20     if(dis<0)   ans++;
    21     for(i=0;i<G[s].size();i++)
    22     {
    23         int id = G[s][i].b;
    24         dfs(id,dis-1);
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     int t;
    31     scanf("%d",&t);
    32     while(t--)
    33     {
    34         int n,d;
    35         scanf("%d%d",&n,&d);
    36         int i;
    37         ans = 0;
    38         for(i=0;i<n;i++)
    39         {
    40             G[i].clear();
    41         }
    42         for(i=0;i<n-1;i++)
    43         {
    44             int a,b;
    45             scanf("%d%d",&a,&b);
    46             tnd.b = b;
    47             tnd.dis = 0;
    48             G[a].push_back(tnd);
    49         }
    50         dfs(0,d);
    51         printf("%d
    ",ans);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    nodejs install
    taobao sass
    Cors 跨域访问API
    多文件上传
    Next
    实用小工具
    下载包含src,tgz,zip的文件
    HTML5文件API
    Bootstrap (导航、标签、面包屑导航)
    Bootstrap 固定定位(Affix)
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3310908.html
Copyright © 2011-2022 走看看