hdu 4707 bfs

bfs基础算法水题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>

using namespace std;

const int Max = 1e5+50;

int dist[Max];
vector<int> tree[Max];

int N, D, T;

void init()
{
	for(int i = 0; i < Max-40; i ++)
	{
		tree[i].clear();
	}
	memset(dist, 0, sizeof(dist));
}

int ans;
void bfs()
{
	int t = 0, dis = 0;
	queue<int> q;
	q.push(t);
	ans ++;
	while(!q.empty())
	{
		t = q.front();
		q.pop();
		if(dist[t] + 1 > D) continue;
		for(int i = 0; i < tree[t].size(); i ++)
		{
			dist[tree[t][i]] = dist[t] + 1;
			ans ++;
			q.push(tree[t][i]);
		}
	}
}

int main()
{
	int x, y;

	scanf("%d", &T);
	while(T --)
	{
		init();
		scanf("%d%d", &N, &D);
		for(int i = 0; i < N-1; i ++)
		{
			scanf("%d%d", &x, &y);
			tree[x].push_back(y);
		}
		ans = 0;
		bfs();
		printf("%d\n", N-ans);
	}

	return 0;
} 

hdu 4707 bfs

时间: 2024-10-22 05:14:56

hdu 4707 bfs的相关文章

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 sear

hdu 1175 bfs 转弯题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 和之前的1728类似.就是判断转弯数,建立一个用于记录转弯数的数组.. 还有就是对于特殊情况要进行考虑,比如起点与终点相同的情况,对于本题来说是不可以消去的应该输出NO.还有就是起点或终点是零这也是不行的,因为0代表没有棋子... 还有在判断能不能走的时候要小心,对于判断条件一定要小心,不要图赶快写.. 错误的地方都写在注释中了.. 代码: // hdu 1175 bfs 转弯数 //1.起点

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 1072 bfs

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7083    Accepted Submission(s): 3409 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a tim

hdu 1252 BFS

1 /* 2 题意:给出一个m*m矩阵表示的完全图,且每个点都有自环,每条边都有一种颜色:有三个玩家ABC的三张纸片分别初始在 3 某三个位置(可以重叠),纸片可以沿着边走一步,可以走的路径的规则为:若A要走到某个点i,则A-i的颜色要和B-C的颜 4 色相同:问最少要走多少步.(题意太难懂了,看了别人的说明才弄懂了题意) 5 6 题解:BFS 7 首先初步分析题意似乎很难用图论来解决,那么就是搜索/DP/数据结构,然后从搜索方面去思考的话,就是要找状态,然 8 后初步列出所有信息,三个点得位置

hdu 4707 Pet【BFS求树的深度】

Pet                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1909    Accepted Submission(s): 924 链接:pid=4707">Click Me ! Problem Description O

hdu 4707 仓鼠 记录深度 裸bfs

题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且图中没有回路,每个房间都是联通的,求仓鼠可能出现的房间的数量. Sample Input110 20 10 20 31 41 52 63 74 86 9 Sample Output2 1 #include <cstdio> 2 #include <algorithm> 3 #inclu

hdu 4707 Pet(dfs,bfs)

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1548    Accepted Submission(s): 733 Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He sear

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};