码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4707 Pet

时间:2015-08-20 19:01:14      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:搜索   dfs   bfs   

Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1909    Accepted Submission(s): 924


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<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.
 

Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
 

Sample Input
1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
 

Sample Output
2
 

Source
 

Recommend
liuyiding
 
题目意思是给出一颗有n个节点,且以0为根节点的树,要求深度大于d的节点数目。数据有10w,时限2s,o(n)的复杂度还是可以接受的。做法其实有很多种最容易想到的是搜索,深搜宽搜均可,还有就是直接保存每个结点的深度,然后爆搜扫一遍。
DFS代码如下:

/*
Author:ZXPxx
Memory: 5904 KB		Time: 1201 MS
Language: C++		Result: Accepted
*/

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int mx = 1e5 + 5;
vector<int> ma[mx];
int n, d, cnt;
bool vis[mx];

void dfs(int p, int deep) {
    if (deep > d)
        return;
    vis[p] = 1;
    cnt++;
    int len = ma[p].size();
    for(int i = 0; i < len; i++)
        if (!vis[ma[p][i]])
            dfs(ma[p][i], deep + 1);
}

int main() {
    int t, a, b;
    scanf("%d", &t);
    while (t--) {
        for(int i = 0; i < n; i++)
            ma[i].clear();
        memset(vis, 0, sizeof(vis));
        cnt = 0;
        scanf("%d%d", &n, &d);
        for(int i = 0; i < n-1; i++) {
            scanf("%d%d", &a, &b);
            ma[a].push_back(b);
            ma[b].push_back(a);
        }
        dfs(0, 0);
        printf("%d\n", n - cnt);
    }
    return 0;
}

BFS代码如下:

/*
Author:ZXPxx
Memory: 10108 KB		Time: 764 MS
Language: C++		Result: Accepted
*/

#include <cstdio>
#include <cstring>
#include <vector>
#define mx 100005
using namespace std;

vector<int> mp[mx];
int deep[mx*10],Que[mx];

void BFS(int s) {
    int v,temp,front=0,rear=1;
    memset(deep,-1,sizeof(deep));
    deep[s]=0;
    Que[front]=s;
    while(front<rear) {
        v=Que[front++];
        for(int i=0; i<mp[v].size(); ++i) {
            temp=mp[v][i];
            if(deep[temp]==-1) {
                deep[temp]=deep[v]+1;
                Que[rear++]=temp;
            }
        }
    }
}

int main() {
    int x,y,n,D,t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&D);
        for(int i=0; i<mx; ++i)
            mp[i].clear();
        for(int i=1; i<n; ++i) {
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        BFS(0);
        int ans=0;
        for(int i=0; i<n; ++i) {
            if(deep[i]>D)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

爆搜代码如下:

/*
Author:ZXPxx
Memory: 2112 KB		Time: 577 MS
Language: C++		Result: Accepted
*/

#include<cstring>
#include<cstdio>
using namespace std;
const int mx=1e5+5;
int a[mx];
int main() {
    int t,n,d,x,y;;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&d);
        memset(a,0,sizeof(a));
        for(int i=0; i<n-1; i++) {
            scanf("%d%d",&x,&y);
            a[y]=a[x]+1;
        }
        int ans=0;
        for(int i=0; i<n; i++)
            if(a[i]>d)
                ans++;
        printf("%d\n",ans);
    }
    return 0;
}


个人觉得这题用爆搜做最好……

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 4707 Pet

标签:搜索   dfs   bfs   

原文地址:http://blog.csdn.net/zhang_xueping/article/details/47808453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!