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. LeeCode(Database)-Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  2. Yii2 框架下bootstrap 弹窗预览视频等~

    Yii2 本身已经引用了'yii\bootstrap\BootstrapAsset',所以使用bootstrap 非常简洁. 1 在PHP页面引用命名空间 use app\assets\AppAsse ...

  3. debian msyql 5.1 卸载与安装

    卸载:apt-get autoremove --purge mysql-server-5.1 卸载服务端 apt-get remove mysql-common #一定要卸载(包含配置文件) dpkg ...

  4. Unity 2DSprite

    Unity官方意识到在4.3版本之前,并没有自带的支持2D游戏工具,商店里面有很多有名2D插件Uni2D,2DToolkit,在4.3版本之后就出现UISprite精灵来支持2D游戏开发,我用这个很多 ...

  5. iOS https认证 && SSL/TLS证书申请

    1.下面列出截止2016年底市面上常见的免费CA证书: 腾讯云SSL证书管理(赛门铁克TrustAsia DV SSL证书)阿里云云盾证书服务(赛门铁克DV SSL证书)百度云SSL证书服务Let's ...

  6. sybase用户管理(创建、授权、删除)

    一.登录用户管理:1.创建用户:sp_addlogin loginame, passwd [, defdb] [, deflanguage] [, fullname] [, passwdexp] [, ...

  7. hdu2095 像水题的不错题 异或运算

    异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...

  8. 新一代分布式任务调度框架:当当elastic-job开源项目的10项特性

    作者简介: 张亮,当当网架构师.当当技术委员会成员.消息中间件组负责人.对架构设计.分布式.优雅代码等领域兴趣浓厚.目前主导当当应用框架ddframe研发,并负责推广及撰写技术白皮书.   一.为什么 ...

  9. EF查询数据库框架的搭建

    一个简单的EF查询框架除了运行项目外,大概需要5个类库项目,当然这个不是一定要这样做,这可以根据自己的需要设置有多少个项目.这里介绍的方法步骤只适合EF零基础的人看看就是了. 在开始之前,先建立一个运 ...

  10. Ubuntu Server 安装部署 Cacti 服务器监控

    本文的英文版本链接是 http://xuri.me/2013/10/20/install-the-cacti-server-monitor-on-ubuntu-server.html Cacti是一套 ...