#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
vector<int> G[1010];
int N,L,K;
bool vis[1010];
int total=0;
void BFS(int st)
{
queue<int> q;
q.push(st);
vis[st]=true;
int preend=st,last=st,level=0;
while(level<=L&&!q.empty())
{
int top=q.front();
q.pop();
total++;
for(int i=0;i<G[top].size();i++)
{
int index=G[top][i];
if(vis[G[top][i]]==false)
{
q.push(G[top][i]);
last=G[top][i];
vis[G[top][i]]=true;
}
}
if(top==preend)
{
level++;
preend=last;
}
}
}
int main()
{
scanf("%d%d",&N,&L);
for(int i=1;i<=N;i++)
{
int num;
scanf("%d",&num);
for(int j=0;j<num;j++)
{
int f;
scanf("%d",&f);
G[f].push_back(i);
}
}
scanf("%d",&K);
for(int i=0;i<K;i++)
{
memset(vis,false,sizeof(vis));
total=0;
int p;
scanf("%d",&p);
BFS(p);
printf("%d\n",total-1);
}
return 0;
}
PAT甲
最新推荐文章于 2022-12-04 15:26:29 发布