Pet(dfs+vector)

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的数的个数;还可以用邻接表和并差集做;

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<vector>
 4 using namespace std;
 5 vector<int>vec[100010];
 6 int dis[100010];
 7 void dfs(int x){
 8     for(int i=0;i<vec[x].size();i++){
 9             dis[vec[x][i]]=dis[x]+1;//深度加一;vec【x】【i】代表b的值也就是dis【b】等于a的深度加一;
10             dfs(vec[x][i]);//如果vec[x].size()不为0;下一步搜索,改变深度的值;下次就是b了;
11     }
12     return;
13     }
14 int main(){
15     int T,N,D,a,b,flot;
16     scanf("%d",&T);
17     while(T--){memset(dis,0,sizeof(dis));flot=0;
18         scanf("%d%d",&N,&D);
19         for(int i=0;i<N;i++)vec[i].clear();
20         for(int i=0;i<N-1;i++){
21             scanf("%d%d",&a,&b);
22             vec[a].push_back(b);
23         }
24         dfs(0);
25         for(int i=0;i<N;i++){
26             if(dis[i]>D)flot++;
27         }
28         printf("%d\n",flot);
29     }
30     return 0;
31 }

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

邻接表:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int next,to;
 8     int step;
 9 }a[100005];
10 int head[100005];
11 int n,d,len,ans;
12 void add(int x,int y)
13 {
14     a[len].to = y;
15     a[len].next = head[x];
16     head[x] = len++;
17 }
18 void dfs(int x,int step)
19 {
20     int i,j,k;
21     if(-1 == head[x])
22         return ;
23     for(i = head[x]; i!=-1; i = a[i].next)
24     {
25         k = a[i].to;
26         a[i].step = step+1;
27         if(a[i].step>d)
28             ans++;
29         dfs(k,a[i].step);
30     }
31 }
32 int main()
33 {
34     int T,i,j,x,y;
35     scanf("%d",&T);
36     while(T--)
37     {
38         memset(head,-1,sizeof(head));
39         memset(a,0,sizeof(a));
40         scanf("%d%d",&n,&d);
41         len = 0;
42         for(i = 1; i<n; i++)
43         {
44             scanf("%d%d",&x,&y);
45             add(x,y);
46         }
47         ans = 0;
48         dfs(0,0);
49         printf("%d\n",ans);
50     }

并差集:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define MAXN 110000
 6 using namespace std;
 7 int pri[MAXN];
 8 int sum,m;
 9 int find(int x)
10 {
11     int r=x;
12     while(r!=pri[r])
13     r=pri[r];
14     return r;
15 }
16 int num(int a)
17 {
18     int b=0;
19     while(a!=pri[a])
20     {
21         a=pri[a];
22         b++;
23     }
24     return b;
25 }
26 void fun()
27 {
28     for(int i=MAXN;i>0;i--)
29     {
30         if(find(i)==0&&num(i)>m)//根节点为0且距离大于m
31         sum++;
32     }
33 }
34 int main()
35 {
36     int n,i,a,b,t;
37     scanf("%d",&t);
38     while(t--)
39     {
40         sum=0;
41         for(i=0;i<MAXN;i++)
42         pri[i]=i;
43         scanf("%d%d",&n,&m);
44         for(i=0;i<n-1;i++)
45         {
46             scanf("%d%d",&a,&b);
47             pri[b]=a;//直接并,不用查
48         }
49         fun();
50         printf("%d\n",sum);
51     }
52     return 0;
53 }

时间: 2024-10-10 10:20:19

Pet(dfs+vector)的相关文章

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

hdu4499 Cannon (DFS+回溯)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499 Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 214 Problem Des

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

素数环(dfs+回溯)

题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 方法1:(生成测试法,会超时) #include <bits/stdc++.h>#define MAXN 100using namespace std; int isp[MAXN], a[MAXN]; void get_prime(void)          //*****素数打表{    m

POJ 2907 Collecting Beepers (DFS+回溯)

Description Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in he

数独问题(DFS回溯)

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty c