码迷,mamicode.com
首页 > 其他好文 > 详细

杭电4707--Pet (Dfs)

时间:2015-08-06 12:59:24      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

 

Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1788    Accepted Submission(s): 863


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
 

 

Source
 

 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5352 5351 5350 5349 5348 
 
RE:  宠物丢了, 主人根据地图找, 求找到宠物有几种可能(距根节点距离 > n →→ 宠物可以找到)。 Rt,  题意我也是醉了。
技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 struct node
 7 {
 8     int next, to;   
 9     int step;
10 } a[100005];
11 
12 int head[100005];
13 int n, d, len, ans;
14 
15 void Add(int x, int y)
16 {
17     a[len].to = y;
18     a[len].next = head[x];
19     head[x] = len++;
20 }
21 
22 void Dfs(int x, int step)
23 {
24     int i, j, k;
25     if(head[x] == -1)
26         return;
27     for(i = head[x]; i!=-1; i = a[i].next)  
28     {  
29         k = a[i].to;  
30         a[i].step = step+1;  //这里的step和结构体里边的不一样; 
31        if(a[i].step>d)      
32            ans++;  
33        Dfs(k,a[i].step);    //结构体已经清零。 
34    }  
35 
36 }
37 
38 int main()
39 {
40     int t, i, j, x, y;
41     while(~scanf("%d", &t))
42     {
43         while(t--)
44         {
45             memset(head, -1, sizeof(head));
46             memset(a, 0, sizeof(a));
47             scanf("%d %d", &n, &d);
48             len = 0;
49             for(i=1; i<n; i++)
50             {
51                 scanf("%d %d", &x, &y);
52                 Add(x, y);
53             }
54             ans = 0;
55             Dfs(0, 0);
56             printf("%d\n", ans);
57         } 
58     }
59     return 0;
60 }
结构体建树, 深搜

 

//STL真是强大。 
技术分享
 1 #include <vector>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 int dis[100005];  vector<int>v[100005];
 8 void Dfs(int x)
 9 {
10     for(int i = 0; i < v[x].size(); i++)
11     {
12         dis[v[x][i]] = dis[x] + 1;
13         Dfs(v[x][i]);
14     }
15     return;
16 }
17 int main()
18 {
19     int t;
20     scanf("%d", &t);
21     while(t--)
22     {
23         int n, m, x, y;
24         scanf("%d %d", &n, &m);
25         for(int i=1; i < n; i++)
26             v[i].clear();
27         for(int i=1; i<n; i++)
28         {
29             scanf("%d %d", &x, &y); 
30             v[x].push_back(y);
31         }
32         memset(dis, 0, sizeof(dis));  //数组清0, 保存路径经过结点个数;  
33         Dfs(0);
34         int ans = 0;
35         for(int i=1; i<n; i++)   
36         {
37             if(dis[i] > m)
38             ans++;
39         }
40         printf("%d\n", ans);
41     }
42     return 0;
43 }
vector建树, 深搜
 

杭电4707--Pet (Dfs)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4707546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!