Pet

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

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
题意:读了半天没理解,其实就是一个图求深度大于D的数的个数;还可以用邻接表和并差集做;//前提不能成环;;;
代码:
 #include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>vec[];
int dis[];
void dfs(int x){
    for(int i=;i<vec[x].size();i++){
            dis[vec[x][i]]=dis[x]+;//深度加一;vec【x】【i】代表b的值也就是dis【b】等于a的深度加一;
            dfs(vec[x][i]);//如果vec[x].size()不为0;下一步搜索,改变深度的值;下次就是b了;
    }
    return;
    }
int main(){
    int T,N,D,a,b,flot;
    scanf("%d",&T);
    while(T--){memset(dis,,sizeof(dis));flot=;
        scanf("%d%d",&N,&D);
        for(int i=;i<N;i++)vec[i].clear();
        for(int i=;i<N-;i++){
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
        }
        dfs();
        for(int i=;i<N;i++){
            if(dis[i]>D)flot++;
        }
        printf("%d\n",flot);
    }
    return ;
}

自己写的邻接表:

 #include<stdio.h>
#include<string.h>
struct Node{
int now;
int next;
int step;
};
Node map[];
int head[];
int len,anser,N,D;//每次加一代表的是当前树的总结点数
void add(int x,int y){
map[len].now=y;//now放第二个 y
map[len].next=head[x];//next指向第一个x;也就是y指向x;
head[x]=len++;
}
void dfs(int x,int st){
if(st>D)anser++;//深度大于D答案就++;
for(int i=head[x];i!=-;i=map[i].next){//相当于vector的size,这个以链表的查找方式;
map[i].step=st+;
dfs(map[i].now,map[i].step);//将当前搜索到的与head[i]相接的now传下去,继续搜索;
}
return;
}
int main(){
int T,a,b;
scanf("%d",&T);
while(T--){len=;anser=;
memset(map,,sizeof(map));
memset(head,-,sizeof(head));//将head数组的初始化为-1
scanf("%d%d",&N,&D);
for(int i=;i<N-;i++){
scanf("%d%d",&a,&b);
add(a,b);
}
dfs(,);
printf("%d\n",anser);
}
return ;
}

自己写的并查集:

 #include<stdio.h>
const int MAXN=;
int tree[MAXN];
int N,D;
int find(int x){
int r=x;
while(r!=tree[r])r=tree[r];
return r;
}
int depth(int x){int tot=;
while(x!=tree[x]){
x=tree[x];tot++;
}
return tot;
}
void initial(){
for(int i=;i<=N;i++)tree[i]=i;
}
void merge(int x,int y){
tree[y]=x;//由于是找深度,没有路径压缩,所以直接并上;
}
int findanswer(){int answer=;
for(int i=N;i>;i--){
if(depth(i)>D&&find(i)==)answer++;
// printf("%d\n",depth(i));
}
return answer;
}
int main(){
int T,a,b,answer;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&D);
initial();
for(int i=;i<N-;i++){
scanf("%d%d",&a,&b);
merge(a,b);
}
answer=findanswer();
printf("%d\n",answer);
}
return ;
}

大神们用的邻接表和并差集先贴着吧;有空自己写写:

邻接表:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int next,to;
int step;
}a[];
int head[];
int n,d,len,ans;
void add(int x,int y)
{
a[len].to = y;
a[len].next = head[x];
head[x] = len++;
}
void dfs(int x,int step)
{
int i,j,k;
if(- == head[x])
return ;
for(i = head[x]; i!=-; i = a[i].next)
{
k = a[i].to;
a[i].step = step+;
if(a[i].step>d)
ans++;
dfs(k,a[i].step);
}
}
int main()
{
int T,i,j,x,y;
scanf("%d",&T);
while(T--)
{
memset(head,-,sizeof(head));
memset(a,,sizeof(a));
scanf("%d%d",&n,&d);
len = ;
for(i = ; i<n; i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
ans = ;
dfs(,);
printf("%d\n",ans);
}

并差集:

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAXN 110000
using namespace std;
int pri[MAXN];
int sum,m;
int find(int x)
{
int r=x;
while(r!=pri[r])
r=pri[r];
return r;
}
int num(int a)
{
int b=;
while(a!=pri[a])
{
a=pri[a];
b++;
}
return b;
}
void fun()
{
for(int i=MAXN;i>;i--)
{
if(find(i)==&&num(i)>m)//根节点为0且距离大于m
sum++;
}
}
int main()
{
int n,i,a,b,t;
scanf("%d",&t);
while(t--)
{
sum=;
for(i=;i<MAXN;i++)
pri[i]=i;
scanf("%d%d",&n,&m);
for(i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
pri[b]=a;//直接并,不用查
}
fun();
printf("%d\n",sum);
}
return ;
}
 

Pet(dfs+vector)的更多相关文章

  1. 蓝桥杯--- 历届试题 大臣的旅费 (DFS & Vector)

    题目提交链接:http://lx.lanqiao.org/problem.page?gpid=T32 问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国 ...

  2. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. UVA 291 The House Of Santa Claus(DFS算法)

    题意:从 节点1出发,一笔画出 圣诞老人的家(所谓一笔画,就是遍访所有边且每条边仅访问一次). 思路:深度优先搜索(DFS算法) #include<iostream> #include&l ...

  4. 历届试题 邮局(dfs+剪枝)

      历届试题 邮局   时间限制:1.0s   内存限制:256.0MB      问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...

  5. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  6. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  7. HDU 1010 Tempter of the Bone (DFS+剪枝)

    题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...

  8. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

  9. 【HDU - 1010】Tempter of the Bone(dfs+剪枝)

    Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...

随机推荐

  1. LRU Cache 解答

    Question Design and implement a data structure for Least Recently Used (LRU) cache. It should suppor ...

  2. 探索PHP+Nginx(二) 安装PHP

    首先,我们简单了解一下什么是PHP,PHP(Hypertext Preprocessor 超文本预处理器) 和Java语言一样,PHP也是属于高级语言,并不能直接在操作系统上运行.Java运行需要虚拟 ...

  3. Android专项面试训练题(一)

    1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...

  4. 在php添加mongo过程中出现的mongo.so: > undefined symbol: php_json_encode in Unknown on line 0. After installation mongo driver for php 的错误

    3 down vote my system is centos 6.3. I got the problem solved. vim /etc/php.ini then add extension=j ...

  5. JSP中的include的两种用法

    1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用法区别 (1)执行时间上区别 < ...

  6. 九度OnlineJudge之1032:ZOJ

    题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. 输入: 题目包含多组用例,每组用例占一行,包含ZOJ三个 ...

  7. Visual Studio® 2010 Web Deployment Projects站点编译生成bin同时发表插件

    VS2010环境下: 1.Visual Studio® 2010 Web Deployment Projects下载地址:        http://www.microsoft.com/downlo ...

  8. C#整理8——结构体

    结构体:相当于是我们自己定义的一种复杂的类型.int... double float bool char string DateTime 数组类型生活中大部份的对象都是复合型的对象. 如何定义结构体类 ...

  9. 80端口被NT kernel & System 占用pid= 4的解决方法

    引用http://www.2cto.com/os/201111/111269.html的方法.亲测可用 该进程是Http.sys.它是http API的驱动组件,Http栈服务器.如果该端口被Http ...

  10. OC——NSDictionary和NSMutableDictionary

    //初始化 NSString *key1 = @"key1"; NSString *key2 = @"key2"; NSString *key3 = @&quo ...