博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电4707--Pet (Dfs)***
阅读量:5933 次
发布时间:2019-06-19

本文共 5549 字,大约阅读时间需要 18 分钟。

 

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:            
 
RE:  宠物丢了, 主人根据地图找, 求找到宠物有几种可能(距根节点距离 > n →→ 宠物可以找到)。 Rt,  题意我也是醉了。

 //STL真是强大。 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 int dis[100005]; vector
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
m)38 ans++;39 }40 printf("%d\n", ans);41 }42 return 0;43 }
vector建树, 深搜

 

1 #include 
2 #include
3 #include
4 using namespace std; 5 int len; 6 int ans; 7 8 struct tree 9 {10 int to; //弧头节点序号;11 int next; //下一条边的序号; 12 int step; //用作权值; 13 } a[100005]; //第几条边; 14 15 int head[100005], m, n;16 17 void Add(int x, int y)18 {19 a[len].to = y;20 a[len].next = head[x];21 head[x] = len++; //序号为i的弧尾节点发出的第一条边的序号; 22 }23 24 void Dfs(int x, int y)25 {26 int i, j, k;27 if(head[x] = -1)28 return;29 for(i = head[x]; i != -1; i = a[i].next) //x弧尾节点序号, i为x所发边序号, 30 {31 k = a[i].to; //弧头节点序号; 32 a[i].step += 1;33 if(a[i].step > n)34 ans++;35 Dfs(k, a[i].step); 36 }37 }38 39 int main()40 {41 int t;42 scanf("%d", &t);43 while(t--)44 {45 int m, n;46 memset(a, 0, sizeof(a));47 memset(head, -1, sizeof(head));48 scanf("%d %d", &m, &n);49 len = 0;50 for(int i = 1; i < m; i++)51 {52 int x, y;53 scanf("%d %d", &x, &y);54 Add(x, y);55 }56 ans = 0;57 Dfs(0, 0);58 printf("%d\n", ans);59 }60 return 0;61 }
结构体建树,  深搜

 

1 #include 
2 #include
3 #include
4 using namespace std; 5 int m, n; 6 int father[100005]; 7 void init() 8 { 9 for(int i = 0; i < m; i++)10 father[i] = i;11 }12 int find(int a, int flag) 13 {14 int sum = 0;15 while(a != father[a]) 16 {17 a = father[a]; ////记录查找次数; 18 sum++;19 }20 if(flag)21 return a;22 else23 return sum;24 }25 int main()26 {27 int t;28 scanf("%d", &t);29 while(t--)30 {31 scanf("%d %d", &m, &n);32 init();33 int total = 0;34 for(int i = 1; i < m; i++)35 {36 int a, b;37 scanf("%d %d", &a, &b);38 father[b] = a; //只建立关系, 不合并; 39 if(find(b, 1) == 0 && find(b, 0) > n)40 total++;41 }42 printf("%d\n", total);43 }44 return 0; 45 }
并查集, 神思路

 

1 #define MAXNODES 100 2 #define MAXEDGES 1005 3 int first[MAXNODES], next[MAXEDGES];  //记录邻接表信息;  4 //first[i]可以理解为第i条链的头,它指向所有以编号为i的点为起点的边中的第一条边(存储的是边的编号)  5 //next[i]是什么呢?它其实就是链表结点的next指针,它指向下一条边(同样存储的是边的编号)。 6 int u[MAXEDGES], v[MAXEDGES], w[MAXNEDGES]; //起点, 终点, 边权;  7 int e;    //边的条数;  8  9 void init(){10     memset(first, -1, sizeof(first));11     e = 0;    12 } 13 14 //add函数是怎么链表的插入过程呢?15 //在这里,因为first[i]记录的是第i个点指向的第一条边的编号,所以在插入链表的时候,我们插入的是链表的头部,而不是尾部。16 void add(int a, int t, int c){17     u[e] = s; v[e] = t; w[e] = c;18 //那么这两句就很好理解了。先把e这条边的下一条设置成之前的链表头,然后再把表头指向现在的这条边e。19 //注意顺序不能反,就像链表的插入和删除一样,顺序很重要,不能把链给断了。20     next[e] = first;21     first[s] = e++;22 }
临接表, 详解

 

 

转载于:https://www.cnblogs.com/soTired/p/4707546.html

你可能感兴趣的文章
闭包 !if(){}.call()
查看>>
python MySQLdb安装和使用
查看>>
Java小细节
查看>>
poj - 1860 Currency Exchange
查看>>
chgrp命令
查看>>
Java集合框架GS Collections具体解释
查看>>
洛谷 P2486 BZOJ 2243 [SDOI2011]染色
查看>>
linux 笔记本的温度提示
查看>>
数值积分中的辛普森方法及其误差估计
查看>>
Web service (一) 原理和项目开发实战
查看>>
跑带宽度多少合适_跑步机选购跑带要多宽,你的身体早就告诉你了
查看>>
广平县北方计算机第一届PS设计大赛
查看>>
深入理解Java的接口和抽象类
查看>>
java与xml
查看>>
Javascript异步数据的同步处理方法
查看>>
快速排序——Java
查看>>
iis6 zencart1.39 伪静态规则
查看>>
SQL Server代理(3/12):代理警报和操作员
查看>>
基于事件驱动的DDD领域驱动设计框架分享(附源代码)
查看>>
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>