Pet
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int n,d;
vector<int >map[1000000+10];
int vist[100000];
struct node
{
int x,t;
};
vector<int>::iterator i;//定义迭代器i
void bfs()
{
node st,ed;
queue<node>Q;
st.x=0,st.t=0;
Q.push(st);
while(!Q.empty())
{
st=Q.front();
Q.pop();
if(st.t==d)
continue;
vist[st.x]=1;
for(i=map[st.x].begin();i!=map[st.x].end();i++)//迭代器i指向容器首位,并向下指直到容器末
{
if(!vist[*i])//i是指针类的
{
ed.x=*i;
ed.t=st.t+1;
vist[*i]=1;
Q.push(ed);
}
}
}
}
int main()
{
int t,s,c;
int ans;
scanf("%d",&c);
while(c--)
{
scanf("%d%d",&n,&d);
memset(vist,0,sizeof(vist));
for(int i=0;i<n;i++)
map[i].clear();
for(int i=0;i<n-1;i++)//注意少一
{
scanf("%d%d",&s,&t);
map[s].push_back(t);//队尾插入队列,相当于联通s,t
map[t].push_back(s);
}
ans=0;
bfs();
for(int i=0;i<n;i++)
{
if(!vist[i])
{
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}