1076 Forwards on Weibo (30分)

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxv = 1005;
int n, L;
struct node {
	int id;
	int level;
};
vector<node> Adj[maxv];
void BFS(int id) {
	bool inq[maxv] = { false }; //放在函数体里面或者作为全局变量但每次调用时需要重新初始化
	queue<node> q;
	node start{id ,0}; //起始点
	q.push(start);
	inq[id] = true;
	int num=0;
	while (!q.empty()) {
		node top = q.front();
		q.pop();
		for (int i = 0; i < Adj[top.id].size(); i++) {
			node next = Adj[top.id][i];
			next.level = top.level + 1;
			if (inq[next.id] == false&&next.level<=L) { //层号大于L就不入队
				q.push(next);
				inq[next.id] = true;
				num++;
			}
		}
	}
	cout << num << endl;
}

int main() {
	cin >> n >> L;
	for(int i=1;i<=n;i++){
		int m; cin >> m;
		for (int j = 0; j < m; j++) {
			int t; cin >> t;
			Adj[t].push_back(node{ i,0 });
		}
	}
	int k; cin >> k;
	while (k--) {
		int t; cin >> t;
		BFS(t);
	}
	return 0;
}
发布了26 篇原创文章 · 获赞 5 · 访问量 440

猜你喜欢

转载自blog.csdn.net/weixin_43590232/article/details/104041583