Group Programming ladder match L2-031 Cronicas (25 points) (bfs)

Topic links:

L2-031 Cronicas (25 points)

ideas:

bfs what can

the code:

#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;
}
Published 281 original articles · won praise 7 · views 6722

Guess you like

Origin blog.csdn.net/qq_45228537/article/details/103974709