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 ;
}
05-23 15:08