Pet(hdu 4707 BFS)

Pet

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

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

给定一个树,根节点为0,找到距离根节点大于d的节点的个数。bfs搜索

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <queue>
 5 #include <vector>
 6 using namespace std;
 7 int n,d;
 8 vector<int > map[100000+10];
 9 int vis[100000+10];
10 struct node
11 {
12     int x;
13     int t;
14 }tem,top;
15 vector<int>::iterator p;
16 void bfs()
17 {
18     int i,j;
19     queue <node> s;
20     top.x=0,top.t=0;
21     s.push(top);
22     while(!s.empty())
23     {
24         top=s.front();
25         s.pop();
26         if(top.t==d)
27             continue;
28         vis[top.x]=1;
29         for(p=map[top.x].begin();p!=map[top.x].end();p++)
30         {
31             if(!vis[*p])
32             {
33                 tem.x=*p;
34                 tem.t=top.t+1;
35                 vis[*p]=1;
36                 s.push(tem);
37             }
38         }
39     }
40 }
41 int main()
42 {
43     int T;
44     int s,t;
45     int ans;
46     freopen("in.txt","r",stdin);
47     scanf("%d",&T);
48     while(T--)
49     {
50         scanf("%d%d",&n,&d);
51         memset(vis,0,sizeof(vis));
52         for(int i=0;i<n;i++)    map[i].clear();
53         for(int i=0;i<n-1;i++)
54         {
55             scanf("%d%d",&s,&t);
56             map[s].push_back(t);
57             map[t].push_back(s);
58         }
59         ans=0;
60         bfs();
61         for(int i=0;i<n;i++)
62         {
63             if(!vis[i])
64             {
65                 ans++;
66                 //cout<<i<<" ";
67             }
68         }
69         printf("%d\n",ans);
70     }
71 }
时间: 2024-10-10 16:04:55

Pet(hdu 4707 BFS)的相关文章

逃离迷宫(HDU 1728 BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21384    Accepted Submission(s): 5180 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem si

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 1253:胜利大逃亡(简单三维BFS)

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24937    Accepted Submission(s): 9535 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

图结构练习——BFS——从起始点到目标点的最短步数(邻接表+BFS)

图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天灾,一个叫近卫.在他们所在的地域,有n个隘口,编号为1..n,某些隘口之间是有通道连接的.其中近卫军团在1号隘口,天灾军团在n号隘口.某一天,天灾军团的领袖巫妖王决定派兵攻打近卫军团,天灾军团的部队如此庞大,甚至可以填江过河.但是巫妖王不想付出不必要的代价,他想知道在不修建任何通道的前提下,部队是否

URAL 1837. Isenbaev&#39;s Number (map + Dijkstra || BFS)

1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time champion of Ural, vice champion of TopCoder Open 2009, and absolute champion of ACM ICPC 2009. In the time you will spend reading this problem stateme

[LeetCode] Surrounded Regions(DFS、BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

hdu1885Key Task(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 这题和hdu1429是姊妹题  请参见传送门 题目: Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1187    Accepted Submission(s): 470 Problem Description The Czech Technical University is rath

hdu1429胜利大逃亡(续)(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 题意及思路 最开始我以为跟普通的bfs一样,所以直接写了一个朴素的bfs,一跑,前两组数据对了,但是第三组不对,一看,走过的还可以走啊,所以不能标记,结果我的bfs乱改,最后 毫无疑问改成了死循环.所以看题解... 思路:因为有10中不同的钥匙,每种都有两种状态,所以结合计算机是二进制保存的特点,刚好把这10把钥匙当成每一个为,要要1<<10个位保存所有的状态,然后就是模拟捡起钥匙,捡起钥匙就是说明这个位上的数字变成1这个状态,所以自然而然想到了位运算,只要|一下