团体程序设计天梯赛 L2-031 深入虎穴 (25分)(bfs)

题目链接:

L2-031 深入虎穴 (25分)

思路:

bfs一下即可

代码:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;
int n, par[maxn];
vector<int> son[maxn];

void bfs(int u){
	int now;
	queue<int> que;
	que.push(u);
	while(!que.empty()){
		now = que.front();
		que.pop();
		for(int & x : son[now]) que.push(x);
	}
	printf("%d", now);
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int k, d;
		scanf("%d", &k);
		while(k--){
			scanf("%d", &d);
			son[i].push_back(d);
			par[d] = i;	
		}
	}
	int root = 1;
	while(par[root]) root = par[root];
	bfs(root);
	return 0;
}
发布了281 篇原创文章 · 获赞 7 · 访问量 6722

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/103974709