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. 《如何让TT T4模板输出多个文件(VS2010中)》-- access911.net 文章

    <如何让TT T4模板输出多个文件(VS2010中)>-- access911.net 文章   问题:   VS2010中自带的 TT 模板功能挺好用,但是如何定义其输出的目录,或者如何 ...

  2. 格而知之3:Core Data的基本使用

    最近准备做一个随手笔记类的app给自己用,考虑到从未使用过Core Data,就决定用Core Data来做数据存储.在网上参考了一些Core Data的资料后,用一天的时间写了这个demo,主要测试 ...

  3. Android学习总结——系统提示对话框(AlertDialog)

    new AlertDialog.Builder(MainActivity.this).setTitle("退出")//设置对话框标题 .setMessage("官人可是要 ...

  4. AJAX最简单的原理以及应用

    Ajax是创建快速动态网页的技术,通过后台与服务器少量的数据交互,是网页实现异步更新.也就是在不整个刷新页面的情况下,可以更新网页中的局部区域. 在原始web应用的模式中: 浏览器       以 h ...

  5. java的System.getProperty()方法能够获取的值

    java.version Java 执行时环境版本号 java.vendor Java 执行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装 ...

  6. c-version:null]] could not deserialize the servlet-context scoped attribute with name: "MENU_LIST"

    <Jul 26, 2013 10:45:02 AM CST> <Error> <HTTP> <BEA-101362> <[ServletConte ...

  7. IE 中创建 子窗口 传值 与接收值 【window.showModalDialog】

    父窗口 创建一个窗口 var backinfo = window.showModalDialog('UserSelect.aspx', '', 'dialogHeight=600px; dialogW ...

  8. WPF学习记录1:ListView的一个模板

    在网上找的一个模板,放在这里,作为笔记,收集 <ListView Grid.Column=" Name="ListmuLu" > <ListView.I ...

  9. hadoop python and Twitter

    http://www.wubiaoblog.com/archives/1159 http://blog.csdn.net/anbo724 http://f.dataguru.cn/forum.php? ...

  10. trangleProble switch方法 java

    public class trangleProblem { static int res=1; int codePart=1; int n=100; Stack<Param> stack= ...