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

题意:判断距离大于D的点有多少个。

思路: 邻接表建图,dfs每一个点,记录步数。

 #include <stdio.h>
#include <string.h>
const int N=;
int vis[N],dis[N],head[N],cnt,step;
struct node
{
int u;
int v;
int next;
} edge[N];
void add(int u,int v)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
};
void dfs(int u,int step)
{
if (vis[u])
return ;
dis[u] = step;
vis[u] = ;
for (int j = head[u]; j != -; j = edge[j].next)
{
int v = edge[j].v;
if (!vis[v])
{
dfs(v,step+);
}
}
return ;
}
void init()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
cnt = ; }
int main()
{
int t,n,d,u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&d);
init();
for (int i = ; i < n; i ++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);
int ans = ;
for (int i = ; i < n; i ++)
{
if (dis[i] > d)
ans++;
}
printf("%d\n",ans);
}
return ;
}

Pet(dfs)的更多相关文章

  1. HDU4707:Pet(DFS)

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  2. Pet(dfs+vector)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  4. hdu 4707 Pet(DFS &amp;&amp; 邻接表)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. hdu 4707 Pet(DFS水过)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...

  6. hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题

    题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...

  7. HDU 4707 DFS

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  8. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  9. Pet

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

随机推荐

  1. java网络

    title: java 网络 date: 2017年3月11日11:14:52 1. 复杂的东西就把他封装成对象 概述:(网络就是找到别人) 找到对方的机器,(找到对方的ip地址) 每个机器中有很多进 ...

  2. Forum/viewthread.php代码备份

    <!--{eval $lzthread = DB::fetch_all("SELECT `tid`,`subject` FROM ".DB::table('forum_thr ...

  3. ES6 中set的用法

  4. scss基础

    1.变量$ 全局 局部 .div{ $color:yellow; } 2.类似函数@mixin border-radius($radius) { }引用:@include border-radius( ...

  5. VM虚拟机NAT链接外网

    1.vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=localhost.localdomainGATEWAY=192.168.110.2 2.vi /e ...

  6. 16.2 【C# 5】调用者信息特性

    16.2.1 基本行为 .NET 4.5引入了三个新特性(attribute),即 CallerFilePathAttribute . CallerLineNumber- Attribute 和 Ca ...

  7. swift--调用系统单例实现打电话

    //自动打开拨号页面并自动拨打电话 var phone="15974462468"; UIApplication.sharedApplication().openURL(NSURL ...

  8. Keil-MDK编译完成后代码大小

    Code 代表执行的代码,程序中所有的函数都位于此处. RO-data 代表只读数据,程序中所定义的全局常量数据和字符串都位于此处. RW-data 代表已初始化的读写数据,程序中定义并且初始化的全局 ...

  9. BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花

    [题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

  10. eclipse 快捷键及使用技巧

    一.程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:Installed JREs 默 ...