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. Java Keyword -- super

    Reference: super When we override superclass's methods, but still want to invoke them, we can use ke ...

  2. 关于xhEditor

    关于xhEditor xhEditor是一个基于jQuery开发的简单迷你并且高效的在线可视化HTML编辑器,基于网络访问并且兼容IE 6.0+,Firefox 3.0+,Opera 9.6+,Chr ...

  3. wget命令1(转载)

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...

  4. 2、elasticsearch 的安装和插件的安装

    1.安装Elasticsearch集群 1.下载elasticsearch-2.0.0.tar.gz,执行tar -zxvf elasticsearch-2.0.0.tar.gz解压 2.修改conf ...

  5. Unity 手指触摸的方向(单手)

    最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~ using UnityEngine; using System.Collections; public class Demo : MonoB ...

  6. textField 判断输入长度限制

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  7. MFC学习之程序执行过程梳理

    *首先利用全局变量对象theApp启动应用程序.这是由于这个全局对象,基类CWinApp中this的指针才干指向这个对象.假设没有这个全局对象,程序在编译时不会出错,但在执行时就会出错. *调用全局应 ...

  8. 【贪心+中位数】【新生赛3 1007题】 Problem G (K)

    Problem G Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  9. 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)

    例题4  墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...

  10. Sublime+Emmet

    Sublime使用Package Control安装Emmet插件: 按Ctrl+Shift+P命令板 输入install然后选择install Package,然后输入emmet找到 Emmet C ...