注意题意输入粉丝的时候,第i行是第i个关注了谁,而不是他的粉丝。
在遍历的时候也是只转发第一次,所以要一个isVis数组去标记一下。
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <set>
#define MAX 1010
using namespace std;
int n, l;
struct Node {
int level;
vector <int> follow;
Node() { level = 1; }
};
Node list[MAX];
vector <int> s;
bool isVis[MAX];
void LevelOrder(int u) {
queue <int> q;
q.push(u);
isVis[u] = true;
list[u].level = 1;
int topLevel = list[u].level;
int NowLevel = topLevel;
while (!q.empty()) {
int top = q.front();
q.pop();
NowLevel = list[top].level;
if (NowLevel - topLevel < l) {
for (int i = 0; i < list[top].follow.size(); i++) {
int v = list[top].follow[i];
if (!isVis[v]) {
list[v].level = NowLevel + 1;
q.push(v);
isVis[v] = true;
s.push_back(v);
}
}
}
}
}
int main() {
cin >> n >> l;
int co, t;
for (int i = 1; i <= n; i++) {
cin >> co;
for (int j = 0; j < co; j++) {
cin >> t;
list[t].follow.push_back(i);
}
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> t;
memset(isVis, false, sizeof(isVis));
s.clear();
LevelOrder(t);
cout << s.size() << endl;
}
return 0;
}