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

Pet(dfs+vector)

时间:2015-08-05 00:37:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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     }
View Code

并差集:

技术分享
 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 }
View Code
 

Pet(dfs+vector)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4703376.html

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