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. 通过项目逐步深入了解Mybatis<三>

    Mybatis 高级知识 安排:对订单商品数据模型进行分析 订单商品数据模型 数据模型分析思路: 1.每张表记录的数据内容(分模块对每张表记录的内容进行熟悉,相当于学习系统需求的过程) 2.每张表重要 ...

  2. NSNotificationCenter 传对象

    [[NSNotificationCenter defaultCenter] postNotificationName:@"postCity" object:pro]; [[NSNo ...

  3. php 中利用json_encode和json_decode传递包括特殊字符的数据

    </pre><span style="font-size:24px"></span><pre name="code" ...

  4. Android 4.4 Kitkat Phone工作流程浅析(六)__InCallActivity显示更新流程

    本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象,与Google原生AOSP有些许差异,请读者知悉. ...

  5. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  6. 谋哥:研究App排行榜浮出的神器

    昨天发的<App排行榜的秘密>到头条网,阅读量到2万,踩的比顶的多几倍.原因是由于我使用360市场的数据来分析,而且这帮喷子根本不看你分析数据背后的意义,反正看到自己不喜欢的比方" ...

  7. 使用有限状态机(FSM)编写的敌人AI

    using UnityEngine; using System.Collections; public class AttackState : FSMState { public AttackStat ...

  8. Oracle SQL函数之字符串函数

    1.SQL> ) from dual; --ASCLL(x)返回x的ASCLL码,CHR(x)返回ASCLL码为x的字符 ASCII() ---------- ---------- ------ ...

  9. ANCS协议翻译

    综述 苹果通知中心(Apple Notification Center Service, ANCS)的目的是提供给蓝牙外设一种简单.方便的获取ios设备通知信息的方式. 依赖 ANCS的使用没有依赖, ...

  10. JSON 日期格式带 T 问题

    var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd"; object obj = new  ...