考查点:BFS
思路:第一次用DFS只有20分,有很多情况没有考虑,直接用BFS过了,BFS要记录每个点的层数,满足层数时更新个数
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAXN 2013
#define oo 0x3f3f3f3f
using namespace std;
int num,n;
int weight;
vector<int> wb[MAXN];
int vis[MAXN];
void BFS(int index,int& num)
{
memset(vis,-1,sizeof(vis));
queue<int> q;
int level=0;
q.push(index);
vis[index]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
int d=vis[u];
for(int i=0;i<wb[u].size();i++)
{
int v=wb[u][i];
if(vis[v]==-1){
q.push(v);
vis[v]=d+1;
if(vis[v]<=weight){
num++;
}
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
scanf("%d%d",&n,&weight);
int k,x;
FOR(i,1,n)
{
scanf("%d",&k);
FOR(j,1,k)
{
scanf("%d",&x);
wb[x].push_back(i);
}
}
scanf("%d",&k);
FOR(i,1,k)
{
memset(vis,0,sizeof(vis));
int id;
scanf("%d",&id);
int num=0;
BFS(id,num);
printf("%d\n",num);
}
return 0;
}