您的位置:首页 > 其它

浙大PAT甲级 1076

2016-08-28 15:49 274 查看
广度优先搜索。用queue来实现,且用mark来表示userid是否出现。

AC代码:

#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 999999999
using namespace std;
vector<int> v[1001];
int n,m;
int mark[1001];
struct node
{
int lay;
int data;
};
queue<node> q;
void bfs(int x)
{
node w;
w.lay=0;
w.data=x;
q.push(w);
while(!q.empty())
{
node tmp=q.front();
q.pop();
for(int i=0;i<v[tmp.data].size();i++)
{
node gg;
gg.data=v[tmp.data][i];
if(mark[gg.data]==1)
continue;
gg.lay=tmp.lay+1;
if(gg.lay!=m)
{
q.push(gg);
}
mark[gg.data]=1;
}
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
int d;
scanf("%d",&d);
for(int j=1;j<=d;j++)
{
int t;
scanf("%d",&t);
v[t].push_back(i);
}
}
int d;
scanf("%d",&d);
for(int i=0;i<d;i++)
{
memset(mark,0,sizeof(mark));
int t;
scanf("%d",&t);
bfs(t);
int ans=0;
for(int j=1;j<=n;j++)
{
if(j==t) continue;
if(mark[j]==1)
ans++;
}
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: