hdu4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

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.
题意:说的是Lin Ji丢失了一个宠物,他在房间放了一些cheess陷阱等了3天都没有抓到。现在他拿到了一份学校地图,有n个房间,n-1条边,从0开始,也就是Lin Ji的房间号。还给你一个distance D,表示最远能抓到宠物的距离。现在问有多少个宠物可能在的地方。(在小于D的距离内,宠物是会跑回来陷入陷阱的)。
题解:看题意可知宠物一定是在比D大的地方,也就是到0房间的距离大于D。统计个数就可以了。最开始想的是求单源的最短路径,然后统计路径大于D的个数,发现bellman,SPFA什么的都是TLE..- -.. 换了n个建图的方式。。然后我重写,直接定义一个全部为0的数组,输入x,y表示x到y是可达的,我就将a[y]=a[x]+1,表示x到y的距离是a[x]的值加1。并且同时统计大于D的个数。(再开一个for循环统计个数也是会TLE的。。)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#define maxn 100005
using namespace std;
int a[maxn];
int main()
{
    int T,n,d,ans;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&d);
        ans=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[y]=a[x]+1;
            if(a[y]>d||a[x]>d)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值