HDU4707 pet

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值